// ============================================================================ // message_box_custom — Qt 桌面示例 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 原文片段:[58954535:11] // 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication // 外壳、include、修正笔误)。详见 docs/ERRATA.md。 // 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh (自动退出) // ============================================================================ #include #include #include #include #include class DemoWidget : public QWidget { Q_OBJECT public: DemoWidget(){ doDemo(); } private slots: void doDemo(){ QMessageBox msgBox; msgBox.setText(tr("The document has been modified.")); msgBox.setInformativeText(tr("Do you want to save your changes?")); msgBox.setDetailedText(tr("Differences here...")); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Save); int ret = msgBox.exec(); switch (ret) { case QMessageBox::Save: qDebug() << "Save document!"; break; case QMessageBox::Discard: qDebug() << "Discard changes!"; break; case QMessageBox::Cancel: qDebug() << "Close document!"; break; } } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); DemoWidget w; w.show(); // 仅在离屏自动化验证时(QT_QPA_PLATFORM=offscreen)200ms 后自动退出; // 正常运行(有显示环境)窗口保持打开,不自动退出。 if (qgetenv("QT_QPA_PLATFORM") == "offscreen") { QTimer::singleShot(200, &app, &QApplication::quit); } return app.exec(); } #include "message_box_custom.moc"