Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved QSqlTableModel() empty on Ubuntu but works on Windows ?

    General and Desktop
    qsqltablemodel qsqldatabase postgresql
    4
    41
    2478
    Loading More Posts
    • 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.
    • R
      R-P-H last edited by

      Hi, I am trying to populate a QSqlTableModel() using a table in my Postgresql database and display it in a QTableView. Here is the first part of the code:

      QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
      db.setHostName("localhost");
      db.setPort(5432);
      db.setDatabaseName("test_db");
      db.setUserName("test_user");
      db.setPassword("test_pass");
      bool ok = db.open();
      QSqlTableModel *model = new QSqlTableModel(nullptr, db);
      if (ok){
          model->setTable("my_table_name_here");
          model->select();
      }
      

      On Windows, this works perfectly and QTableView is populated.

      However, when testing on Ubuntu 16.04 LTS the QTableView is empty. I checked the output of model->rowCount(); and it returns '0'.

      A few things to note:

      • Both Operating Systems are accessing the same database using the same credentials.
      • On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.
      • There are no errors in opening the database or anything else related.

      This really has me stumped. Why is it not working on Ubuntu ? Thanks.

      V 1 Reply Last reply Reply Quote 0
      • R
        R-P-H last edited by

        Upgrading to Qt Version 5.15+ solved the issue.

        1 Reply Last reply Reply Quote 1
        • V
          VRonin last edited by

          check the return value of model->select(); and if it returns false use model->lastError() to diagnose what's going wrong

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          R 1 Reply Last reply Reply Quote 2
          • R
            R-P-H @VRonin last edited by

            @VRonin said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

            check the return value of model->select(); and if it returns false use model->lastError() to diagnose what's going wrong

            QSqlError("", "Unable to find table logs", "")
            

            It doesn't make sense because the table name is correct and it does exist ?

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

              Might be a silly question but are you sure that your database is properly populated under Linux ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply Reply Quote 0
              • R
                R-P-H @SGaist last edited by R-P-H

                @SGaist said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                Hi,

                Might be a silly question but are you sure that your database is properly populated under Linux ?

                Hi, yes the database is actually hosted on the same machine. I have double checked it with pgadmin.

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  So the server is on a different machine ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply Reply Quote 0
                  • R
                    R-P-H @SGaist last edited by

                    @SGaist Which server ? The Windows machine I tested on is a different machine.

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      I thought about the machine running PostgreSQL.

                      Check the query made by the QSqlTableModel to see if there's anything suspicious.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      R 1 Reply Last reply Reply Quote 1
                      • R
                        R-P-H @SGaist last edited by

                        @SGaist Which query ? How would I check it besides what I've already done.

                        1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          The one returned by the query method.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          R 1 Reply Last reply Reply Quote 1
                          • R
                            R-P-H @SGaist last edited by

                            @SGaist I get the error: QSqlError("", "Driver not loaded", "Driver not loaded").

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              Looks like you are not connected to the database.

                              What version of Qt are you using ?
                              How did you install it ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              R 1 Reply Last reply Reply Quote 1
                              • R
                                R-P-H @SGaist last edited by R-P-H

                                @SGaist
                                Qt Creator 4.11.2
                                Based on Qt 5.14.2 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)

                                I installed using the online-installer for Ubuntu x64 from Qt website.

                                What is strange is that I am able to access the database just fine in the same application using QSqlQuery.

                                1 Reply Last reply Reply Quote 0
                                • SGaist
                                  SGaist Lifetime Qt Champion last edited by

                                  That's the Qt Creator version.

                                  In any case, start your application with the QT_DEBUG_PLUGINS environment variable set to 1, it will give you more information about why the plugin does not load.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  R 1 Reply Last reply Reply Quote 0
                                  • R
                                    R-P-H @SGaist last edited by

                                    @SGaist I don't see any problems in loading sql drivers:

                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers" ...
                                    QFactoryLoader::QFactoryLoader() looking at "/home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlite.so"
                                    Found metadata in lib /home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlite.so, metadata=
                                    {
                                        "IID": "org.qt-project.Qt.QSqlDriverFactoryInter(removed)",
                                        "MetaData": {
                                            "Keys": [
                                                "QSQLITE"
                                            ]
                                        },
                                        "archreq": 0,
                                        "className": "QSQLiteDriverPlugin",
                                        "debug": false,
                                        "version": 331008
                                    }
                                    
                                    Got keys from plugin meta data ("QSQLITE")
                                    QFactoryLoader::QFactoryLoader() looking at "/home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlodbc.so"
                                    Found metadata in lib /home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlodbc.so, metadata=
                                    {
                                        "IID": "org.qt-project.Qt.QSqlDriverFactoryInter(removed)",
                                        "MetaData": {
                                            "Keys": [
                                                "QODBC3",
                                                "QODBC"
                                            ]
                                        },
                                        "archreq": 0,
                                        "className": "QODBCDriverPlugin",
                                        "debug": false,
                                        "version": 331008
                                    }
                                    
                                    Got keys from plugin meta data ("QODBC3", "QODBC")
                                    QFactoryLoader::QFactoryLoader() looking at "/home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlpsql.so"
                                    Found metadata in lib /home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlpsql.so, metadata=
                                    {
                                        "IID": "org.qt-project.Qt.QSqlDriverFactoryInter(removed)",
                                        "MetaData": {
                                            "Keys": [
                                                "QPSQL7",
                                                "QPSQL"
                                            ]
                                        },
                                        "archreq": 0,
                                        "className": "QPSQLDriverPlugin",
                                        "debug": false,
                                        "version": 331008
                                    }
                                    
                                    Got keys from plugin meta data ("QPSQL7", "QPSQL")
                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/(removed)/build-(removed)_Demo-Desktop_Qt_5_13_0_GCC_64bit-Release/sqldrivers" ...
                                    loaded library "/home/(removed)/Qt/5.13.0/gcc_64/plugins/sqldrivers/libqsqlpsql.so"
                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/Qt/5.13.0/gcc_64/plugins/accessible" ...
                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/(removed)/build-(removed)_Demo-Desktop_Qt_5_13_0_GCC_64bit-Release/accessible" ...
                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/Qt/5.13.0/gcc_64/plugins/accessiblebridge" ...
                                    QFactoryLoader::QFactoryLoader() checking directory path "/home/(removed)/(removed)/build-(removed)_Demo-Desktop_Qt_5_13_0_GCC_64bit-Release/accessiblebridge" ...
                                    QSqlDatabasePrivate::database: unable to open database: "FATAL:  role \"(removed)\" does not exist\nQPSQL: Unable to connect"
                                    
                                    JonB 1 Reply Last reply Reply Quote 0
                                    • JonB
                                      JonB @R-P-H last edited by

                                      @R-P-H said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                                      QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"

                                      I'm not an expert, but you can Google for PSQL FATAL role and get a fair number of hits about this "permission" issue, e.g. https://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist.

                                      R 1 Reply Last reply Reply Quote 0
                                      • R
                                        R-P-H @JonB last edited by

                                        @JonB said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                                        @R-P-H said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                                        QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"

                                        I'm not an expert, but you can Google for PSQL FATAL role and get a fair number of hits about this "permission" issue, e.g. https://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist.

                                        No, that is caused by the following line of code:

                                        QSqlDatabase::addDatabase("QPSQL");

                                        It is because at this point in the code I have not yet set the details for accessing the database. I am just loading the driver on startup. So this "error" is not the issue.

                                        1 Reply Last reply Reply Quote 0
                                        • SGaist
                                          SGaist Lifetime Qt Champion last edited by

                                          @R-P-H said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                                          FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect

                                          Does removed match postgres ? If not, how did you create the database in the first place ? Under which role did you create it ?

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          R 1 Reply Last reply Reply Quote 0
                                          • R
                                            R-P-H @SGaist last edited by R-P-H

                                            @SGaist No. The database is already created with a specific role that has access.

                                            /*Somewhere in beginning of program*/
                                            QSqlDatabase::addDatabase("QPSQL"); //Only called once
                                            
                                            /* Later in program when accessing the database */
                                            QSqlDatabase db = QSqlDatabase::database();
                                            db.setHostName("localhost");
                                            db.setPort(5432);
                                            db.setDatabaseName("test_db");
                                            db.setUserName("test_user");
                                            db.setPassword("test_pass");
                                            

                                            The role with access to the db and table is "test_user", created using pgadmin4 or psql.

                                            1 Reply Last reply Reply Quote 0
                                            • V
                                              VRonin @R-P-H last edited by

                                              @R-P-H said in QSqlTableModel() empty on Ubuntu but works on Windows ?:

                                              On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.

                                              This is what baffles me. If the problem was in the db driver, the query should fail too. I'm very confused about what's going on...

                                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                              ~Napoleon Bonaparte

                                              On a crusade to banish setIndexWidget() from the holy land of Qt

                                              JonB R 2 Replies Last reply Reply Quote 0
                                              • First post
                                                Last post