Results 1 to 3 of 3

Thread: How to abort the application when the connection to database fails

  1. #1
    Join Date
    Jan 2009
    Posts
    78
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default How to abort the application when the connection to database fails

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to abort the application when the connection to database fails

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Angry Re: How to abort the application when the connection to database fails

    I haven't tried this in anger but the C++ GUI Programming with Qt 4, Second Edition uses his trick in the constructor:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ...
    4. QTimer::singleShot(0, this, SLOT(openConnection()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    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().

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  3. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  4. Replies: 3
    Last Post: 2nd August 2007, 21:28
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.