code

QMessageBox를 사용하는 예 / 아니오 메시지 상자

codestyles 2020. 8. 20. 18:46
반응형

QMessageBox를 사용하는 예 / 아니오 메시지 상자


Qt에서 예 / 아니오 버튼이있는 메시지 상자를 표시하려면 어떻게하나요? 그리고 어떤 버튼을 눌렀는지 어떻게 확인하나요?

즉, 다음과 같은 메시지 상자입니다.

여기에 이미지 설명 입력


당신은 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 += widgets, Qt 5 및 CONFIG += consoleWin32에서 qDebug()출력 을 확인해야 합니다.

StandardButton사용할 수있는 버튼 목록은 열거 형을 참조하십시오 . 이 함수는 클릭 한 버튼을 반환합니다. 추가 인수를 사용하여 기본 버튼을 설정할 수 있습니다 (을 지정하지 않거나 지정하지 않으면 Qt " 자동으로 적합한 기본값을 선택합니다 " QMessageBox::NoButton).


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
}

QT는 Windows만큼 간단 할 수 있습니다. 동등한 코드는

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

}

tr답변에 번역 전화 없습니다 .

나중에 국제화 할 수있는 가장 간단한 솔루션 중 하나 :

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

일반적으로 호출 Qt내에 코드 레벨 문자열을 넣는 것은 좋은 습관 tr("Your String")입니다.

( QMessagebox위와 같이 모든 QWidget방법 내에서 작동 )

편집하다:

컨텍스트 QMesssageBox외부에서 사용할 수 있습니다 QWidget. @TobySpeight의 답변을 참조하십시오.

당신이 밖에서도 경우 QObject상황에 맞는 교체 tr와 함께 qApp->translate("context", "String")- 당신이 필요합니다#include <QApplication>


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
        ;
}

파이썬으로 만들고 싶다면 워크 벤치에서이 코드를 확인해야합니다. 또한 이렇게 쓰십시오. 파이썬으로 팝업 상자를 만들었습니다.

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_()

참고 URL : https://stackoverflow.com/questions/13111669/yes-no-message-box-using-qmessagebox

반응형