【问题标题】:Yes/No message box using QMessageBox是/否消息框使用 QMessageBox
【发布时间】:2012-10-18 04:18:40
【问题描述】:

如何在 Qt 中显示带有“是/否”按钮的消息框,以及如何检查按下了哪些按钮?

即如下所示的消息框:

【问题讨论】:

    标签: c++ qt qmessagebox


    【解决方案1】:
    QMessageBox 的

    Python 等效代码,其中包含一个问题以及 YesNo 按钮。单击“是”按钮时,它将弹出另一个消息框,说明单击“是”,并且“否”按钮也相同。你可以在 if 块之后推送你自己的代码。

    button_reply = QMessageBox.question(self,"Test", "Are you sure want to quit??", QMessageBox.Yes,QMessageBox.No,)
    
    if button_reply == QMessageBox.Yes:
        QMessageBox.information(self, "Test", "Yes Button Was Clicked")
    else :
        QMessageBox.information(self, "Test", "No Button Was Clicked")
    
    

    【讨论】:

      【解决方案2】:

      如果你想在 python 中制作它,你需要在你的工作台中检查这个代码。 也这样写。 我们用 python 创建了一个弹出框。

      msgBox = QMessageBox()
      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)
      ret = msgBox.exec_()
      

      【讨论】:

        【解决方案3】:

        我在答案中错过了翻译电话tr

        最简单的解决方案之一,允许以后进行国际化:

        if (QMessageBox::Yes == QMessageBox::question(this,
                                                      tr("title"),
                                                      tr("Message/Question")))
        {
            // do stuff
        }
        

        将代码级字符串放在tr("Your String") 调用中通常是一个良好的Qt 习惯。

        QMessagebox 如上所述适用于任何QWidget 方法)

        编辑:

        您可以在QWidget 上下文之外使用QMesssageBox,请参阅@TobySpeight 的回答。

        如果您甚至不在QObject 上下文中,请将tr 替换为qApp->translate("context", "String") - 您需要#include <QApplication>

        【讨论】:

          【解决方案4】:

          QMessageBox 包含快速提出此类问题的静态方法:

          #include <QApplication>
          #include <QMessageBox>
          
          int main(int argc, char **argv)
          {
              QApplication app{argc, argv};
              while (QMessageBox::question(nullptr,
                                           qApp->translate("my_app", "Test"),
                                           qApp->translate("my_app", "Are you sure you want to quit?"),
                                           QMessageBox::Yes|QMessageBox::No)
                     != QMessageBox::Yes)
                  // ask again
                  ;
          }
          

          如果您的需求比静态方法提供的更复杂,您应该构造一个新的QMessageBox 对象,并调用其exec() 方法以在其自己的事件循环中显示它并获取按下的按钮标识符。例如,我们可能希望将“否”作为默认答案:

          #include <QApplication>
          #include <QMessageBox>
          
          int main(int argc, char **argv)
          {
              QApplication app{argc, argv};
              auto question = new QMessageBox(QMessageBox::Question,
                                              qApp->translate("my_app", "Test"),
                                              qApp->translate("my_app", "Are you sure you want to quit?"),
                                              QMessageBox::Yes|QMessageBox::No,
                                              nullptr);
              question->setDefaultButton(QMessageBox::No);
          
              while (question->exec() != QMessageBox::Yes)
                  // ask again
                  ;
          }
          

          【讨论】:

          • 既然您已经包含了QApplication,我建议您使用qApp-&gt;translate("context", "String"),它会替换tr,以便在QObject 类之外使用
          【解决方案5】:

          您可以使用 QMessage 对象创建一个消息框,然后添加按钮:

          QMessageBox msgBox;
          msgBox.setWindowTitle("title");
          msgBox.setText("Question");
          msgBox.setStandardButtons(QMessageBox::Yes);
          msgBox.addButton(QMessageBox::No);
          msgBox.setDefaultButton(QMessageBox::No);
          if(msgBox.exec() == QMessageBox::Yes){
            // do something
          }else {
            // do something else
          }
          

          【讨论】:

          • 有趣的答案,您将如何为其添加图标?喜欢信息?
          • @Dariusz:您拥有QMessageBox 对象的setIcon 方法部分。如果这些枚举作为参数之一:QMessageBox::NoIconQMessageBox::QuestionQMessageBox::Informationdoc.qt.io/qt-4.8/qmessagebox.html#icon-prop
          【解决方案6】:

          QT 可以像 Windows 一样简单。等效代码是

          if (QMessageBox::Yes == QMessageBox(QMessageBox::Information, "title", "Question", QMessageBox::Yes|QMessageBox::No).exec()) 
          {
          
          }
          

          【讨论】:

            【解决方案7】:

            你可以使用QMessageBox::question

            假设小部件插槽中的示例:

            #include <QApplication>
            #include <QMessageBox>
            #include <QDebug>
            
            // ...
            
            void MyWidget::someSlot() {
              QMessageBox::StandardButton reply;
              reply = QMessageBox::question(this, "Test", "Quit?",
                                            QMessageBox::Yes|QMessageBox::No);
              if (reply == QMessageBox::Yes) {
                qDebug() << "Yes was clicked";
                QApplication::quit();
              } else {
                qDebug() << "Yes was *not* clicked";
              }
            }
            

            应该在 Qt 4 和 5 上工作,在 Qt 5 上需要 QT += widgets,在 Win32 上需要 CONFIG += console 才能看到 qDebug() 输出。

            查看StandardButton 枚举以获取您可以使用的按钮列表;该函数返回被点击的按钮。您可以使用额外的参数设置默认按钮(如果您不这样做,Qt "自动选择合适的默认值" 或指定QMessageBox::NoButton)。

            【讨论】:

            • 我有一个关于您动态生成消息框的方式的问题:是这样做更好还是预先定义整个事情(创建消息框并将其存储在变量中等)然后只需在需要时调用它?
            • @rbaleksandar 最好使用 QMessageBox 静态方法。当方法返回时,Qt 会清理所有使用的内存,无需在内存中永久保留。
            • 谢谢,有道理。毕竟这部分 UI 不是 1) 需要大量资源因此需要一些时间来加载和 2) 经常甚至经常在屏幕上供用户查看的东西。
            • 最佳答案。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多