Results 1 to 5 of 5

Thread: Taking important tests before executing the Event Loop.

  1. #1
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Taking important tests before executing the Event Loop.

    Hi,

    I am currently making an application that will residue in a system tray and show a message per hour. Before all these can work, I will need to perform some tests to make sure there are required files. The application needs to be terminated immediately if something is missing. First, I simply tried to use QApplication::quit() or qApp->quit(). However, it cannot work if Event Loop is executed in a program via return app.exec()--this is usually the last command in my programs. I've thought that I might not always need to execute the Event Loop and the application can be terminated at once. Though, I don't know if this is a correct approach and would be grateful if someone could provide a better way of solving this problem.
    Thanks

    Below is this the test program.
    Qt Code:
    1. #include <QApplication>
    2. #include <QFile>
    3. #include <QMessageBox>
    4. #include <QIcon>
    5. //#include "stworz_plik/rekord.h"
    6. //#include "stworz_plik/errors.h"
    7.  
    8.  
    9. namespace Wydarzenia {
    10. enum code {NOERROR, ERRORFILE};
    11. const QString CFILENAME = "database";
    12. }
    13.  
    14. bool testFile(const QString& fileName, Wydarzenia::code& locStatus) {
    15. using namespace Wydarzenia;
    16. QFile dbFile(fileName);
    17. if(dbFile.open(QIODevice::ReadOnly)) {
    18. locStatus = NOERROR;
    19. return true;
    20. } else {
    21. QMessageBox msgBox;
    22. msgBox.setIcon(QMessageBox::Critical);
    23. msgBox.setText(QObject::trUtf8("Cannot find the file %L1").arg(fileName));
    24. msgBox.setInformativeText(QObject::trUtf8("The file %L1 is required and must be in the same folder as the application").arg(fileName));
    25. msgBox.setStandardButtons(QMessageBox::Close);
    26. msgBox.setDefaultButton(QMessageBox::Close);
    27. msgBox.exec();
    28. locStatus = ERRORFILE;
    29. return false;
    30. }
    31. }
    32.  
    33. int main(int argc, char* argv[]) {
    34. using namespace Wydarzenia;
    35. QApplication app(argc,argv);
    36.  
    37. code status;
    38. if(testFile(CFILENAME,status)) {
    39. // Main program where I am going to create:
    40. // * a Window object
    41. // * a QSystemTrayIcon
    42. // ...
    43. return app.exec();
    44. } else
    45. return EXIT_FAILURE;
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Taking important tests before executing the Event Loop.

    Quote Originally Posted by ZikO View Post
    First, I simply tried to use QApplication::quit() or qApp->quit(). However, it cannot work if Event Loop is executed in a program via return app.exec()
    I think you have this mixed up.
    QApplication::quit() or QApplication::exit() do not work when there is no event loop.
    Make sure that you use these functions only when the event loop is actually running. This might not be the case in your situation.
    Tip: do not check for files in the application or mainwindow constructor and call quit or exit inside that constructor. Postpone the check till everything is initialised and the event loop is there.

  3. #3
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Taking important tests before executing the Event Loop.

    Quote Originally Posted by tbscope View Post
    I think you have this mixed up.
    QApplication::quit() or QApplication::exit() do not work when there is no event loop.
    Make sure that you use these functions only when the event loop is actually running. This might not be the case in your situation.
    To be honst, this is what I am trying to avoid. I forked the application into two paths. If there is a critical error, the program does not even start the event loop, neither does it call QApplication::quit() nor QApplication::exit().
    Quote Originally Posted by tbscope View Post
    Tip: do not check for files in the application or mainwindow constructor and call quit or exit inside that constructor. Postpone the check till everything is initialised and the event loop is there.
    I understood what you said but from my understanding, if I don't check for files in the main constructor, once everything is initialised, the event loop will start working and application will be waiting for events--either triggered by user or some kind of timer. It means that the error will appear later when the user starts doing something. I thought I could avoid that. Is it possible?

  4. #4
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Taking important tests before executing the Event Loop.

    The only way I found that helped me solve this problem quickly enough was a QTimer object. I have connected its SIGNAL timeout() to my SLOT testFile(). Still within the main constructor, I have initialized a QTimer object and run its function QTimer::start(0). I believe the signal is fired right after the event loop has started; that is, in the line: return app.exec(). If this is not a case, then I don't know how I can do it in any other way.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Taking important tests before executing the Event Loop.

    The function testFile() is being called when the event loop is running. The time out of the timer is in fact an event.
    Edit: Calling application quit or exit should work from within the testFile() function.
    Last edited by tbscope; 1st September 2012 at 20:29.

  6. The following user says thank you to tbscope for this useful post:

    ZikO (1st September 2012)

Similar Threads

  1. Not able to receive the custom event from the event loop
    By Jagadeeshakr in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2012, 11:47
  2. Qt/MFC Event Loop - stop MFC receiving event?
    By Kaylx in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2012, 18:57
  3. Replies: 4
    Last Post: 6th August 2011, 01:40
  4. Replies: 10
    Last Post: 15th January 2010, 14:35
  5. Replies: 0
    Last Post: 23rd October 2008, 12:43

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.