我用Qt框架编写了一个类似于C ++中记事本的文本编辑程序。它只有基本的功能,如撤消,重做等。我想显示一个对话框,如果用户没有保存他的更改并询问他/她是否想要保存类似于记事本和其他的更改,将显示该对话框这样的程序。我该如何处理?
答案 0 :(得分:3)
Qt' documentation for QMessageBox对此进行了讨论并提供了以下示例: -
QMessageBox msgBox;
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);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
// Save was clicked
break;
case QMessageBox::Discard:
// Don't Save was clicked
break;
case QMessageBox::Cancel:
// Cancel was clicked
break;
default:
// should never be reached
break;
}
应用程序可以通过aboutToQuit信号监控他们何时退出,这是您提示用户的信号。
例如(with C++ 11)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MainWindow w;
// Note Qt 5 connect syntax with C++ 11 lambda function
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&w](){
qDebug() << "Terminating - Goodbye!\n";
//Display msg prompt here
if(QMessageBox::question(NULL, "Test", "Save?", QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
{
// if result is save...
w.save();
}
});
w.show();
return a.exec();
}
答案 1 :(得分:1)
你可以这样做:
System.out.println("\tCreatd new node as:" + newNode);