Results 1 to 8 of 8

Thread: intercepting button clicks in QDialogButtonBox

  1. #1
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default intercepting button clicks in QDialogButtonBox

    I have a QDialogButtonBox with Reset, OK, Cancel buttons. This was created (i.e. put in a dialog windows) using Qt Designer.
    To intercept clicks on OK and Cancel buttons is straightforward using accepted() and rejected() signals.

    To intercept clicks on the Reset button I have to use the following signal:
    void clicked(QAbstractButton * button)
    I.e. I can use the following slot (CProgOptions is the class name of my dialog window):

    Qt Code:
    1. void CProgOptions::on_buttonBox_clicked(QAbstractButton *button)
    2. {
    3. if(button==MYRESETBUTTON){
    4. HERE GOES MY CODE
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    My problem is: what should I put in the code instead of "MYRESETBUTTON"?
    In other words, where can I find the name of the pointer to the Reset button?

    Thanks to anyone that would consider answering.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: intercepting button clicks in QDialogButtonBox

    Qt Code:
    1. void CProgOptions::on_buttonBox_clicked(QAbstractButton *button)
    2. {
    3. if(button== buttonBox->button(QDialogButtonBox::Reset) ){
    4. HERE GOES MY CODE
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    should work fine.

  3. The following user says thank you to stampede for this useful post:

    ceraolo (22nd November 2013)

  4. #3
    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: intercepting button clicks in QDialogButtonBox

    Alternatively you can use the pointer to the button and directly connect to its clicked() signal.

    Cheers,
    _

  5. #4
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: intercepting button clicks in QDialogButtonBox

    It works!.
    I just had to add a cast and to use ui-> (since I generated the buttonBox inside Qt Designer) as follows:
    Qt Code:
    1. if((QPushButton *)button== ui->buttonBox->button(QDialogButtonBox::Reset) ){
    2. HERE GOES MY CODE
    3. }
    To copy to clipboard, switch view to plain text mode 
    Thanks again for the help.

  6. #5
    Join Date
    Jul 2012
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: intercepting button clicks in QDialogButtonBox

    I would recommend avoid using C-style casts in C++ application, its matter of consistency. Use static_cast in this situation,

  7. #6
    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: intercepting button clicks in QDialogButtonBox

    And you don't have to cast at all if you connect directly to the button in question

    Cheers,
    _

  8. #7
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: intercepting button clicks in QDialogButtonBox

    It's an old thread, but my internet search turned it up so for other travellers I thought it'd be helpful to translate anda_skoa's suggestion into code:

    Qt Code:
    1. ...
    2. // in dialog class definition in header file
    3. private slot:
    4. void on_reset_clicked();
    5. ...
    6. // in the dialog cpp file:
    7. #include <QPushButton>
    8. ...
    9. // in dialog contructor:
    10. connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(on_reset_clicked())
    11. ...
    12. // and finally the clicked method
    13. void on_reset_clicked()
    14. {
    15. // Your code here
    16. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Mar 2020
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: intercepting button clicks in QDialogButtonBox

    Quote Originally Posted by markwal View Post
    It's an old thread, but my internet search turned it up so for other travellers I thought it'd be helpful to translate anda_skoa's suggestion into code:

    Qt Code:
    1. ...
    2. // in dialog class definition in header file
    3. private slot:
    4. void on_reset_clicked();
    5. ...
    6. // in the dialog cpp file:
    7. #include <QPushButton>
    8. ...
    9. // in dialog contructor:
    10. connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(on_reset_clicked())
    11. ...
    12. // and finally the clicked method
    13. void on_reset_clicked()
    14. {
    15. // Your code here
    16. }
    To copy to clipboard, switch view to plain text mode 
    markwal, by far the best solution using signals for each!

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2011, 20:21
  2. Replies: 4
    Last Post: 20th July 2011, 12:37
  3. Replies: 2
    Last Post: 20th December 2010, 18:51
  4. Replies: 3
    Last Post: 12th May 2010, 14:11
  5. Replies: 1
    Last Post: 29th July 2009, 21:25

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.