所有的信号(例如以下代码中的textChanged)将被丢弃,一旦我使用DummyWidget().setupUi(mainWin)
, printDummy函数将被静音,但当我使用dw = DummyWidget(); dw.setupUi(main)
时,一切都会正常工作。我没有看到Python语法的具体区别。有人能分享一下评论吗?
class DummyWidget(object):
def setupUi(self, parent=None):
assert parent is not None
self.parent = parent
parent.resize(480, 320)
self.DUMMY = QtGui.QLineEdit(parent)
# parent.setCentralWidget(self.DUMMY)
self.DUMMY.textChanged.connect(self.printDummy)
QtGui.QApplication.processEvents()
def printDummy(self):
print "DUMMY IN CLASS"
if __name__ == "__main__":
import sys
def printDummy(*args):
print "DUMMY"
app = QtGui.QApplication(sys.argv)
# mainWin = MainWindow()
# edit = QtGui.QLineEdit()
# edit.textChanged.connect(printDummy)
# mainWin.setCentralWidget(edit)
mainWin = QtGui.QDialog()
# DummyWidget().setupUi(mainWin)
dw = DummyWidget()
dw.setupUi(mainWin)
mainWin.show()
# mainWin.open()
sys.exit(app.exec_())
如果您不保留对DummyWidget
实例的引用,它将被垃圾收集,因此您的插槽将不再存在。
如果您执行DummyWidget.setupUi(parent)
,则不能存储引用(这一行为您提供setupUi
的返回值,即None
)。