QSqlDatabase Strange Behavior



  • Hi everyone,
    I am trying mysql database connection with QSqlDatabase and it's behaving really strange. When I have push button, or tool button in the GUI, everything works fine. But when I add line edit, or text edit, program hangs for about 4 seconds on exit.
    This strange behavior is only on Windows, when I run the same code in Linux, everything works fine.
    Only thing the program does is that it creates connection to database in constructor and then closes it in destructor.

    Here is the code:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    db = QSqlDatabase::addDatabase("QMYSQL", "connection");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("db");
    db.setUserName("user");
    db.setPassword("password");
    connected = db.open();
    }

    MainWindow::~MainWindow()
    {
    db.close();
    db = QSqlDatabase();
    QSqlDatabase::removeDatabase("connection");
    delete ui;
    }
    @

    If I put this code in "on_pushButton_clicked()" function, program exits correctly. But is it okay to create and remove connection to database in every function of the program, when I want to create more complex database solution?

    Another strange thing is that I had to add "db = QSqlDatabase();" before I call removeDatabase, because it showed warning about connection being used when program exited.

    Is someone here who can explain it to me, please? :)



  • Sorry but I didn't understand what do you mean with:

    [quote author="macav" date="1297270562"]
    If I put this code in "on_pushButton_clicked()" function, program exits correctly[/quote]

    What code did you put in on_pushButton_clicked ?



  • what I meant was, that when I do this:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    db = QSqlDatabase::addDatabase("QMYSQL", "casist");
    db.setDatabaseName("casist");
    db.setHostName("127.0.0.1");
    db.setUserName("casist");
    db.setPassword("casist");
    db.open();
    QSqlQuery q("SELECT username FROM users", db);
    while (q.next())
    qDebug() << q.value(0).toString();
    db.close();
    db = QSqlDatabase();
    QSqlDatabase::removeDatabase("casist");
    }
    @

    then the program exits immediately, but when I create the connection in constructor, the program hangs few seconds on exit. Point is, it hangs only when I have line edit in GUI. When I delete it, the program exits immediately.



  • Sorry I can't help you but I suggest you this:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    db = new QSqlDatabase();
    *db = QSqlDatabase::addDatabase("QMYSQL", "casist");
    db->setDatabaseName("casist");
    db->setHostName("127.0.0.1");
    db->setUserName("casist");
    db->setPassword("casist");
    db->open();
    }

    MainWindow::~MainWindow()
    {
    db->close();
    delete db;
    QSqlDatabase::removeDatabase("casist");
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    ...
    QSqlQuery q("SELECT username FROM users", *db);
    while (q.next())
    qDebug() << q.value(0).toString();
    ...
    }
    @

    This is what I usually do.



  • Okay, thank you. I had to ask, I was going crazy because of that, it doesn't make any sense why adding simple line edit causes program to hang few seconds on exit. I guess I can only hope it will be fixed in next release of framework.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.