問題描述
我有一個循環.我創建了一個 QCheckBox
并將它放在一個 QTableWidget
單元格中,一切正常.在循環的每一步中,我都為 myslot SLOT 調用了一個 connect
函數,但只應用了最后一個 QCheckBox
實例.我用谷歌搜索了很多,發現很多人都有我的問題.我已經應用了他們的解決方案,但我的問題仍然存在.
I have got a loop. I created a QCheckBox
and put it in a QTableWidget
cell, and everything is Ok. In each step of loop I have called a connect
function, for myslot SLOT, but only the last QCheckBox
instance is applied. I googled a lot and have found many people have my problem. I have applied their solutions, but my problem remains.
for row in xrange(len(uniqueFields)):
instance = QtGui.QCheckBox(uniqueFields[row], findInstance.tableWidget)
print QtCore.QObject.connect(instance,
QtCore.SIGNAL(_fromUtf8("stateChanged (int)")),
lambda: findInstance.projectsInstance.myslot(
"TWCH", findInstance, instance.text(),
instance.checkState(), instance))
findInstance.tableWidget.setRowCount(findInstance.tableWidget.rowCount() + 1)
findInstance.tableWidget.setCellWidget(row, 0, instance)
注意:我的connect
函數返回True
.
如何在枚舉所有實例
的循環中創建connect
函數?
How to create connect
function in a loop that enumerates all of the instances
?
推薦答案
我有同樣的問題,你應該使用 functools.partial
如:
I have same problem , you should use functools.partial
such as:
for key, val in a_DICT_THAT_YOU_STORED_YOUR_OBJECTS_AND_STRINGS:
obj = partial( findInstance.projectsInstance.myslot,arg1="TWCH",arg2=self,arg3=key,arg4=val.checkState() )
QtCore.QObject.connect(val, QtCore.SIGNAL(_fromUtf8("stateChanged (int)")), obj)
當然,argX 應該設置為你的函數名參數的真實名稱.
Of course, argX should set to your real name of your argument of your function name.
這篇關于循環中的 QtCore.QObject.connect 只影響最后一個實例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!