循环中的 QtCore.QObject.connect 只影响最后一个实例

2021-12-09 00:00:00 pyqt qt qt-signals c++ qtcore

我有一个循环.我创建了一个 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.

相关文章