Yanıtlar:
Bunun QMessageBox::questioniçin kullanırdın.
Varsayımsal bir widget'ın yuvasındaki örnek:
#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 ve 5 üzerinde çalışmalı, çıktıyı görmek QT += widgetsiçin Qt 5 ve CONFIG += consoleWin32 üzerinde gereklidir qDebug().
StandardButtonKullanabileceğiniz düğmelerin listesini almak için numaralandırmaya bakın ; işlev tıklanan düğmeyi döndürür. Ekstra bağımsız değişken içeren varsayılan bir düğme ayarlayabilirsiniz ( siz yapmazsanız veya belirtirseniz Qt " uygun bir varsayılanı otomatik olarak seçer " QMessageBox::NoButton).
Bir Mesaj Kutusu oluşturmak için QMessage nesnesini kullanabilir ve ardından düğmeler ekleyebilirsiniz:
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
}
setIconyöntem kısmına sahipsiniz QMessageBox. parametreler olarak bu numaralandırmalar varsa biriyle: QMessageBox::NoIcon QMessageBox::Question QMessageBox::Information doc.qt.io/qt-4.8/qmessagebox.html#icon-prop
trCevaplarda çeviri çağrısını kaçırıyorum .
Daha sonra uluslararasılaşmaya izin veren en basit çözümlerden biri:
if (QMessageBox::Yes == QMessageBox::question(this,
tr("title"),
tr("Message/Question")))
{
// do stuff
}
QtBir tr("Your String")çağrıya kod düzeyinde Dizeler koymak genellikle iyi bir alışkanlıktır .
( QMessageboxyukarıdaki gibi herhangi bir QWidgetyöntemle çalışır )
DÜZENLE:
QMesssageBoxbir QWidgetbağlamın dışında kullanabilirsiniz , bkz. @ TobySpeight'ın cevabı.
Eğer bir dış hatta iseniz QObjectbağlamda, yerini trile qApp->translate("context", "String")- yapmanız gerekir#include <QApplication>
QMessageBox bu tür soruları hızla sormak için statik yöntemler içerir:
#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
;
}
İhtiyaçlarınız statik yöntemler tarafından sağlanandan daha karmaşıksa, yeni bir QMessageBoxnesne exec()oluşturmalı ve kendi olay döngüsünde göstermek için yöntemini çağırmalı ve basılan düğme tanımlayıcısını almalısınız. Örneğin, varsayılan yanıtın "Hayır" olmasını isteyebiliriz:
#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öneririmqApp->translate("context", "String")trQObject
Python'da yapmak istiyorsanız, çalışma tezgahınızda bu kodu kontrol etmeniz gerekir. ayrıca böyle yaz. python ile bir açılır kutu oluşturduk.
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_()