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] Do I need to run Qt even loop (QApplication.exec()) to display QMessage::critical()?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Do I need to run Qt even loop (QApplication.exec()) to display QMessage::critical()?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 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.
  • Z Offline
    Z Offline
    Zingam
    wrote on last edited by
    #1

    While this code seems to work. Is it correct?
    I thought that no Qt functinality would work if I didn't use a.exec().

    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    try {
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    catch (ExceptionInitialization& exception) {
        qDebug() << exception.getMessage();
        QMessageBox::critical(nullptr,
                              QObject::tr("ERROR"),
                              exception.getMessage());
    
        return (-1);
    }
    

    }@

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Not required. It is dialog. Instead of passing nullptr, try passing 0. What is the issue you are facing ? It it not shown ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        [quote author="Zingam" date="1407675034"]While this code seems to work. Is it correct?
        I thought that no Qt functinality would work if I didn't use a.exec().[/quote]Yes, it's fine.

        exec() starts and event loop. Without it, you can't handle events and signals, but other things in Qt still work. (For example, even if I don't call exec() I can still use QFile to read/write files)

        Note that there are many ways to start an event loop:

        • QCoreApplication::exec()
        • QThread::exec()
        • QEventLoop::exec()
        • QDialog::exec()

        What Dheerendra meant is that QMessage::critical() calls QDialog::exec() internally, so you do get a running event loop until you close the dialog.

        [quote author="Dheerendra" date="1407677087"]Instead of passing nullptr, try passing 0.[/quote]Why? nullptr was invented specifically to avoid the ambiguity caused by passing 0.

        We should encourage each other to use C++11.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          @
          #include <QtGui/QApplication>
          #include <QtGui/QMessageBox>
          #include <exception>
          using namespace std;

          #if __cplusplus<201103L // naive C++11 support test
          #define nullptr 0
          #endif

          class MyException: public exception {
          virtual const char *what() const throw(){
          return "My Exception Happened";
          }
          } myException;

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);

          try { throw myException; }
          catch(exception &e) {
              QMessageBox::critical(nullptr, "Exception!", e.what());
          }
          

          }
          @

          Works perfectly for me (Qt4.8) ;o)

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zingam
            wrote on last edited by
            #5

            Thank you!
            I wasn't sure if the MessageBox would work without issues. I do a great deal of initialization in MainWindow like copying files, socket initialization, xml parsing, etc. And if some of this fails I need to warn the user and exit.

            So I assume my doubts are solved.

            Note: It's a C++11 app.

            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