如何在 PyQT 中使用预定义的 SIGNAL 将参数传递给函数
假设您在一个类中有 3 个 QRadioButtons 实例
self.Option1 = QRadioButton()
self.Option2 = QRadioButton()
self.Option2 = QRadioButton()
(为简洁起见,我没有编写整个脚本) 并且您想在用户单击它时使用它们来执行特定的函数,因此您可以执行
self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)
How do i pass argument to myFunction so it Knows the QRadioButtons was selected by the user?所以我可以做这样的事情
def myFunction(choice):
if choice == Option1:
do something
if choice == Option2:
do something
现在我有3个函数,每个函数都像这样的每个连接
self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction1)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)
这种方法工作得很好,但是你可以看到代码增长的速度有多快,因为对于我添加的每个小部件,我至少必须编写一个新功能或修改现有功能,这将是一场维护噩梦(更不用说它看起来丑陋并且妨碍代码重用)。
快速访问谷歌让我得到了这个:(
self.label = QLabel(" ")
self.connect(self, SIGNAL("didSomething"),
self.update_label)
self.do_something()
def do_something(self):
self.emit(SIGNAL("didSomething"), "important", "information")
def update_label(self, value1, value2):
self.label.setText(value1 + " " + value2)
我再次没有包含整个脚本,但你可以查看它 此处) 他所做的是创建一个方法来定义一个自定义发射器,该发射器在发射时发送参数,然后使用手动激活它。
self.do_something()
因此,很自然地,connect 函数选择此发射并将参数传递给函数 update_label,部分实现了我想要做的事情。但他是手动完成的,有没有办法自动“拦截”标准信号(例如 QRadioButtons 发出的 clicked() 信号)并向其添加参数,以便将它们传递给函数来使用它们?
提前致谢
lets suppose you have 3 QRadioButtons instances inside a class
self.Option1 = QRadioButton()
self.Option2 = QRadioButton()
self.Option2 = QRadioButton()
(for brevity i didn't wrote the entire script)
and you want to use them to execute a particular function when the user clicks it, so you do
self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)
How do i pass arguments to to myFunction so it knows wich of the QRadioButtons was clicked by the user? so i can do stuff like
def myFunction(choice):
if choice == Option1:
do something
if choice == Option2:
do something
Right now i have 3 functions, each for every connect like this
self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction1)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)
And this approach works just fine, but you can see how quickly the code can grow because with every widget i add i'll have to write at least one new function or modify the existing ones, its going to be a maintainance nightmare(not to mention that it looks ugly and it prevents code reuse).
A quick visit to google got me this:
self.label = QLabel(" ")
self.connect(self, SIGNAL("didSomething"),
self.update_label)
self.do_something()
def do_something(self):
self.emit(SIGNAL("didSomething"), "important", "information")
def update_label(self, value1, value2):
self.label.setText(value1 + " " + value2)
(again i didnt include the entire script but you can check it out here)
What he did was creating a method to define a custom emitter that sends arguments when emitted and then activated it manually using
self.do_something()
So naturally, the connect function picks this emition and pass the arguments to the function update_label, partially achieving what i want to do. But he is doing it manually, is there a way to automatically "intercept" standard signals(for example the clicked() signal QRadioButtons emit) and add arguments to it, so they get passed to a function to work with them?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这个问题的Qt解决方案是QSignalMapper。但我以前也遇到过同样的问题,我发现使用“部分”功能更简单。像这样使用它:
有关更多信息: http://docs.python.org/库/functools.html#functools.partial
The Qt solution of this problem is QSignalMapper I think. But I had the same problem before, and I figured out using "partial" function is simplier. Use it like that:
For more info: http://docs.python.org/library/functools.html#functools.partial