問題描述
我有一個循環(huán).我創(chuàng)建了一個 QCheckBox
并將它放在一個 QTableWidget
單元格中,一切正常.在循環(huán)的每一步中,我都為 myslot SLOT 調(diào)用了一個 connect
函數(shù),但只應(yīng)用了最后一個 QCheckBox
實(shí)例.我用谷歌搜索了很多,發(fā)現(xiàn)很多人都有我的問題.我已經(jīng)應(yīng)用了他們的解決方案,但我的問題仍然存在.
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
函數(shù)返回True
.
如何在枚舉所有實(shí)例
的循環(huán)中創(chuàng)建connect
函數(shù)?
How to create connect
function in a loop that enumerates all of the instances
?
推薦答案
我有同樣的問題,你應(yīng)該使用 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)
當(dāng)然,argX 應(yīng)該設(shè)置為你的函數(shù)名參數(shù)的真實(shí)名稱.
Of course, argX should set to your real name of your argument of your function name.
這篇關(guān)于循環(huán)中的 QtCore.QObject.connect 只影響最后一個實(shí)例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!