PDA

View Full Version : intercepting button clicks in QDialogButtonBox



ceraolo
22nd November 2013, 07:32
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):


void CProgOptions::on_buttonBox_clicked(QAbstractButton *button)
{
if(button==MYRESETBUTTON){
HERE GOES MY CODE
}
}

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.

stampede
22nd November 2013, 07:39
void CProgOptions::on_buttonBox_clicked(QAbstractButton *button)
{
if(button== buttonBox->button(QDialogButtonBox::Reset) ){
HERE GOES MY CODE
}
}
should work fine.

anda_skoa
22nd November 2013, 17:15
Alternatively you can use the pointer to the button and directly connect to its clicked() signal.

Cheers,
_

ceraolo
22nd November 2013, 20:06
It works!.
I just had to add a cast and to use ui-> (since I generated the buttonBox inside Qt Designer) as follows:

if((QPushButton *)button== ui->buttonBox->button(QDialogButtonBox::Reset) ){
HERE GOES MY CODE
}

Thanks again for the help.

skunkos
23rd November 2013, 10:30
I would recommend avoid using C-style casts in C++ application, its matter of consistency. Use static_cast in this situation,

anda_skoa
23rd November 2013, 11:36
And you don't have to cast at all if you connect directly to the button in question :)

Cheers,
_

markwal
17th August 2015, 00:54
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:



...
// in dialog class definition in header file
private slot:
void on_reset_clicked();
...
// in the dialog cpp file:
#include <QPushButton>
...
// in dialog contructor:
connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(on_reset_clicked())
...
// and finally the clicked method
void on_reset_clicked()
{
// Your code here
}

Larswad
17th March 2020, 12:13
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:



...
// in dialog class definition in header file
private slot:
void on_reset_clicked();
...
// in the dialog cpp file:
#include <QPushButton>
...
// in dialog contructor:
connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(on_reset_clicked())
...
// and finally the clicked method
void on_reset_clicked()
{
// Your code here
}



markwal, by far the best solution using signals for each!