Results 1 to 3 of 3

Thread: Can I connect multiple buttons to same slot, but pass different parameters ?

  1. #1
    Join Date
    Jun 2010
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Can I connect multiple buttons to same slot, but pass different parameters ?

    Hey guys .. My question here is regarding signals and slots ..

    For example, say you have like 10 buttons, all of which pass a number into a factorial function .. the only difference is that they all pass a different number into this one factorial function ... suppose the function is:

    int factorial (int a);

    so for example, button1 evaluates this function where a =1,
    so for example, button2 evaluates this function where a =2,
    so for example, button3 evaluates this function where a =3,

    and so on ..

    In such a case, is there any way to connect these multiple 'clicked' signals from each individual button to the same factorial slot, but just pass in a different value for a .. ? The alternative is to make a connect statement for each individual button, which seems rather inefficient considering they all do more or less the same thing ..

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Can I connect multiple buttons to same slot, but pass different parameters ?

    There are several ways to do this, but the most logical way, from a design standpoint, would be to extend your button to emit a signal with an integer argument. This signal can be connected to the button's clicked() signal. The data itself can be stored in the new button class, or simply extracted from the button's label and converted to an integer.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I connect multiple buttons to same slot, but pass different parameters ?

    A good alternative would be to use QSignalMapper but you'll still need to do a separate connection for each button.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Why is not possible call slot with parameters?
    By somename in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2010, 13:17
  2. Connect button and pass QListView current index
    By been_1990 in forum Qt Programming
    Replies: 3
    Last Post: 30th November 2009, 16:20
  3. Replies: 4
    Last Post: 3rd May 2009, 18:32
  4. Context menu / connect with parameters
    By pietrom in forum Newbie
    Replies: 1
    Last Post: 18th February 2009, 18:20
  5. Replies: 2
    Last Post: 16th August 2007, 00:20

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.