Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind QMessageBox button to action

Tags:

c++

qt

I created a QMessageBox with Save, Discard and Cancel button:

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
msgBox.setIcon(QMessageBox::Question);

Now, how can I bind each of these buttons to a specific command?

like image 743
user3204810 Avatar asked Jan 23 '26 02:01

user3204810


1 Answers

 QMessageBox msgBox;
 msgBox.setText("The document has been modified.");
 msgBox.setInformativeText("Do you want to save your changes?");
 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
 msgBox.setDefaultButton(QMessageBox::Save);
 int ret = msgBox.exec();

 switch (ret) {
   case QMessageBox::Save:
       // Save was clicked
       break;
   case QMessageBox::Discard:
       // Don't Save was clicked
       break;
   case QMessageBox::Cancel:
       // Cancel was clicked
       break;
   default:
       // should never be reached
       break;
 }
like image 134
Liviu Gheorghisan Avatar answered Jan 24 '26 21:01

Liviu Gheorghisan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!