Results 1 to 5 of 5

Thread: Passing additional arguments to a slot in pyqt4

  1. #1
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    Smile Passing additional arguments to a slot in pyqt4

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing additional arguments to a slot in pyqt4

    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,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    Default Re: Passing additional arguments to a slot in pyqt4

    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(value)))

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

    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
    Last edited by pazgs; 12th August 2015 at 16:48.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing additional arguments to a slot in pyqt4

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing additional arguments to a slot in pyqt4

    Quote Originally Posted by pazgs View Post
    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.

    Quote Originally Posted by pazgs View Post
    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,
    _

Similar Threads

  1. Passing arguments to compiler
    By aaditya190 in forum Newbie
    Replies: 3
    Last Post: 18th December 2013, 11:41
  2. Replies: 2
    Last Post: 24th February 2013, 11:16
  3. Passing arguments
    By papillon in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2012, 00:44
  4. Replies: 12
    Last Post: 7th September 2010, 13:53
  5. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 22:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.