Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent closing QMessageBox after clicking a button

Tags:

c++

qt

I have 3 buttons on QMessageBox added by QMessageBox::addButton() method. Is it possible to prevent closing the message box if a button has been clicked? By defult every button closes the window, but I don't want to do it for one button.

EDIT: Why is this downvoted?

like image 655
tobi Avatar asked Sep 01 '12 19:09

tobi


1 Answers

One interesting way to approach it that worked for me is to completely disconnect the signals for the target button created, and then re-add the intended functionality. This won't work for everyone, especially if the button isn't created this way and/or you still want to close the dialog correctly. (There might be a way to add it back and/or simulate the behavior with QDialog::accept, QDialog::reject, QDialog::done - haven't tried yet.)

Example:

QMessageBox *msgBox = new QMessageBox(this);
QAbstractButton *doNotCloseButton = msgBox->addButton(tr("This button will not close anything"), QMessageBox::ActionRole);
// Disconnect all events - this will prevent the button from closing the dialog
doNotCloseButton->disconnect();
connect(doNotCloseButton, &QAbstractButton::clicked, this, [=](){ doNotCloseButton->setText("See? Still open!"); });
like image 187
Albert H Avatar answered Nov 10 '22 01:11

Albert H