QtCore.QObject.connect in a loop only affects the last instance(循环中的 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 只影响最后一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:循环中的 QtCore.QObject.connect 只影响最后一个实例


基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 设计字符串本地化的最佳方法 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04