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.
  • 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
                • SGaistS Offline
                  SGaistS 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
                  • SGaistS SGaist
                    15 Jun 2020, 20:28

                    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.

                    R Offline
                    R Offline
                    R-P-H
                    wrote on 16 Jun 2020, 19:39 last edited by R-P-H
                    #33

                    @SGaist Hi, I just use psql to create the database/table/user.

                    CREATE DATABASE my_db
                        WITH 
                        OWNER = postgres
                        ENCODING = 'UTF8'
                        TABLESPACE = pg_default
                        CONNECTION LIMIT = -1;
                    
                    CREATE ROLE my_user WITH
                      LOGIN
                      NOSUPERUSER
                      INHERIT
                      NOCREATEDB
                      NOCREATEROLE
                      NOREPLICATION
                      ENCRYPTED PASSWORD 'md5hidden';
                    
                    CREATE TABLE public.my_table
                    (
                        example_column character varying COLLATE pg_catalog."default",
                    )
                    
                    TABLESPACE pg_default;
                    
                    ALTER TABLE public.my_table OWNER to postgres;
                    
                    GRANT INSERT, SELECT, DELETE ON TABLE public.my_table TO my_user;
                    

                    So for example:

                    sudo -u postgres psql -c 'one of the above blocks of code'
                    

                    So here my_db is the database name, my_user is the role I'm using to access the table and my_table is the table name.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      R-P-H
                      wrote on 18 Jun 2020, 21:00 last edited by
                      #34

                      Is there anything else I can try ?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 18 Jun 2020, 21:19 last edited by
                        #35

                        Sorry I did not had time to test the database.

                        One thing that you could try (I don't think it would change anything but worth a try), reorder the database setup and add your custom role and it setup both at the end.

                        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 19 Jun 2020, 19:35
                        0
                        • SGaistS SGaist
                          18 Jun 2020, 21:19

                          Sorry I did not had time to test the database.

                          One thing that you could try (I don't think it would change anything but worth a try), reorder the database setup and add your custom role and it setup both at the end.

                          R Offline
                          R Offline
                          R-P-H
                          wrote on 19 Jun 2020, 19:35 last edited by
                          #36

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

                          Sorry I did not had time to test the database.

                          One thing that you could try (I don't think it would change anything but worth a try), reorder the database setup and add your custom role and it setup both at the end.

                          I guess I could try that but why would the error be

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

                          then ?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 10 Jul 2020, 21:48 last edited by
                            #37

                            Gut feeling: PostgreSQL namespace.

                            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 Jul 2020, 13:14
                            0
                            • SGaistS SGaist
                              10 Jul 2020, 21:48

                              Gut feeling: PostgreSQL namespace.

                              R Offline
                              R Offline
                              R-P-H
                              wrote on 11 Jul 2020, 13:14 last edited by
                              #38

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

                              Gut feeling: PostgreSQL namespace.

                              I had a look, but I don't quite understand how it applies to this...

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 11 Jul 2020, 19:18 last edited by
                                #39

                                The database role must have access to the namespace in order to access the relation it contains.

                                There might be a glitch there.

                                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 Jul 2020, 18:49
                                0
                                • SGaistS SGaist
                                  11 Jul 2020, 19:18

                                  The database role must have access to the namespace in order to access the relation it contains.

                                  There might be a glitch there.

                                  R Offline
                                  R Offline
                                  R-P-H
                                  wrote on 16 Jul 2020, 18:49 last edited by
                                  #40

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

                                  The database role must have access to the namespace in order to access the relation it contains.

                                  There might be a glitch there.

                                  Yet the same role (same exact application/code) works fine on Windows ?

                                  1 Reply Last reply
                                  0
                                  • R Offline
                                    R Offline
                                    R-P-H
                                    wrote on 7 Oct 2020, 14:21 last edited by
                                    #41

                                    Upgrading to Qt Version 5.15+ solved the issue.

                                    1 Reply Last reply
                                    1

                                    • Login

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