简体   繁体   English

QMessageBox exec似乎不返回QDialog :: DialogCode

[英]QMessageBox exec doesnt seem to return QDialog::DialogCode

My application's closeEvent() looks nearly like this (Qt 5.8.0 on Windows): 我的应用程序的closeEvent()几乎像这样(Windows上为Qt 5.8.0):

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(some_changes_were_made)  // bool
    {
        QMessageBox mbox;
        mbox.setText("Page(s) have been changed.");
        mbox.setInformativeText("What do you want to do?");
        mbox.addButton("Exit now", QMessageBox::AcceptRole);
        mbox.addButton("Save page(s) first", QMessageBox::RejectRole);

        int exit_code = mbox.exec();

        if(exit_code == QDialog::Rejected)
        {
           // bail out of the close event so the user can save pages
           event->ignore();
           return;
        }
    }

    event->accept();
}

I'm curious if the documentation is wrong, which states that exec() returns a QDialog::DialogCode . 我很好奇文档是否错误,该文档指出exec()返回QDialog::DialogCode It actually seems to return the QMessageBox::ButtonRole (which interestingly is the inverse value). 它实际上似乎返回了QMessageBox::ButtonRole (有趣的是,它是反值)。 Or am I just doing something totally wrong here? 还是我在这里做错了什么?

Please forgive any typos, as I'm unable to copy the actual code here. 请原谅任何错别字,因为我无法在此处复制实际代码。

Check QMessageBox reference here. 在此处检查QMessageBox参考。

It is supposed to return one of the standardButton replies. 它应该返回standardButton答复之一。 You are using QDialogBox replies, QMessageBox has already overriden QDialogBox 's exec method. 您正在使用QDialogBox答复, QMessageBox已经覆盖了QDialogBoxexec方法。

You want to check something like this: 您想检查如下内容:

switch (exit_code) {
  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;
}

Source from the same link. 来自同一链接的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM