Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QMessageBox after qApp->exec();

    General and Desktop
    4
    7
    2177
    Loading More Posts
    • 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.
    • M
      MrMNight last edited by

      Hellop, i have segmantation fault error. Can i do in Qt something like this:
      main.cpp
      @
      int main(int argc, char** argv)
      {
      QApplication app(argc, argv);

      MainWindow* mainWindow = new MainWindow;
      //some code
      int rc = app.exec();
      
      delete mainWindow;
      
      if(rc==2)
      {
          QMessageBox::critical(NULL, "CriticalError", "something wrong", QMessageBox::Ok | QMessageBox::Default, QMessageBox::Escape);
      }
      if(rc==3)
      {
          QMessageBox::critical(NULL, "CriticalError", "something else wrong", QMessageBox::Ok | QMessageBox::Default, QMessageBox::Escape);
      }
      return rc;
      

      }@

      I want to call qApp->exit(error_code) in my classes, and then all my classes have to destruct and only message box appear and show until user didn't close it. Sorry for my English...

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        no thats not possible. You need to keep the QApplication object alive...

        A possible solution for your problem would be to implement a custom method which displays the message box and calls qApp->exit() and maybe set a member that you've closed the app. So you call this method instead of qApp->exit() directly.
        Additionally you override closeEvent() handler of your mainwindow and also display a messagebox in there (depending on the member is set or not mentioned before).

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • M
          mcosta last edited by

          HI,

          this code works fine

          @
          #include "Widget.h"
          #include <QApplication>
          #include <QMessageBox>

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          Widget *w = new Widget;
          w->show();

          int result = a.exec();

          delete w;
          w = 0;

          if (0 == result) {
          QMessageBox::information(0, QApplication::translate("Main", "Bye"),
          QApplication::translate("Main", "Normal Execution"));
          }
          else {
          QMessageBox::critical(0, QApplication::translate("Main", "Bye"),
          QApplication::translate("Main", "Wrong Execution %1").arg(result));
          }

          return result;
          }
          @

          in Widget i call qApp->exit() with different parameters and all works (no crash). Probably there are problem in MainWindow code.

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply Reply Quote 0
          • M
            MrMNight last edited by

            thaks, my problem solved by adding EventLoop (:public QEventLoop) inside main loop, outside Eventloop i doing mainWindow->deleteLater() and checking EventLoop exec code
            [quote author="raven-worx" date="1372848306"]no thats not possible. You need to keep the QApplication object alive...

            A possible solution for your problem would be to implement a custom method which displays the message box and calls qApp->exit() and maybe set a member that you've closed the app. So you call this method instead of qApp->exit() directly.
            Additionally you override closeEvent() handler of your mainwindow and also display a messagebox in there (depending on the member is set or not mentioned before).[/quote]

            1 Reply Last reply Reply Quote 0
            • M
              MrMNight last edited by

              you right, your code work fine but i still cant find error in my code...
              [quote author="mcosta" date="1372855531"]HI,

              this code works fine

              @
              #include "Widget.h"
              #include <QApplication>
              #include <QMessageBox>

              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              Widget *w = new Widget;
              w->show();

              int result = a.exec();

              delete w;
              w = 0;

              if (0 == result) {
              QMessageBox::information(0, QApplication::translate("Main", "Bye"),
              QApplication::translate("Main", "Normal Execution"));
              }
              else {
              QMessageBox::critical(0, QApplication::translate("Main", "Bye"),
              QApplication::translate("Main", "Wrong Execution %1").arg(result));
              }

              return result;
              }
              @

              in Widget i call qApp->exit() with different parameters and all works (no crash). Probably there are problem in MainWindow code.
              [/quote]

              1 Reply Last reply Reply Quote 0
              • M
                MrMNight last edited by

                i handled many objects which parent is mainWindow inside main loop...so i fix @delete mainWindow@ to @mainWindow->deleteLater()@ and it works fine for me without additional loop

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Hi,

                  What do you mean by "handle many objects which parent is mainWindow" ? Are you deleting them yourself ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post