Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] qFatal does not return from QApplication::exec()?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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();
    }
    }@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved