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. Text from database to QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

Text from database to QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
database
15 Posts 4 Posters 5.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on 7 May 2016, 03:42 last edited by
    #1

    Hi,
    I'm trying to display string from a database into a tableView using StandardItemModel.

        Item->setData (NameD = query.value (0).toString (),Qt::DisplayRole);
        qDebug() << "Name(1) " << NameD;
        smodel->setItem (row,0,NameD);
    

    The code generates a long error message:
    C:\Programming\Projects\Folkfriends\mainwindow.cpp:54: error: no matching function for call to 'QStandardItemModel::setItem(int&, int, QString&)'
    smodel->setItem (row,0,NameD);
    ^
    C:\Qt\5.5\mingw492_32\include\QtGui\qstandarditemmodel.h:357: note: no known conversion for argument 3 from 'QString' to 'QStandardItem*'
    C:\Qt\5.5\mingw492_32\include\QtGui\qstandarditemmodel.h:420: note: candidate expects 2 arguments, 3 provided

    How can I use setItem with a QString (I think that causes the trouble) and howto avoid this error?
    Thank you.

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yizhixiaobai
      wrote on 7 May 2016, 03:56 last edited by
      #2

      look this:
      Qt5.6.0
      void QStandardItemModel::setItem(int row, int column, QStandardItem *item);
      maybe you can use it like this:
      setItem(row,0,&NameD);

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 7 May 2016, 08:03 last edited by mrjj 5 Jul 2016, 08:10
        #3

        Hi
        Its not really clear what you are trying but if you are trying to ADD
        new items, you should new EACH of them.

        QStandardItemModel model(4, 4);
        for (int row = 0; row < 4; ++row) {
            for (int column = 0; column < 4; ++column) {
        // Create new Item
                QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
        // add it
                model.setItem(row, column, item); 
            }
        }
        

        Also if from database, why not just take it directly using QSqlQueryModel:
        QSqlQueryModel* model = new QSqlQueryModel();
        model->setQuery("select firstname from person"); // display all firstnames

        G 1 Reply Last reply 7 May 2016, 14:38
        1
        • M mrjj
          7 May 2016, 08:03

          Hi
          Its not really clear what you are trying but if you are trying to ADD
          new items, you should new EACH of them.

          QStandardItemModel model(4, 4);
          for (int row = 0; row < 4; ++row) {
              for (int column = 0; column < 4; ++column) {
          // Create new Item
                  QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
          // add it
                  model.setItem(row, column, item); 
              }
          }
          

          Also if from database, why not just take it directly using QSqlQueryModel:
          QSqlQueryModel* model = new QSqlQueryModel();
          model->setQuery("select firstname from person"); // display all firstnames

          G Offline
          G Offline
          gabor53
          wrote on 7 May 2016, 14:38 last edited by
          #4

          Hi @mrjj,
          I tried the second solution first. When I used

          QSqlTableModel *model = new QSqlTableModel();
          model->setQuery ("SELECT Name FROM Item" );
          

          I got the following error message:
          C:\Programming\Projects\Folkfriends\mainwindow.cpp:42: error: no matching function for call to 'QSqlTableModel::setQuery(const char [22])'
          model->setQuery ("SELECT Name FROM Item" );

          What did I miss?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 7 May 2016, 15:27 last edited by
            #5

            @gabor53 said:
            Hmm, even ?
            QString q;
            q="SELECT Name FROM Item";
            model->setQuery (q);

            G 1 Reply Last reply 7 May 2016, 19:58
            1
            • M mrjj
              7 May 2016, 15:27

              @gabor53 said:
              Hmm, even ?
              QString q;
              q="SELECT Name FROM Item";
              model->setQuery (q);

              G Offline
              G Offline
              gabor53
              wrote on 7 May 2016, 19:58 last edited by
              #6

              @mrjj
              It gives the same error message for the

              ;
              model->setQuery (q);
              
              ```line....
              M 1 Reply Last reply 7 May 2016, 20:00
              0
              • G gabor53
                7 May 2016, 19:58

                @mrjj
                It gives the same error message for the

                ;
                model->setQuery (q);
                
                ```line....
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 7 May 2016, 20:00 last edited by
                #7

                @gabor53
                hmm.
                Do you have
                QT += sql
                in the .pro file?

                G 1 Reply Last reply 7 May 2016, 20:01
                1
                • M mrjj
                  7 May 2016, 20:00

                  @gabor53
                  hmm.
                  Do you have
                  QT += sql
                  in the .pro file?

                  G Offline
                  G Offline
                  gabor53
                  wrote on 7 May 2016, 20:01 last edited by
                  #8

                  @mrjj

                  Yes, like this:

                  QT       += core gui sql
                  
                  M 1 Reply Last reply 7 May 2016, 20:02
                  0
                  • G gabor53
                    7 May 2016, 20:01

                    @mrjj

                    Yes, like this:

                    QT       += core gui sql
                    
                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 7 May 2016, 20:02 last edited by
                    #9

                    @gabor53
                    and
                    #include <QSqlQueryModel>
                    ?

                    G 1 Reply Last reply 7 May 2016, 20:03
                    1
                    • M mrjj
                      7 May 2016, 20:02

                      @gabor53
                      and
                      #include <QSqlQueryModel>
                      ?

                      G Offline
                      G Offline
                      gabor53
                      wrote on 7 May 2016, 20:03 last edited by
                      #10

                      @mrjj

                      I have that one too.

                      M 1 Reply Last reply 7 May 2016, 20:04
                      0
                      • G gabor53
                        7 May 2016, 20:03

                        @mrjj

                        I have that one too.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 7 May 2016, 20:04 last edited by mrjj 5 Jul 2016, 20:12
                        #11

                        @gabor53
                        ok. no idea then.
                        http://doc.qt.io/qt-5/qsqlquerymodel.html

                        What version of Qt do you have ? did you compile yourself?

                        G 1 Reply Last reply 7 May 2016, 20:25
                        1
                        • M mrjj
                          7 May 2016, 20:04

                          @gabor53
                          ok. no idea then.
                          http://doc.qt.io/qt-5/qsqlquerymodel.html

                          What version of Qt do you have ? did you compile yourself?

                          G Offline
                          G Offline
                          gabor53
                          wrote on 7 May 2016, 20:25 last edited by
                          #12

                          @mrjj

                          I have 5.6 and Yes I compiled myself.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 7 May 2016, 20:28 last edited by
                            #13

                            Hi,

                            That's because you are using a QSqlTableModel, you should use a QSqlQueryModel.

                            QSqlTableModel goal is to make a table accessible.

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

                            M 1 Reply Last reply 7 May 2016, 20:44
                            3
                            • SGaistS SGaist
                              7 May 2016, 20:28

                              Hi,

                              That's because you are using a QSqlTableModel, you should use a QSqlQueryModel.

                              QSqlTableModel goal is to make a table accessible.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 7 May 2016, 20:44 last edited by mrjj 5 Jul 2016, 20:44
                              #14

                              QSqlTableModel

                              Good catch :)

                              G 1 Reply Last reply 8 May 2016, 03:11
                              2
                              • M mrjj
                                7 May 2016, 20:44

                                QSqlTableModel

                                Good catch :)

                                G Offline
                                G Offline
                                gabor53
                                wrote on 8 May 2016, 03:11 last edited by
                                #15

                                @mrjj
                                The following worked:

                                	model = new QSqlTableModel(this);
                                    model->setTable ("Items");
                                    model->select ();
                                    ui->tableView->setModel (model);
                                

                                Thank you for all your help!

                                1 Reply Last reply
                                2

                                1/15

                                7 May 2016, 03:42

                                • Login

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