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. Editing data in QStandardItemModel (and tableview)

Editing data in QStandardItemModel (and tableview)

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 8.2k 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.
  • SGaistS SGaist

    Hi,

    By save, do you mean in your QStandardItemModel object or in the database ?

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

    @SGaist
    Hi,
    My bad. I ment the db.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #7

      ok, this is a whole different matter.

      You have to connect a slot to dataChanged of the model and execute a query to update the db

      "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

      G 1 Reply Last reply
      1
      • VRoninV VRonin

        ok, this is a whole different matter.

        You have to connect a slot to dataChanged of the model and execute a query to update the db

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

        @VRonin
        I'm trying to implement the slot and query.
        I have the following for connect:

        connect(fixModel, SIGNAL(fixModel->dataChanged),this, SLOT(refreshDb()));
        

        For updating the db:

            QSqlQuery query_update;
            query_update.prepare ("UPDATE Items SET ID = :fixID");
            query_update.bindValue (":fixID",ID);
        

        Assuming this works (wasn't able to test it yet). To make this work, I need to get the ID from column 0 of tableview. I try to use this:

            QModelIndex fixIndex(index.model ()->index.fixRow(),0,index.parent ());
            qDebug() << fixIndex.data (Qt::DisplayRole);
            QVariant vfix(fixIndex.data(Qt::DisplayRole));
            fixID = vfix.toString ();
            qDebug() << "fixID: " << fixID;
        

        but the first line gives me an error:
        C:\Programming\Projects\Folkfriends_1_0\fixdb.cpp:124: error: '(& index)->QModelIndex::model()->QAbstractItemModel::index' does not have class type
        QModelIndex fixIndex(index.model ()->index.fixRow(),0,index.parent ());
        ^
        What did I miss?
        Thank you.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #9

          Are you in a hurry? looks like it...

          connect(fixModel, SIGNAL(fixModel->dataChanged),this, SLOT(refreshDb())); makes no sense. the SIGNAL argument is completely wrong and both miss the arguments...

          index.model ()->index makes no sense. index.row() is what you need

          "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

          G 1 Reply Last reply
          0
          • VRoninV VRonin

            Are you in a hurry? looks like it...

            connect(fixModel, SIGNAL(fixModel->dataChanged),this, SLOT(refreshDb())); makes no sense. the SIGNAL argument is completely wrong and both miss the arguments...

            index.model ()->index makes no sense. index.row() is what you need

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

            @VRonin
            I think I need to emit an itemChanged signal and it has to receive by the function which will process the db update.
            I came up with the following, but it is still not working:

             connect(fixModel ,SIGNAL(itemChanged), this, SLOT(updateItem(itemChanged)));
            

            How can I emit the itemChanged signal correctly? I get the error message
            "QObject::connect: Parentheses expected, signal QStandardItemModel::itemChanged in ..\Folkfriends_1_0\fixdb.cpp:107"
            Thank you

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #11

              Both your SIGNAL and SLOT macro content is wrong.

              You really should use the syntax as described here. It will avoid such errors.

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

              G 1 Reply Last reply
              1
              • SGaistS SGaist

                Both your SIGNAL and SLOT macro content is wrong.

                You really should use the syntax as described here. It will avoid such errors.

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

                @SGaist
                I try to use the sample .
                It creates a 2 dimensional array QString m_gridData[ROWS][COLS] to hold the text entered into QTableView. This requires me to know the number of rows and columns. Is there any way I can get the text entered not using an array? Thank you.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  QVector< QVector<QString> > m_gridData;

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

                  G 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    QVector< QVector<QString> > m_gridData;

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

                    @SGaist
                    I try to understand how to work this QVector. I believe your code includes 2 QVectors. I think what I'm trying to do is adding 2 int (the row and col) to the vector plus the QString stored at the location defined by the 2 int. Would you please show me how to do that? Thank you.

                    mrjjM 1 Reply Last reply
                    0
                    • G gabor53

                      @SGaist
                      I try to understand how to work this QVector. I believe your code includes 2 QVectors. I think what I'm trying to do is adding 2 int (the row and col) to the vector plus the QString stored at the location defined by the 2 int. Would you please show me how to do that? Thank you.

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

                      @gabor53
                      Hi To get the string, you need to know row, col so why you need to store it with string ?

                      Anyway, you can use a struct
                      struct MyData {
                      QString data;
                      int col;
                      int row;
                      };

                      QVector< QVector<MyData> > m_gridData;

                      Notice !
                      unlike
                      QString m_gridData[ROWS][COLS];

                      QVector is EMPTY to start with so you must append before you can access.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        Or if you know the initial size your can fill your QVector of QVector are startup.

                        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
                        0

                        • Login

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