【发布时间】:2018-04-30 11:58:49
【问题描述】:
我有一个 Qt 程序,它使用 QApplication 作为其主窗口,并且还可能产生许多 QMessageBox 小部件。当主 QApplication 窗口退出时,我需要关闭所有这些 QMessageBox 对话框。但是,我无法使用任何正常的回调,因为 QMessageBox 似乎阻止了 onDestruction() 信号。当我单击 X 退出 QApplication 时,它的窗口消失了,但只有在最后一个 QMessageBox 退出时才会触发 onDestruction() 符号。请让我知道执行此操作的正确方法。
编辑:
这是我的 main.cpp:
int main(int argc, char* argv[]) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
Application app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("applicationVersion", VER_FILEVERSION_STR);
engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
int retval = app.exec();
qInstallMessageHandler(0);
return retval;
}
下面是我实例化 QMessageBox 的方法:
QMessageBox* errorD = new QMessageBox();
errorD->setStandardButtons(QMessageBox::Ok);
errorD->setDefaultButton(QMessageBox::Ok);
errorD->setModal(false);
errorD->setWindowTitle(title);
errorD->setText(msg);
// We reset when closed
QObject::connect(errorD, &QMessageBox::destroyed, [=]() { printf("QMBox destroyed.");});
errorD->raise();
errorD->activateWindow();
errorD->show();
QApplication::processEvents();
【问题讨论】:
-
MessageDialog QML Type 还是 QMessageBox?
-
QMessageBox,由 C++ 代码动态创建。
-
好的,提供。
-
什么时候调用QMessageBox?