Results 1 to 4 of 4

Thread: MessageBox comes before MainWindow

  1. #1
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default MessageBox comes before MainWindow

    Hi all, I just started qt, so I don't have enough knowledge.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. connectDatabase();
    8. }
    9.  
    10. void MainWindow::connectDatabase()
    11. {
    12. QFile databaseFile("database.db");
    13. if (!databaseFile.exists()) {
    14. connectionError();
    15. } else
    16. {
    17. db.setHostName("localhost");
    18. db.setDatabaseName("database.db");
    19. if (!db.open()) {
    20. connectionError();
    21. }
    22. }
    23. }
    24.  
    25. void MainWindow::connectionError()
    26. {
    27. QMessageBox msgBox;
    28. msgBox.addButton(QMessageBox::Yes);
    29. msgBox.addButton(QMessageBox::No);
    30. msgBox.setWindowTitle("Connection Error");
    31. msgBox.setIcon(QMessageBox::Critical);
    32. msgBox.setText("Program cannot connect the database file which is required to run this software. Would you like to download default database file? Please note that all of the saved process data will be removed.");
    33. int selection = msgBox.exec();
    34.  
    35. if (selection == QMessageBox::Yes) {
    36. //restore data
    37. } else {
    38. MainWindow::close();
    39. }
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 


    This is what I did. When I delete the database file to see connectionError function, messagebox comes before the MainWindow and MainWindow::close() doesnt work. What should I do?

  2. #2
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: MessageBox comes before MainWindow

    Instead of direct call connectDatabase(), make it slot, and than emit the signal:
    emit doConnectDatabase();

  3. #3
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: MessageBox comes before MainWindow

    Thanks for the reply,

    I made it public slot and also changed its name to "doConnectDatabase"
    and then, I typed
    Qt Code:
    1. emit doConnectDatabase()
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. connectDatabase()
    To copy to clipboard, switch view to plain text mode 

    but nothing changed, any other ideas?

  4. #4
    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

    Default Re: MessageBox comes before MainWindow

    Your main window doesn't show because it is not even constructed at the time you fail to open the database. Your attempt to close the un-constructed window may schedule a close event but it will almost certainly be followed nearly immediately with the show event scheduled in your main().

    You need to schedule the attempt to open the database for a point in time after the main window is constructed and visible. You can do this with a zero length, single shot timer. Convert you existing connectDatabase() to a slot and make your constructor look like this:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QTimer::singleShot(0, this, SLOT(connectDatabase()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    The window will finish construction, return to the main event loop and be shown before the timer fires. When the timer event is processed the connection error should function as you expect.

Similar Threads

  1. Extending MessageBox
    By thru in forum Qt Programming
    Replies: 7
    Last Post: 17th December 2010, 16:31
  2. MessageBox appearnig twice.
    By ronak.vashi in forum Newbie
    Replies: 7
    Last Post: 30th November 2010, 07:45
  3. show messagebox
    By chinmayapadhi in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2010, 13:35
  4. MessageBox Validation
    By fortyhideout12 in forum Newbie
    Replies: 10
    Last Post: 2nd September 2009, 17:14
  5. QSA 1.2.1 && MessageBox
    By xk in forum Newbie
    Replies: 0
    Last Post: 20th April 2006, 17:30

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.