Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt windows crashing when inside if-body
Forum Update on Monday, May 27th 2025

Qt windows crashing when inside if-body

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 605 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Ceriana
    wrote on 31 May 2016, 13:37 last edited by
    #1

    Hey,

    I'm a beginner to Qt5.6 and I have some trouble. I'm working on this code:

    #include "mainwindow.h"
    #include "logindialog.h"
    #include <QApplication>
    #include <QtSql>
    #include <QMessageBox>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    
        db.setHostName("localhost");
        db.setUserName("user");
        db.setPassword("pass");
        db.setDatabaseName("db");
        bool ok = db.open();
    
        if(ok) 
        {
            // only show login dialog if the connection is valid and established
            LoginDialog login;
            login.show();
            
            // showing this window will depend on a successful login, login check not implemented yet
            MainWindow w;
            w.show();
    
        }
        else
        {
            // shows error message if the connection couldn't get established
            
            QMessageBox* msg = new QMessageBox(nullptr);
            msg->setIcon(QMessageBox::Critical);
            msg->setWindowTitle("Error");
            msg->setText("Error:\n\n"+db.lastError().text());
    
            msg->show();
        }
    
        return a.exec();
    }
    

    As you can see, I want to check if the connection is valid before showing the LoginDialog-formular (or even the MainWindow). Before I added this if-Statement, both windows worked just fine. But after I added the check the program crashes immediately, without any error messages (just the crash note in Creator). If the connection parameters are wrong, it shows the QMessageBox, just as intended. But as soon as the program enters the if-true-body, the program shuts down.

    What did I miss? I'm thankful for any help.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 31 May 2016, 13:39 last edited by JohanSolo
      #2

      You're declaring your window inside the if-true body, and its scope is limited to this body... You should declare a pointer on a MainWindow and allocate it using new.

      Edit: this works similar with your LoginDialog instance.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jun 2016, 05:28 last edited by
        #3

        First: you should call

        login.exec();
        instead of
        login.show();
        

        else the MainWindow will always be visible, even before login is finished.

        Second: you should declare login and w before if block. You can still declare login inside if block if you use exec() (because exec() blocks until the dialog is closed).

        Your app crashes because login and w are destroyed after if block is executed. Actually it does not crash - your dialog and main window are destroyed after if block and thus not shown.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        31 May 2016, 13:37

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved