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. QSqlTableModel() empty on Ubuntu but works on Windows ?

QSqlTableModel() empty on Ubuntu but works on Windows ?

Scheduled Pinned Locked Moved Solved General and Desktop
qsqltablemodelqsqldatabasepostgresql
41 Posts 4 Posters 8.4k 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.
  • S SGaist
    10 Jun 2020, 21:48

    Looks like you are not connected to the database.

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

    R Offline
    R Offline
    R-P-H
    wrote on 11 Jun 2020, 09:24 last edited by R-P-H 6 Nov 2020, 09:26
    #13

    @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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Jun 2020, 09:26 last edited by
      #14

      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 11 Jun 2020, 09:49
      0
      • S SGaist
        11 Jun 2020, 09:26

        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.

        R Offline
        R Offline
        R-P-H
        wrote on 11 Jun 2020, 09:49 last edited by
        #15

        @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"
        
        J 1 Reply Last reply 11 Jun 2020, 09:54
        0
        • R R-P-H
          11 Jun 2020, 09:49

          @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"
          
          J Offline
          J Offline
          JonB
          wrote on 11 Jun 2020, 09:54 last edited by
          #16

          @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 11 Jun 2020, 09:57
          0
          • J JonB
            11 Jun 2020, 09:54

            @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 Offline
            R Offline
            R-P-H
            wrote on 11 Jun 2020, 09:57 last edited by
            #17

            @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
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Jun 2020, 18:10 last edited by
              #18

              @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 12 Jun 2020, 09:47
              0
              • S SGaist
                11 Jun 2020, 18:10

                @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 ?

                R Offline
                R Offline
                R-P-H
                wrote on 12 Jun 2020, 09:47 last edited by R-P-H 6 Dec 2020, 09:48
                #19

                @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
                0
                • R R-P-H
                  9 Jun 2020, 22:20

                  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 Offline
                  V Offline
                  VRonin
                  wrote on 12 Jun 2020, 10:12 last edited by
                  #20

                  @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

                  J R 2 Replies Last reply 12 Jun 2020, 10:23
                  0
                  • V VRonin
                    12 Jun 2020, 10:12

                    @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...

                    J Offline
                    J Offline
                    JonB
                    wrote on 12 Jun 2020, 10:23 last edited by JonB 6 Dec 2020, 10:26
                    #21

                    @VRonin
                    @R-P-H has already said:

                    QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"
                    No, that is caused by the following line of code:

                    QSqlDatabase::addDatabase("QPSQL");
                    On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.

                    So we seem to have an error at add database stage, and then QSqlQuery works fine but not QSqlTableModel. But I think the OP says this does not matter (though it would worry me).

                    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", "")

                    Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                    R 1 Reply Last reply 12 Jun 2020, 11:20
                    0
                    • V VRonin
                      12 Jun 2020, 10:12

                      @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...

                      R Offline
                      R Offline
                      R-P-H
                      wrote on 12 Jun 2020, 11:17 last edited by
                      #22

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

                      @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...

                      Yes, it makes no sense.

                      1 Reply Last reply
                      0
                      • J JonB
                        12 Jun 2020, 10:23

                        @VRonin
                        @R-P-H has already said:

                        QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"
                        No, that is caused by the following line of code:

                        QSqlDatabase::addDatabase("QPSQL");
                        On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.

                        So we seem to have an error at add database stage, and then QSqlQuery works fine but not QSqlTableModel. But I think the OP says this does not matter (though it would worry me).

                        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", "")

                        Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                        R Offline
                        R Offline
                        R-P-H
                        wrote on 12 Jun 2020, 11:20 last edited by
                        #23

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

                        @VRonin
                        @R-P-H has already said:

                        QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"
                        No, that is caused by the following line of code:

                        QSqlDatabase::addDatabase("QPSQL");
                        On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.

                        So we seem to have an error at add database stage, and then QSqlQuery works fine but not QSqlTableModel. But I think the OP says this does not matter (though it would worry me).

                        Even if I change the code to this it still doesn't work (and the error disappears).

                        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
                        db.setHostName("localhost");
                        db.setPort(5432);
                        db.setDatabaseName("test_db");
                        db.setUserName("test_user");
                        db.setPassword("test_pass");
                        
                        J 1 Reply Last reply 12 Jun 2020, 11:26
                        0
                        • R R-P-H
                          12 Jun 2020, 11:20

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

                          @VRonin
                          @R-P-H has already said:

                          QSqlDatabasePrivate::database: unable to open database: "FATAL: role "(removed)" does not exist\nQPSQL: Unable to connect"
                          No, that is caused by the following line of code:

                          QSqlDatabase::addDatabase("QPSQL");
                          On Ubuntu, using a standard QSqlQuery works perfectly in accessing data from the very same database in the same application.

                          So we seem to have an error at add database stage, and then QSqlQuery works fine but not QSqlTableModel. But I think the OP says this does not matter (though it would worry me).

                          Even if I change the code to this it still doesn't work (and the error disappears).

                          QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
                          db.setHostName("localhost");
                          db.setPort(5432);
                          db.setDatabaseName("test_db");
                          db.setUserName("test_user");
                          db.setPassword("test_pass");
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 12 Jun 2020, 11:26 last edited by
                          #24

                          @R-P-H
                          Point taken.

                          I did suggest

                          Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                          ?

                          R 1 Reply Last reply 12 Jun 2020, 13:28
                          0
                          • J JonB
                            12 Jun 2020, 11:26

                            @R-P-H
                            Point taken.

                            I did suggest

                            Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                            ?

                            R Offline
                            R Offline
                            R-P-H
                            wrote on 12 Jun 2020, 13:28 last edited by
                            #25

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

                            @R-P-H
                            Point taken.

                            I did suggest

                            Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                            ?

                            How do I call these functions because they are protected ?

                            J 1 Reply Last reply 12 Jun 2020, 13:46
                            0
                            • R R-P-H
                              12 Jun 2020, 13:28

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

                              @R-P-H
                              Point taken.

                              I did suggest

                              Should the OP try printing out what QString QSqlTableModel::selectStatement() const returns? And/or whatever is in QSqlQuery QSqlQueryModel::query() const?

                              ?

                              How do I call these functions because they are protected ?

                              J Offline
                              J Offline
                              JonB
                              wrote on 12 Jun 2020, 13:46 last edited by JonB 6 Dec 2020, 14:53
                              #26

                              @R-P-H
                              You sub-class from QSqlDatabaseQSqlTableModel. Personally I would always do that anyway, precisely for this reason --- you never know what you may find you need to add/alter from the supplied QSqlDatabaseQSqlTableModel. (I do this for all QWidgets too, but that's another matter.)

                              R 1 Reply Last reply 12 Jun 2020, 14:47
                              0
                              • J JonB
                                12 Jun 2020, 13:46

                                @R-P-H
                                You sub-class from QSqlDatabaseQSqlTableModel. Personally I would always do that anyway, precisely for this reason --- you never know what you may find you need to add/alter from the supplied QSqlDatabaseQSqlTableModel. (I do this for all QWidgets too, but that's another matter.)

                                R Offline
                                R Offline
                                R-P-H
                                wrote on 12 Jun 2020, 14:47 last edited by
                                #27

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

                                @R-P-H
                                You sub-class from QSqlDatabase. Personally I would always do that anyway, precisely for this reason --- you never know what you may find you need to add/alter from the supplied QSqlDatabase. (I do this for all QWidgets too, but that's another matter.)

                                Can you please provide an example ?

                                J 1 Reply Last reply 12 Jun 2020, 15:01
                                0
                                • R R-P-H
                                  12 Jun 2020, 14:47

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

                                  @R-P-H
                                  You sub-class from QSqlDatabase. Personally I would always do that anyway, precisely for this reason --- you never know what you may find you need to add/alter from the supplied QSqlDatabase. (I do this for all QWidgets too, but that's another matter.)

                                  Can you please provide an example ?

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 12 Jun 2020, 15:01 last edited by JonB 6 Dec 2020, 15:08
                                  #28

                                  @R-P-H
                                  Of how to sub-class in C++? It's just basic C++.

                                  I see @SGaist offered this 7 years ago: https://forum.qt.io/topic/30562/example-of-subclassing-qsqltablemodel/2. You'll want something like:

                                  // mytablemodel.h
                                  class MyTableModel : public QSqlTableModel
                                  {
                                  public:
                                  explicit MyTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ); // the same arguments as QSqlTableModel
                                  
                                  protected:
                                  virtual QString selectStatement() const;
                                  };
                                  
                                  // mytablemodel.cpp
                                  MyTableModel::MyTableModel(QObject * parent, QSqlDatabase db) :
                                  QSqlTableModel(parent, db)
                                  {
                                  // the rest of your code
                                  }
                                  
                                  QString MyTableModel::selectStatement() const
                                  {
                                      QString ss = QSqlTableModel::selectStatement();
                                      qDebug() << ss;
                                      return ss;
                                  }
                                  

                                  I don't think QSqlQuery QSqlQueryModel::query() const is virtual, so you can't override it; nor is it protected, it's public, so just call it if you need to access it.

                                  R 1 Reply Last reply 15 Jun 2020, 11:44
                                  0
                                  • J JonB
                                    12 Jun 2020, 15:01

                                    @R-P-H
                                    Of how to sub-class in C++? It's just basic C++.

                                    I see @SGaist offered this 7 years ago: https://forum.qt.io/topic/30562/example-of-subclassing-qsqltablemodel/2. You'll want something like:

                                    // mytablemodel.h
                                    class MyTableModel : public QSqlTableModel
                                    {
                                    public:
                                    explicit MyTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ); // the same arguments as QSqlTableModel
                                    
                                    protected:
                                    virtual QString selectStatement() const;
                                    };
                                    
                                    // mytablemodel.cpp
                                    MyTableModel::MyTableModel(QObject * parent, QSqlDatabase db) :
                                    QSqlTableModel(parent, db)
                                    {
                                    // the rest of your code
                                    }
                                    
                                    QString MyTableModel::selectStatement() const
                                    {
                                        QString ss = QSqlTableModel::selectStatement();
                                        qDebug() << ss;
                                        return ss;
                                    }
                                    

                                    I don't think QSqlQuery QSqlQueryModel::query() const is virtual, so you can't override it; nor is it protected, it's public, so just call it if you need to access it.

                                    R Offline
                                    R Offline
                                    R-P-H
                                    wrote on 15 Jun 2020, 11:44 last edited by
                                    #29

                                    @JonB I'm not quite sure how to implement this correctly. I have a class that deals with all database related stuff. Inside that class I have a function that generates the QSqlTableModel using the code you've already seen.

                                    Now obviously if I create another class for MyTableModel, include it and create model using MyTableModel *model = new MyTableModel(nullptr, db); instead I still get a protected function error when calling its selectStatement() member.

                                    What would be the correct way of testing this ? Thanks.

                                    J 1 Reply Last reply 15 Jun 2020, 12:03
                                    0
                                    • R R-P-H
                                      15 Jun 2020, 11:44

                                      @JonB I'm not quite sure how to implement this correctly. I have a class that deals with all database related stuff. Inside that class I have a function that generates the QSqlTableModel using the code you've already seen.

                                      Now obviously if I create another class for MyTableModel, include it and create model using MyTableModel *model = new MyTableModel(nullptr, db); instead I still get a protected function error when calling its selectStatement() member.

                                      What would be the correct way of testing this ? Thanks.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 15 Jun 2020, 12:03 last edited by JonB
                                      #30

                                      @R-P-H

                                      Inside that class I have a function that generates the QSqlTableModel

                                      You don't want to directly create a QSqlTableModel anywhere, you want it now to create a MyTableModel. E.g if you currently have a new QSqlTableModel somewhere it now goes new MyTableModel. This is basic C++ sub-classing, I don't know whether you're confused over that.

                                      include it and create model using MyTableModel *model = new MyTableModel(nullptr, db); instead I still get a protected function error when calling its selectStatement() member.

                                      Assuming you've got the first bit right, you still cannot call a protected method on an instance from outside of the MyTableModel class code, that's what protected is all about. You will need something like the definition I showed you of QString MyTableModel::selectStatement() const. You can either then write another public method in MyTableModel which calls it and the outside world can go via that public method, or (as I do) override all the methods in MyTableModel which do queries, updates etc. and have them print it out. It takes a few minutes to write these override functions, but once you have them from then on it's a lot easier to see what's going on, do logging, handle any errors etc. from the QSql... classes you use.

                                      R 1 Reply Last reply 15 Jun 2020, 20:16
                                      1
                                      • J JonB
                                        15 Jun 2020, 12:03

                                        @R-P-H

                                        Inside that class I have a function that generates the QSqlTableModel

                                        You don't want to directly create a QSqlTableModel anywhere, you want it now to create a MyTableModel. E.g if you currently have a new QSqlTableModel somewhere it now goes new MyTableModel. This is basic C++ sub-classing, I don't know whether you're confused over that.

                                        include it and create model using MyTableModel *model = new MyTableModel(nullptr, db); instead I still get a protected function error when calling its selectStatement() member.

                                        Assuming you've got the first bit right, you still cannot call a protected method on an instance from outside of the MyTableModel class code, that's what protected is all about. You will need something like the definition I showed you of QString MyTableModel::selectStatement() const. You can either then write another public method in MyTableModel which calls it and the outside world can go via that public method, or (as I do) override all the methods in MyTableModel which do queries, updates etc. and have them print it out. It takes a few minutes to write these override functions, but once you have them from then on it's a lot easier to see what's going on, do logging, handle any errors etc. from the QSql... classes you use.

                                        R Offline
                                        R Offline
                                        R-P-H
                                        wrote on 15 Jun 2020, 20:16 last edited by
                                        #31

                                        @JonB The result of selectStatement() is " ".

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 15 Jun 2020, 20:28 last edited by
                                          #32

                                          Would you be able to provide a small step by step guide to reproduce your test database ? Meaning the command you are currently using on your Linux machine to create the role and the database.

                                          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 16 Jun 2020, 19:39
                                          0

                                          22/41

                                          12 Jun 2020, 11:17

                                          • Login

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