Slot with extra argument
Solved
Qt for Python
-
I am looking to a solution for such a behaviour:
- the boolean signal
s1
connects to the slottest
with an argument, e.g.widget1
- the boolean signal
s2
connects to the same slottest
with another argument, e.g.widget2
@Slot(bool, QWidget) def test(flag, wg): # the state of the flag is retrieved from the signal wg.apply_flag(flag)
After looking into the Qt and PySide documentations and forums I still have not found anything working...
Has anyone have a solution?Best regards
- the boolean signal
-
@denni-0
I'm sorry, but I don't understand your explanation; especially how the emitted boolean is retrieved?
One of my points being to address a 'standard' widget (i.e. not a subclass) stored in a '.ui' file, and using only its methods. A better example would be:s1 = Signal(bool) s2 = Signal(bool) ... s1.connect(test(w1)) # the destination widget is set only at the connection definition s2.connect(test(w2)) ... s1.emit(True) # no need to add the destination widget every time the signal is used s2.emit(False) ... @Slot(bool, QWidget) # this definition of the Slot is not right according the Signal definition def test(flag, wg): # the state of the flag is retrieved from the signal if flag: wg.setTooltip('OK') else: wg.setToolTip('Not OK')
-
@denni-0
I think I have found the way to factorize my code, usinglamdba
to give an extra argument.s1 = Signal(bool) s2 = Signal(bool) ... s1.connect(lambda flag: test(flag, w1)) s2.connect(lambda flag: test(flag, w2)) ... def test(flag, wg): if flag: wg.setTooltip('OK') else: wg.setToolTip('Not OK')
Note that it works:
- with or without the
@Slot(bool)
decorator - with an user-defined signal
s_user = Signal(bool)
or a built-in signal such asstateChanged
As stated here, this may be generalized with:
obj.signal.connect(lambda p1, ... , arg1=val1, ... : fun(p1, ... , arg1, ... )) ... def fun(p1, ... , arg1, ... ): [...]
- with or without the