[Solved] qFatal does not return from QApplication::exec()?



  • I have a custom message handler set in my applications main.cpp. It works just fine for all except qFatal().
    The error message is printed, however the application does not return to the exec() call in main(). I want to return because if there is an error then I am displaying a message to the user.

    @QFile *logFile;
    QTextStream *logStream;
    QMutex *mutex;
    bool errorMsg;
    void myMessageOutput(QtMsgType type, const char msg)
    {
    if(logFile)
    {
    mutex->lock();
    switch (type)
    {
    case QtDebugMsg:
    logStream << msg;
    logStream->flush();
    break;
    case QtWarningMsg:
    logStream << "\n
    Warning **\n";
    logStream << msg;
    logStream << "\n
    Warning Complete *\n";
    logStream->flush();
    errorMsg = true;
    break;
    case QtCriticalMsg:
    logStream << "\n
    Critical **\n";
    logStream << msg;
    logStream << "\n
    Critical Complete *\n";
    logStream->flush();
    errorMsg = true;
    break;
    case QtFatalMsg:
    logStream << "\n
    Fatal **\n";
    logStream << msg;
    logStream << "\n
    Fatal Complete ***\n";
    logStream->flush();
    errorMsg = true;
    QApplication::exit(-123);
    }
    mutex->unlock();
    }
    }

    main()
    {
    QApplication a(argc, argv);
    //....
    int errorCode = a.exec();
    if(errorMsg)//doesnt reach here for qFatal()
    {
    QMessageBox::exec();
    }
    }@


  • Moderators

    It is a bit of a philosophic question of the meaning of fatal. IMHO one should use qCritical for case where some additional information has to be possible. But this is probably more a matter of taste.

    In you case you may add the QMessageBox in the QtFatalMsg section of your handler. This may be a possible workaround.

    However, my current understanding reading the docs for Qt5.2 and Qt4.8 (you are using Qt4 style), it should be possible what you are trying to do. You are using your own message handler. The docs define a stop only for default handler. So, it is either a bug in the docs or in the source IMHO.



  • @koahnig Thank You for pitching in. OK I will use qCritical henceforth. But Qt internally uses qFatal sometimes. So instead of the crash I wanted to fail more elegantly. OK I can display the message from the message handler itself, stupid of me not to realise that. Then instead of using QApplication::exit(), I will use the C++ exit(). In that way it wont seem like a crash.


  • Moderators

    [quote author="CAD_coding" date="1389450369"]I will use the C++ exit(). In that way it wont seem like a crash.[/quote]
    It is probably a matter of view again. If you do not want to show to the user that something went badly wrong, IMHO qCritical is a better choice. qFatal signals to me that there is no hope at all and you show it also to the user.
    Personally, I would use the route with closing quietly only when there a lib involved and you cannot or do not want to change, but it is using qFatal.
    Anyway glad to see that you have a solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.