开发者

pyqt: connect signal dynamically with user-supplied variables?

开发者 https://www.devze.com 2023-03-02 20:28 出处:网络
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

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.p开发者_如何学GoyqtSlot("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?


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:

d = ModifyRect()

l = lambda: modifyRectAccepted(ctrl_name, rect_name)
d.accepted.connect(l)

def modifyRectAccepted(self, ctrl_name, rect_name):
    #foo
0

精彩评论

暂无评论...
验证码 换一张
取 消