QMessageBox after qApp->exec();



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


  • Moderators

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



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



  • 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]



  • 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]



  • 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


  • Lifetime Qt Champion

    Hi,

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


Log in to reply
 

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