c++ - 使用 Visual Studio/Qt 中止执行

标签 c++ visual-studio-2010 qt

如果出现问题,我想中止应用程序执行(使用 Qt 完成)并显示错误消息。

与中止();它不适合我。

你有什么建议吗?

最佳答案

最简单的方法是使用非零结果代码退出应用程序的事件循环,然后显示一个消息框。您可以手动重新旋转事件循环,也可以让消息框的静态方法为您完成。

#include <QPushButton>
#include <QMessageBox>
#include <QApplication>

QString globalMessage;

class Failer : public QObject {
    Q_OBJECT
public:
    Q_SLOT void failure() {
        globalMessage = "Houston, we have got a problem.";
        qApp->exit(1);
    }
};

int main(int argc, char ** argv) {
    QApplication app(argc, argv);
    QPushButton pb("Fail Me");
    Failer failer;
    failer.connect(&pb, SIGNAL(clicked()), SLOT(failure()));
    pb.show();
    int rc = app.exec();
    if (rc) {
        QMessageBox::critical(NULL, "A problem has occurred...", 
                              globalMessage, QMessageBox::Ok);
    }
    return rc;
}

#include "main.moc"

关于c++ - 使用 Visual Studio/Qt 中止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19426710/

相关文章:

c++ - 使用索引将 BST 值存储到数组中

c++ - 从 VS2008 移植到 VS2013 时绑定(bind)错误 C2668

python - 如何将 QWidget 与 QItemDelegate 和 QTableView 一起使用

qt - 我应该使用 KeyPressEvent 还是 QAction 来实现按键?

c++ - win32 C++ 打印字符串到打印机

c++ - 将数组的某些元素转移/分离到其他数组

c++ - 构造函数内部的 Printf 未显示在控制台中

c# - Visual Studio 使用 Silverlight 进行慢速编译

visual-studio-2010 - 如何让 Visual Studio 显示代码覆盖率为 0% 的 dll 的代码覆盖率结果?

visual-studio-2010 - 我应该抑制 CA2204 : Literals should be spelled correctly?