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. QSqlDatabase Strange Behavior

QSqlDatabase Strange Behavior

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.6k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    macav
    wrote on last edited by
    #1

    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? :)

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on last edited by
      #2

      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 ?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        macav
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            macav
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

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