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. QMessageBox after qApp->exec();

QMessageBox after qApp->exec();

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.4k Views
  • 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 Offline
    M Offline
    MrMNight
    wrote on last edited by
    #1

    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
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          MrMNight
          wrote on last edited by
          #4

          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
          0
          • M Offline
            M Offline
            MrMNight
            wrote on last edited by
            #5

            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
            0
            • M Offline
              M Offline
              MrMNight
              wrote on last edited by
              #6

              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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                0

                • Login

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