Results 1 to 14 of 14

Thread: Quit, Exit qApp (program) if error?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    30
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Quit, Exit qApp (program) if error?

    I am trying to figure out how to properly exit and destroy all my classes if say an error happens.

    Trying to make like a close the application if this message box pops up.

    Doing this:
    Qt Code:
    1. qApp->exec();
    2. return;
    To copy to clipboard, switch view to plain text mode 

    Works, but it leaves the Process in Task Manager alive just the window doesn't show up.

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Quit, Exit qApp (program) if error?

    exec() starts the event loop, its called in main normally.

    You probably want qApp->quit();

  3. #3
    Join Date
    Jul 2007
    Posts
    30
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Quit, Exit qApp (program) if error?

    If I use quit(), the error message comes up and then the program starts up anyway. It is suppose to quit the whole program and destroy everything.

    I mean how do people put like a File > Exit ?? What do they connect() to "exit"?

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Quit, Exit qApp (program) if error?

    Your situation sounds to me like a situation I've had before, where you need to do a test in the main window/widget constructor, and if it fails you need to exit the program, but as this is being called before app.exec() in main(), it doesn't work properly. My solution was a hack, but it should work.

    Qt Code:
    1. QTimer::singleShot(250, qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    Probably someone here with more experience should step in and suggest a more correct solution. I would like to know myself.

  5. The following 2 users say thank you to JimDaniel for this useful post:

    akrep55tr (28th May 2011), Arsenic (22nd July 2008)

  6. #5
    Join Date
    Jul 2007
    Posts
    30
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Quit, Exit qApp (program) if error?

    Thanks, it seems to work. I think my main problem was I was trying to use it in my class (QMainWindow):

    I tried
    qApp->quit();

    that didn't do anything.

    I tried
    close(); (because QMainWindow has a close function)
    but apparently, your singleShot timer works awesome, thanks I had forgotten about that.

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Quit, Exit qApp (program) if error?

    Just a thought -...
    Wont it be better if we use some kind of factory for the objects ??
    The factory may return a valid object if the parameters meet the condition, or else it returns null.

  8. #7
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Quit, Exit qApp (program) if error?

    Its been bothering me I suggested so clearly a hackish method of solving the problem. I realized today a better solution. Not sure why it didn't occur to me before:

    Qt Code:
    1. int main(int argc, char * argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QWidget myWidget;
    5.  
    6. bool passed_test = myWidget.testForSomething();
    7. if(passed_test)
    8. {
    9. myWidget.show();
    10. return app.exec();
    11. }
    12. else
    13. {
    14. return 1;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Quit, Exit qApp (program) if error?

    Calling QApplication::quit() indeed doesn't have any effect if one later calls QApplication::exec().
    J-P Nurmi

  10. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Quit, Exit qApp (program) if error?

    Well, I have following chunk of code:
    Qt Code:
    1. {
    2. // sets up database paramaters
    3. m_Database=QSqlDatabase::addDatabase(db_type);
    4. m_Database.setHostName(db_host);
    5. m_Database.setUserName(db_username);
    6. m_Database.setPassword(db_password);
    7. m_Database.setDatabaseName(db_name);
    8. // tries to connect to database
    9. m_bConnectionEstablished=m_Database.open();
    10. if(!m_bConnectionEstablished)
    11. {
    12. //qFatal("Failed to connect to database. Aborting.");
    13. /*
    14.   int ret=QMessageBox::critical(0,
    15.   tr("Critical Error"),
    16.   tr("Failed to connect to database. Aborting."));
    17. */
    18. QString errorString(m_Database.lastError().databaseText());
    19.  
    20. errorString.append(": ");
    21. errorString.append(m_Database.lastError().driverText());
    22. int ret=QMessageBox::critical(0,
    23. tr("Critical Error"),
    24. errorString);
    25.  
    26. qApp->quit(); // terminates application, does not work?!
    27. }
    To copy to clipboard, switch view to plain text mode 

    What I want to achieve, if database connection fails, application must terminate immediately. qApp->quit() does not work, are there any other ideas?
    Qt 5.3 Opensource & Creator 3.1.2

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Quit, Exit qApp (program) if error?

    When does this chunk of code get executed? Before entering to the event loop (ie. before calling QCoreApplication::exec())?
    J-P Nurmi

  12. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Quit, Exit qApp (program) if error?

    Here is code:
    Qt Code:
    1. eROSystem w;
    2. w.showMaximized();
    3. pIntroWindow->finish(&w); // sets splash window closing time
    4. delete pIntroWindow; // deletes pIntroWindow
    5. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    6. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    Database code is executed in following line:
    Qt Code:
    1. eROSystem w;
    To copy to clipboard, switch view to plain text mode 
    . This is the first line of this code chunk.
    Qt 5.3 Opensource & Creator 3.1.2

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Quit, Exit qApp (program) if error?

    So, first in the eROSystem constructor you call QCoreApplication::quit() and then later in main() you call QCoreApplication::exec(). That won't make the application quit. First you tell it to exit the event loop, then you tell it to enter the event loop.
    J-P Nurmi

Similar Threads

  1. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 10:15
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  3. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.