PDA

View Full Version : Passing additional arguments to a slot in pyqt4



pazgs
12th August 2015, 12:43
Hi,

In my current code, when the user highlights an item in a combo box, the index gets passed to a function like so below:

self.tab2TrajectoryCombo.highlighted.connect(self. trajectorychanged)
.
.
.
def trajectorychanged(self, value):
'''trajectory changed'''

self.pcgraphs.changetrajectory(int(str(value)))
self.tab2TrajectoryCombo.setCurrentIndex(int(str(v alue)))
self.tab3TrajectoryCombo.setCurrentIndex(int(str(v alue)))

The above works nicely. However, I want to pass an argument to the function in addition to the index from the combobox. So I would do something like:

self.tab2TrajectoryCombo.highlighted.connect(lambd a: self.trajectorychanged(2))
.
.
.
def trajectorychanged(self, value, numbertwo):
'''trajectory changed'''

self.pcgraphs.changetrajectory(int(str(value)))
self.tab2TrajectoryCombo.setCurrentIndex(numbertwo )
self.tab3TrajectoryCombo.setCurrentIndex(numbertwo )

The above code doesn't work though. The error is:

Traceback (most recent call last):
File "/users/gareths/workspace/pyPczplotGUINew/tabwidget.py", line 55, in <lambda>
self.tab2TrajectoryCombo.highlighted.connect(lambd a: self.trajectorychanged(2))
TypeError: trajectorychanged() takes exactly 3 arguments (2 given)

Does anyone know how to do this?

Many thanks in advance

anda_skoa
12th August 2015, 14:36
Is the value associated with the entry in the combobox or with the location on where you make the connect?

If the former: you can attach any data to a combobox entry using the setItemData() method and retrieve it again with itemData().

Btw: in you attempt you are using that other value to change the value of the combobox again, leading to yet another change signal emit

Cheers,
_

pazgs
12th August 2015, 15:48
Hi,

Thank you very much for your response.

It is the index of the combobox which is emitted by "self.tab2TrajectoryCombo.highlighted.connect" when the user highlights an item in the combobox. This index is passed to the function "def trajectorychanged" as variable "value". As you can see I don't have a problem doing this so I don't need "setItemData" or "itemData".

The problem is that in addition to passing the index of the combobox to "def trajectorychanged" via variable "value", I want to pass a second variable to the function via variable "numbertwo".

Is my problem clearer now?

Many thanks again

Added after 17 minutes:

Further to my last post, I want to use the variable "numbertwo" like this. Note that for clarity I have changed the name of the variable from "numbertwo" to "tab"

def trajectorychanged(self, value, tab):
'''trajectory changed'''

self.pcgraphs.changetrajectory(int(str(value)))

if tab == 2:
self.tab3TrajectoryCombo.setCurrentIndex(int(str(v alue)))

elif tab == 3:
self.tab2TrajectoryCombo.setCurrentIndex(int(str(v alue)))

This will prevent a double emission like you rightly pointed out was happening

Basically I have a number of comboboxes on different tabs of a QTabWidget but I want all the indexes to be the same as each other at all times. To do this I need a way to recognise which tab the signal has come from

d_stranz
12th August 2015, 18:02
this I need a way to recognise which tab the signal has come from

QObject::sender() tells you this once you are inside the slot.

As for sending "extra" arguments into a slot: signals and slots are just C++ functions. Their signatures are defined by their declarations in the classes that contain them. If the signal as defined doesn't contain the arguments you'd like, that's too bad, because you can't just arbitrarily add them in a connect call. What you can do is intercept the signal in a slot of your own, then re-emit a new custom signal that adds whatever you'd like. That's the one you connect to in your other class instance.

anda_skoa
13th August 2015, 09:51
It is the index of the combobox which is emitted by "self.tab2TrajectoryCombo.highlighted.connect" when the user highlights an item in the combobox. This index is passed to the function "def trajectorychanged" as variable "value". As you can see I don't have a problem doing this so I don't need "setItemData" or "itemData".

You said you wanted additional data.
I asked whether that was related to the combobox entry.
You reply unrelated stuff.
Not useful.


Basically I have a number of comboboxes on different tabs of a QTabWidget but I want all the indexes to be the same as each other at all times. To do this I need a way to recognise which tab the signal has come from

Different comboboxes means different combobox entries means itemData can be different even if index is the same.
I would have used itemData, but feel free to find some hack to do the same thing.

Cheers,
_