Results 1 to 16 of 16

Thread: SLOT and QPushButton

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default SLOT and QPushButton

    Hi,
    I' have connect (from Designer) two QPushButton to the same SLOT; and within the SLOT I'd like to know wich button called the SLOT. Is it possible?
    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Qt Code:
    1. void mySlot() {
    2. QPushButton * b = qobject_cast<QPushButton *>(sender());
    3. if (b) {
    4. if (b == button1) {
    5. //button1 SIGNAL()ed
    6. } else if (b == button2) {
    7. //button2 SIGNAL()ed
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by mickey
    Hi,
    I' have connect (from Designer) two QPushButton to the same SLOT; and within the SLOT I'd like to know wich button called the SLOT. Is it possible?
    You can use QObject::sender() and cast to a pushbutton. A better (for me that is) alternative would be to use a QButtonGroup. I believe I've had the same discussion with someone else in the forums

    EDIT:
    @Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
    Last edited by yop; 12th February 2006 at 16:47.

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: SLOT and QPushButton SOLVED

    I solved so:
    Qt Code:
    1. QPushButton* button = (QPushButton*)(sender());
    To copy to clipboard, switch view to plain text mode 

    but "qobject_cast" what is it? I don't find it!
    Thanks
    Regards

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: SLOT and QPushButton

    but "qobject_cast" what is it? I don't find it!
    in previous post from Yop :

    EDIT:
    @Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
    You'll find it in QT4 docs : QObject class
    Last edited by Everall; 13th February 2006 at 10:30.

  6. #6
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: SLOT and QPushButton

    Quote Originally Posted by yop
    @Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
    Sorry, guys I haven't bother to take a look at QT3 docs to make sure qobject_cast<> is available in QT3...

  7. #7
    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: SLOT and QPushButton

    You can use dynamic_cast instead. The sender has to be a QObject derived class, so dynamic_cast should work the same (if the compiler supports rtti of course).

  8. #8
    Join Date
    Feb 2006
    Location
    Berlin
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Hi there,

    I don't use the Designer, but have the same problem. Merely I don't want to write for every QButton a connect line. So I decide to use a QButtonGroup. But I dont know, which Signal I must use. Here is the Code:

    Qt Code:
    1. buttons = new QButtonGroup();
    2.  
    3. buttons->addButton(einer1, 1);
    4. buttons->addButton(zweier1, 2);
    5. buttons->addButton(dreier1, 3);
    6. ...blabla
    7.  
    8. connect(buttons, SIGNAL(???), this, SLOT(createMenus()));
    To copy to clipboard, switch view to plain text mode 

    And how can I use the button in the Slot createMenus()?

    Someone told me that i also can use a signalmapper. What about this possibility?

    Thanks

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by meissner
    But I dont know, which Signal I must use.
    Try this:
    Qt Code:
    1. connect( buttons, SIGNAL( clicked( int ) ), this, SLOT( createMenus() ) );
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by jacek
    Try this:
    Qt Code:
    1. connect( buttons, SIGNAL( clicked( int ) ), this, SLOT( createMenus() ) );
    To copy to clipboard, switch view to plain text mode 
    The signal has an int argument, the slot has none is that legal?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by yop
    The signal has an int argument, the slot has none is that legal?
    Of course it is:
    From Qt docs:
    The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)

  12. #12
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by jacek
    Of course it is: ...some rtfm
    I would swear that it wasn't but since it was too obvious to miss, I thought I'd better ask and I'm glad I did. Thanks...

  13. #13
    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: SLOT and QPushButton

    Quote Originally Posted by yop
    I would swear that it wasn't but since it was too obvious to miss, I thought I'd better ask and I'm glad I did. Thanks...
    Slot are generaly invoked as function calls. If a slot has less parameters than the signal (provided that only the last arguments of the signals are missing) it is possible to tell, which parameters of the signal are mapped to which parameters of the slot. That's why it is possible for a slot to have less parameters than the signal connected to it. The situation is exactly the same as with default arguments for functions.

  14. #14
    Join Date
    Feb 2006
    Location
    Berlin
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Ok, it works.

    But how can I get the id from the QButton of the QButtonGroup that was clicked to use it in my SLOT?

    For example:
    Qt Code:
    1. QPushButton* cbutton = (QPushButton*)(sender());
    2. buttons->button( buttons->id( cbutton ) )->setText("bla");
    To copy to clipboard, switch view to plain text mode 
    This code produce an Segmentation fault and I guess the sender() funktion is stupid in this context, isn't it?

  15. #15
    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: SLOT and QPushButton

    The "int" value of QButtonGroup::buttonClicked signal carries it.

  16. #16
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SLOT and QPushButton

    Quote Originally Posted by meissner
    Ok, it works.

    But how can I get the id from the QButton of the QButtonGroup that was clicked to use it in my SLOT?

    For example:
    Qt Code:
    1. QPushButton* cbutton = (QPushButton*)(sender());
    2. buttons->button( buttons->id( cbutton ) )->setText("bla");
    To copy to clipboard, switch view to plain text mode 
    This code produce an Segmentation fault and I guess the sender() funktion is stupid in this context, isn't it?
    Yes in fact, that is one of the dangers of using sender() - you break the loose coupling and need to know exactly what kind of widget invoked the slot (of course using a C++-style dynamic_cast<> instead of (QPushButton*) as advised earlier would have trapped the error).

    Change the signature of your function to take an int parameter. This will contain the id of the button that was pressed. Then you can do:

    Qt Code:
    1. buttons->button(id)->setText("bla");
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. retrieving signal name in a slot
    By Baschterl in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 20:44
  2. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 14:44
  3. Replies: 2
    Last Post: 23rd May 2008, 13:22
  4. Replies: 2
    Last Post: 4th May 2006, 19:17

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.