在 PyQT 中使用 Newstyle 信号传递参数 [英] Passing Arguments with Newstyle signals in PyQT
问题描述
我在 PyQT 中使用一组动态创建的按钮时遇到了一些问题.
I've been having some trouble with a set of dynamically created buttons in PyQT.
我正在使用列表来创建对应于不同值的按钮.因为每个按钮都不同,所以我需要让它们将参数传递给Button Clicked"函数,分别标识它们自己,以便我可以对它们执行不同的操作.这是我目前所拥有的:
I'm using a list to create buttons corresponding to different values. Because each button is different, I need to have them pass an argument to the "Button Clicked" function identifying themselves separately so I can perform different actions with them. Here is what I have so far:
for x in self.buttons:
name = str(x)
button = QtGui.QPushButton(Frame)
button.setText(name)
cp_result = self.changeProject(name)
if cp_result is None:
print 'changeProject(%s) is None!', name
else:
print 'changeProject(%s) is OK (%r)', name, cp_result
#button.clicked.connect(cp_result)
setattr(self, "btn_%s" % name, button)
self.btnList.append(button)
def changeProject(self, name):
for x in self.btnList:
if x.isDown:
#Change UI Frame to blahblah
return 'A String'
与 Alex 合作,这是最新的代码,在 changeProject 上测试返回值,显然不能是字符串,但我仍然需要确定在自动生成的按钮之间按下了哪个按钮.
Working with Alex, this is the latest code, testing a return value on changeProject, which clearly cannot be a string, but I still need to determine which button is pressed between the auto-generated buttons.
当前错误:TypeError:connect() 槽参数应该是可调用的或信号,而不是 'str'
推荐答案
感谢您的帮助.我偶然发现了 QSignalMapper,结果证明这正是我所需要的.
Thanks for the assistance. I stumbled QSignalMapper, and this turned out to be exactly what I needed.
http://pysnippet.blogspot.com/2010/06/qsignalmapper-at-your-service.html
这篇关于在 PyQT 中使用 Newstyle 信号传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!