循环中的 QtCore.QObject.connect 只影响最后一个实例 [英] QtCore.QObject.connect in a loop only affects the last instance

查看:43
本文介绍了循环中的 QtCore.QObject.connect 只影响最后一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环.我创建了一个 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 只影响最后一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆