开发者

Destructor isn't called after adding a qmenubar

I have the following files (Main window/UI): files

UI:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Sat Apr 23 15:53:12 2011
#     by: PyQt4 UI code generator 4.7.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    ### Presetting Model ###
    model = QtGui.QFileSystemModel()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1000, 600)    
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.horizontalLayout_2 = QtGui.QHBoxLayout(self.centralWidget)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.treeView = QtGui.QTreeView(self.centralWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(2)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.treeView.sizePolicy().hasHeightForWidth())
        self.treeView.setSizePolicy(sizePolicy)
        self.treeView.setHeaderHidden(True)
        self.treeView.setObjectName("treeView")
        self.horizontalLayout.addWidget(self.treeView)
        self.plainTextEdit = QtGui.QPlainTextEdit(self.centralWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(4)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth())
        self.plainTextEdit.setSizePolicy(sizePolicy)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.horizontalLayout.addWidget(self.plainTextEdit)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralWidget)
        ### MENU ###
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 600, 27))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)

        ### Setting up tree view model ###
        self.treeView.setModel(self.model)
        self.treeView.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
#       self.treeView.setRootIndex开发者_运维知识库(self.model.setRootPath(Xmldocument.directorypath))

        ### Hiding additional info in treeview ###
        hHeader = self.treeView.header()
        hHeader.hideSection(1)
        hHeader.hideSection(2)
        hHeader.hideSection(3)

        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "VeloCondDB Browser", None, QtGui.QApplication.UnicodeUTF8))
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

##############################################################

    def __del__(self):
        print "DESTRUCTOR"  

Main Window:

import sys
import os

from browserclass_UI import Ui_MainWindow
from PyQt4 import QtCore, QtGui

class Browser(QtGui.QMainWindow):

#############################################################################################

    def __init__(self, parent=None):
        """Constructor for the main window."""

        QtGui.QWidget.__init__(self, parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        menuitems = ["Open", "Close"]
        menu = self.ui.menuBar.addMenu('File')
        for item in menuitems:
            entry = menu.addAction(item)
            self.connect(entry, QtCore.SIGNAL('triggered()'), lambda item=item: self.doStuff(item)) 

#############################################################################################

    def doStuff(self, item):
        print item

#############################################################################################

if __name__ == "__main__":
    browser = QtGui.QApplication(sys.argv)
    myapp = Browser()
    myapp.show()
    sys.exit(browser.exec_())

When lines from:

menuitems...

to

self.connect...

are not commented, destructor from UI is never being called. If they are commented everything works fine. Any ideas?


You can't rely on __del__ being called for all of your objects, especially when your program is ending (see that other answer)

According to PyQt documentation:

However, if a slot is a lambda function or a partial function then its reference count is automatically incremented to prevent it from being immediately garbage collected.

Some circular references to your Browser object might be created when you connect the signal the lambda functions, and that is what keeps it from being destroyed. As ui is referenced by Browser, it doesn't get destroyed either.

So, you have to disconnect the slots manually when these slots are lambda functions. or use another method than lambda to bind an extra parameters to the slot (e.g. QSignalMapper, or the signal QMenu.triggered that has the QAction as parameter):

def __init__(self, parent):
    ...
    for item in menuitems:
        entry = menu.addAction(item)
    menu.triggered.connect(self.doStuff)

def doStuff(self, entry):
    print entry.text()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜