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.5k 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.
  • mrjjM mrjj

    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 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #5

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

      G 1 Reply Last reply
      1
      • mrjjM mrjj

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

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #6

        @mrjj
        It gives the same error message for the

        ;
        model->setQuery (q);
        
        ```line....
        mrjjM 1 Reply Last reply
        0
        • G gabor53

          @mrjj
          It gives the same error message for the

          ;
          model->setQuery (q);
          
          ```line....
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #7

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

          G 1 Reply Last reply
          1
          • mrjjM mrjj

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

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #8

            @mrjj

            Yes, like this:

            QT       += core gui sql
            
            mrjjM 1 Reply Last reply
            0
            • G gabor53

              @mrjj

              Yes, like this:

              QT       += core gui sql
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @gabor53
              and
              #include <QSqlQueryModel>
              ?

              G 1 Reply Last reply
              1
              • mrjjM mrjj

                @gabor53
                and
                #include <QSqlQueryModel>
                ?

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #10

                @mrjj

                I have that one too.

                mrjjM 1 Reply Last reply
                0
                • G gabor53

                  @mrjj

                  I have that one too.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #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
                  1
                  • mrjjM mrjj

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

                      mrjjM 1 Reply Last reply
                      3
                      • SGaistS SGaist

                        Hi,

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

                        QSqlTableModel goal is to make a table accessible.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #14

                        QSqlTableModel

                        Good catch :)

                        G 1 Reply Last reply
                        2
                        • mrjjM mrjj

                          QSqlTableModel

                          Good catch :)

                          G Offline
                          G Offline
                          gabor53
                          wrote on 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

                          • Login

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