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. QTableView Empty ?

QTableView Empty ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtableviewqsqltablemodelsqlite3
18 Posts 4 Posters 3.3k 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.
  • R Offline
    R Offline
    R-P-H
    wrote on 4 Jun 2019, 14:56 last edited by
    #1

    Hi, I am trying to display data stored in a vector of QSqlRecord in a QTableView within my GUI. I have this code for testing:

    QVector<QSqlRecord> records;
    QSqlTableModel *model = new QSqlTableModel();
    model->insertRecord( 0, records[0]);
    ui->tableView->setModel(model);
    

    I expect to see one row in the table, but the table remains empty. What am i doing wrong ? Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 4 Jun 2019, 15:20 last edited by
      #2

      Hi
      Is the database connected?
      Anyway, the topic is treated here.
      https://forum.qt.io/topic/86139/insert-record-in-qsqltablemodel-with-qtableview

      R 1 Reply Last reply 4 Jun 2019, 15:53
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 4 Jun 2019, 15:34 last edited by
        #3

        @R-P-H said in QTableView Empty ?:

        What am i doing wrong ?

        QVector<QSqlRecord> records;
        ... records[0] - but the vector is empty.

        In debug mode you will likely hit an assertion here. In release it's undefined behavior.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        R 1 Reply Last reply 4 Jun 2019, 15:55
        1
        • M mrjj
          4 Jun 2019, 15:20

          Hi
          Is the database connected?
          Anyway, the topic is treated here.
          https://forum.qt.io/topic/86139/insert-record-in-qsqltablemodel-with-qtableview

          R Offline
          R Offline
          R-P-H
          wrote on 4 Jun 2019, 15:53 last edited by R-P-H 6 Apr 2019, 15:59
          #4

          @mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.

          I just want to easily display them in a table without manually looping through fields...

          Do I need to initialise a dummy database or something ?

          S 1 Reply Last reply 4 Jun 2019, 18:55
          0
          • C Christian Ehrlicher
            4 Jun 2019, 15:34

            @R-P-H said in QTableView Empty ?:

            What am i doing wrong ?

            QVector<QSqlRecord> records;
            ... records[0] - but the vector is empty.

            In debug mode you will likely hit an assertion here. In release it's undefined behavior.

            R Offline
            R Offline
            R-P-H
            wrote on 4 Jun 2019, 15:55 last edited by
            #5

            @Christian-Ehrlicher said in QTableView Empty ?:

            @R-P-H said in QTableView Empty ?:

            What am i doing wrong ?

            QVector<QSqlRecord> records;
            ... records[0] - but the vector is empty.

            In debug mode you will likely hit an assertion here. In release it's undefined behavior.

            The QVector of records is populated with data, it is not empty. I have double checked that.

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 4 Jun 2019, 18:16 last edited by
              #6

              @R-P-H said in QTableView Empty ?:

              I have double checked that.

              Your code example shows something different, therefore my comment. insertRecord() returns a bool which you should check.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              R 1 Reply Last reply 4 Jun 2019, 18:29
              1
              • C Christian Ehrlicher
                4 Jun 2019, 18:16

                @R-P-H said in QTableView Empty ?:

                I have double checked that.

                Your code example shows something different, therefore my comment. insertRecord() returns a bool which you should check.

                R Offline
                R Offline
                R-P-H
                wrote on 4 Jun 2019, 18:29 last edited by
                #7

                @Christian-Ehrlicher said in QTableView Empty ?:

                @R-P-H said in QTableView Empty ?:

                I have double checked that.

                Your code example shows something different, therefore my comment. insertRecord() returns a bool which you should check.

                Yes I didn't show how records gets populated but there is data inside. I did check the bool and it seems to be returning false. I can't understand why though...

                1 Reply Last reply
                0
                • R R-P-H
                  4 Jun 2019, 15:53

                  @mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.

                  I just want to easily display them in a table without manually looping through fields...

                  Do I need to initialise a dummy database or something ?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 4 Jun 2019, 18:55 last edited by
                  #8

                  @R-P-H said in QTableView Empty ?:

                  @mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.

                  I just want to easily display them in a table without manually looping through fields...

                  Do I need to initialise a dummy database or something ?

                  Hi,

                  If you don't have any database then why are you using a SQL model ?

                  Before going further, do you want to store your data in a database ? Or just show them in a tabular fashion ?

                  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 4 Jun 2019, 19:05
                  1
                  • S SGaist
                    4 Jun 2019, 18:55

                    @R-P-H said in QTableView Empty ?:

                    @mrjj No there is no database...I get given the table of results in "records" which have already been populated from a previous data pull.

                    I just want to easily display them in a table without manually looping through fields...

                    Do I need to initialise a dummy database or something ?

                    Hi,

                    If you don't have any database then why are you using a SQL model ?

                    Before going further, do you want to store your data in a database ? Or just show them in a tabular fashion ?

                    R Offline
                    R Offline
                    R-P-H
                    wrote on 4 Jun 2019, 19:05 last edited by
                    #9

                    @SGaist Mainly I just want to display it in a tabular fashion but without the work involved in looping through each QSqlRecord and corresponding field.

                    Considering the data is stored in QSqlRecords I thought using QTableView would be the easiest way to achieve that...

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 4 Jun 2019, 19:12 last edited by
                      #10

                      You still need a valid database to insert your records in.

                      You can use the SQLite plugin for that and the in memory database if you don't want to store in on disk.

                      Note that you still have to create the table before putting something in 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 4 Jun 2019, 19:37
                      2
                      • S SGaist
                        4 Jun 2019, 19:12

                        You still need a valid database to insert your records in.

                        You can use the SQLite plugin for that and the in memory database if you don't want to store in on disk.

                        Note that you still have to create the table before putting something in it.

                        R Offline
                        R Offline
                        R-P-H
                        wrote on 4 Jun 2019, 19:37 last edited by
                        #11

                        @SGaist Do you mean creating a database and database table in code ?

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 4 Jun 2019, 19:42 last edited by
                          #12

                          Before going further, one thing is not clear... Where do you get these records from ?

                          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 4 Jun 2019, 20:02
                          1
                          • S SGaist
                            4 Jun 2019, 19:42

                            Before going further, one thing is not clear... Where do you get these records from ?

                            R Offline
                            R Offline
                            R-P-H
                            wrote on 4 Jun 2019, 20:02 last edited by
                            #13

                            @SGaist From a sqlite database on file...The output from that function is a QVector of QSqlRecords and I didn't really want to mess with that function but now I have the need to display the data neatly in my GUI.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 4 Jun 2019, 20:06 last edited by
                              #14

                              Then why not use a QSqlTableModel ?
                              Or a QSqlQueryModel ?

                              There would be no need for that vector of records.

                              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 4 Jun 2019, 20:28
                              1
                              • S SGaist
                                4 Jun 2019, 20:06

                                Then why not use a QSqlTableModel ?
                                Or a QSqlQueryModel ?

                                There would be no need for that vector of records.

                                R Offline
                                R Offline
                                R-P-H
                                wrote on 4 Jun 2019, 20:28 last edited by
                                #15

                                @SGaist Let's say I only get passed the records and can't access the DB file myself... Obviously the function could be changed to pass the results in that format already but right now it's in the form of QtSqlRecords...

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 4 Jun 2019, 20:58 last edited by
                                  #16

                                  So you can either do as suggested and create that minimal database with the table needed to show your data or use a QStandardItemModel where you put the content of all these records.

                                  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 4 Jun 2019, 21:14
                                  0
                                  • S SGaist
                                    4 Jun 2019, 20:58

                                    So you can either do as suggested and create that minimal database with the table needed to show your data or use a QStandardItemModel where you put the content of all these records.

                                    R Offline
                                    R Offline
                                    R-P-H
                                    wrote on 4 Jun 2019, 21:14 last edited by
                                    #17

                                    @SGaist For QStandardModel I don't see a way to directly make use of my QtSqlRecords, does that mean I have to loop manually through the fields ? Would it still be shown in QTableView ?

                                    For making a minimal database do I need to set a database name?

                                    I will try to implement the other model on the actual dB as well....

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 4 Jun 2019, 21:16 last edited by
                                      #18

                                      Yes you do, or you can make your own custom QAbstractTableModel that will return the content of your vector of records.

                                      Yes you do. However you can use an in-memory database if you don't want to have another database in a file.

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

                                      1 Reply Last reply
                                      3

                                      10/18

                                      4 Jun 2019, 19:12

                                      • Login

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