PDA

View Full Version : Taking important tests before executing the Event Loop.



ZikO
1st September 2012, 14:40
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.


#include <QApplication>
#include <QFile>
#include <QMessageBox>
#include <QIcon>
//#include "stworz_plik/rekord.h"
//#include "stworz_plik/errors.h"


namespace Wydarzenia {
enum code {NOERROR, ERRORFILE};
const QString CFILENAME = "database";
}

bool testFile(const QString& fileName, Wydarzenia::code& locStatus) {
using namespace Wydarzenia;
QFile dbFile(fileName);
if(dbFile.open(QIODevice::ReadOnly)) {
locStatus = NOERROR;
return true;
} else {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(QObject::trUtf8("Cannot find the file %L1").arg(fileName));
msgBox.setInformativeText(QObject::trUtf8("The file %L1 is required and must be in the same folder as the application").arg(fileName));
msgBox.setStandardButtons(QMessageBox::Close);
msgBox.setDefaultButton(QMessageBox::Close);
msgBox.exec();
locStatus = ERRORFILE;
return false;
}
}

int main(int argc, char* argv[]) {
using namespace Wydarzenia;
QApplication app(argc,argv);

code status;
if(testFile(CFILENAME,status)) {
// Main program where I am going to create:
// * a Window object
// * a QSystemTrayIcon
// ...
return app.exec();
} else
return EXIT_FAILURE;
}

tbscope
1st September 2012, 14:47
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.

ZikO
1st September 2012, 17:25
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().

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?

ZikO
1st September 2012, 19:38
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.

tbscope
1st September 2012, 20:17
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.