Results 1 to 5 of 5

Thread: Multiple connections to one method

  1. #1
    Join Date
    May 2007
    Location
    Huntsville, Alabama
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple connections to one method

    I have ten buttons connected to one method:

    connect(m_myPushButton1, SIGNAL(clicked()), this, SLOT(MyMethod()));
    connect(m_myPushButton2, SIGNAL(clicked()), this, SLOT(MyMethod()));
    ...
    connect(m_myPushButton10, SIGNAL(clicked()), this, SLOT(MyMethod()));


    Is there a way I can find out what button did the click in this method?

    void TestClass::MyMethod()
    {
    //if button1 made the click, place a 1 in the spinner box
    //if button2 make the click, place a 2 in the spinner box
    // etc.
    m_SpinnerBox->setValue(...find the button that made the click...);
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple connections to one method

    You can use QObject::sender() in the slot and do a dynamic cast to QPushButton and compare the result with your ten buttons.

    But it would really be nicer if you had ten slots, one for each button.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multiple connections to one method

    QButtonGroup is there for exactly this purpose.
    J-P Nurmi

  4. #4
    Join Date
    May 2007
    Location
    Huntsville, Alabama
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple connections to one method

    Thanks. In reality, I have MANY more slots than my example has. All it The method has to do is take input data from a list , evaluate the data, and add it to a spinner box that corresponds the button I pressed. So for each button, there is an associated spinner box. I guess I would not only have to figure out the button I pressed, but I would have to map a spinner box with the button (perhaps a layout?)

    ...Just read jpn's reply after submitting this...I'll give his idea a try. Thanks

  5. #5
    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: Multiple connections to one method

    QSignalMapper can also be used, especially if you want to map spin boxes. You can then have a slot that takes a spin box pointer as its argument (just make sure it's cast to QWidget*).

Similar Threads

  1. Replies: 4
    Last Post: 10th March 2007, 18:01
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

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.