pyqt:将信号与用户提供的变量动态连接?
我使用自定义类创建一个对话框:
d = ModifyRect(ctrl_name, rect_name)
它以非模式方式显示。当它被接受或拒绝时,我想在我的主窗口上调用一个函数,传递这两个变量,即应该调用这个插槽:
@QtCore.pyqtSlot("QString","QString")
def modifyRectAccepted(self, ctrl_name, rect_name):
#foo
How do I go aboutconnecting d
's accepted< /code> 到我的
MainWindow
的 modifyRectAccepted
,传入这两个参数?或者甚至,连接两者,但至少传递 ModifyRect
实例,以便我可以从那里获取它们。
在 pygtk 中,这非常简单 - 您可以将更多变量传递到 connect 中,它们会被转发,并且在任何情况下,发出的小部件总是会传入。PyQt 中的等效概念是什么?
I create a dialog with a custom class:
d = ModifyRect(ctrl_name, rect_name)
It is shown modelessly. When it is accepted or rejected, I want to call a function on my MainWindow passing in these two variables, i.e. this slot should be called:
@QtCore.pyqtSlot("QString","QString")
def modifyRectAccepted(self, ctrl_name, rect_name):
#foo
How do I go about connecting d
's accepted
to my MainWindow
's modifyRectAccepted
, passing in those 2 parameters? Or even, connect the two, but at least pass the ModifyRect
instance in so I can grab them from there.
in pygtk this is pretty simple - you can pass more variables into connect
and they are forwarded, and in any case the emitting widget is always passed in. What's the equivalent concept in PyQt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能直接这样做。您需要使用信号映射器或中间槽。请参阅此问题回答了一些问题几天前。
或者,如果您只对信号源感兴趣,请使用 QObject 的
sender()
方法。编辑:
要使用闭包作为中间槽,您必须使用 New-syle< /a> 信号和槽范例。这种方式更加Pythonic,可以让你指定任何可调用的。像这样:
You cannot do that directly. You need to use signal mapper or intermediate slot. See this question answered a few days ago.
Or, if you're just interested in the source of the signal, use QObject's
sender()
method.EDIT:
To use closures as intermediate slots you must use the New-syle signal and slot paradigm. This way is more pythonic and lets you specify any callable. Like this: