PDA

View Full Version : How to abort the application when the connection to database fails



gt.beta2
12th April 2009, 17:12
Hi!
In an aplication based on QMainWindow i call createConnection() function in the constructor wish i included from "connection.h".
How/where can i abort the application if the connection fails?

Thanks

marcel
12th April 2009, 18:51
Better move the createConnection away from the constructor to an initialization function, and return an error code or throw an exception if something goes wrong.

ChrisW67
17th April 2009, 23:36
I haven't tried this in anger but the C++ GUI Programming with Qt 4, Second Edition uses his trick in the constructor:

MainWindow::MainWindow()
{
...
QTimer::singleShot(0, this, SLOT(openConnection()));
}

to move a potential long-running init process out of the constructor and allow the GUI to draw so the user doesn't think the program has hung. The zero time out causes the QTimer to fire when the event loop is idle. The openConnection() can then take quite a while if needed, or in your case of a failure, pop-up an error message before calling close().