Yes/No message box using QMessageBox

前端 未结 7 2085
渐次进展
渐次进展 2021-01-30 05:59

How do I show a message box with Yes/No buttons in Qt, and how do I check which of them was pressed?

I.e. a message box that looks like this:

7条回答
  •  孤独总比滥情好
    2021-01-30 06:33

    I'm missing the translation call tr in the answers.

    One of the simplest solutions, which allows for later internationalization:

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

    It is generally a good Qt habit to put code-level Strings within a tr("Your String") call.

    (QMessagebox as above works within any QWidget method)

    EDIT:

    you can use QMesssageBox outside a QWidget context, see @TobySpeight's answer.

    If you're even outside a QObject context, replace tr with qApp->translate("context", "String") - you'll need to #include

提交回复
热议问题