Qt에 예/아니요 단추가있는 메시지 상자를 표시하려면 어떻게하고 어떤 메시지가 눌려 졌는지 확인하려면 어떻게합니까?QMessageBox를 사용하는 메시지 상자 예/아니오
e.e. 다음과 같습니다 메시지 상자 :
Qt에 예/아니요 단추가있는 메시지 상자를 표시하려면 어떻게하고 어떤 메시지가 눌려 졌는지 확인하려면 어떻게합니까?QMessageBox를 사용하는 메시지 상자 예/아니오
e.e. 다음과 같습니다 메시지 상자 :
. 가상 위젯의 슬롯에
예 :
#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에서 작동해야는 qDebug()
출력을 볼 수 Qt는 5 QT += widgets
, 및 Win32에 CONFIG += console
이 필요합니다.
사용할 수있는 버튼 목록을 보려면 StandardButton
enum을 참조하십시오. 이 함수는 클릭 된 버튼을 반환합니다. QMessageBox::NoButton
을 지정하지 않거나 지정하지 않으면 Qt "이 자동으로 적합한 기본값을 선택합니다."을 선택하여 기본 단추를 설정할 수 있습니다.
당신은 버튼을 누른 다음 메시지 상자를 만들 추가 QMessage 객체를 사용할 수 있습니다 : 당신은 그것에 대해 QMessageBox::question
을 사용
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
}
일반적으로 tr("Your String")
호출 내에서 코드 수준의 문자열을 넣을 수있는 좋은 Qt
습관이다.
당신은 QWidget
컨텍스트 외부 QMesssageBox
를 사용 TobySpeight의 대답 @ 볼 수 있습니다
은 EDIT (QMessagebox
은 위의 모든 QWidget
방법 내에서 작동). 당신이 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
;
}
요구 사항이있는 경우를 정적 메서드가 제공하는 것보다 더 복잡한 경우에는 메서드를 호출하여 자체 이벤트 루프에 표시하고 눌려진 단추 식별자를 가져 오는 방법을 호출해야합니다. 예를 들어, 기본 답변으로 '아니요'를 사용하고자 할 수 있습니다.
#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'을 이미 포함 했으므로'QObject' 클래스 밖에서 사용하기 위해'tr'을 대신하는'qApp-> translate ("context", "String")'을 사용할 것을 제안합니다 – DomTomCat
나는 가지고있다. 메시지 상자를 동적으로 생성하는 방법에 관한 질문 : 이렇게하거나 (메시지 상자를 변수에 만들고 저장하는 등) 모든 것을 미리 정의한 다음 필요시에만 호출하는 것이 좋습니다. – rbaleksandar
@rbaleksandar QMessageBox 정적 메서드를 사용하는 것이 좋습니다. Qt는 메서드가 반환 될 때 사용 된 메모리를 정리합니다. 메모리에 영구적으로 저장할 필요가 없습니다. – JoshL
감사합니다. 결국 UI의이 부분은 1) 많은 리소스가 필요하므로로드하는 데 약간의 시간이 걸리며 2) 사용자가 볼 수 있도록 화면에 자주 또는 심지어 계속해서 표시됩니다. – rbaleksandar