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. QML, QSqlTableModel: not saving data
Qt 6.11 is out! See what's new in the release blog

QML, QSqlTableModel: not saving data

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 1.9k Views 2 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 Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    @Bilal_Khan said in QML, QSqlTableModel: not saving data:

    modelIndex

    Are you sure this index is valid ?

    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
    1
    • Bilal_KhanB Offline
      Bilal_KhanB Offline
      Bilal_Khan
      wrote on last edited by Bilal_Khan
      #3

      This modelIndex is computed in both functions (data() & setData()) in the same way but it does not work while setting data. I have also tried this->index with different literal values but no success so far..

      itemModel->setEditStrategy(QSqlTableModel::OnFieldChange);
      
      1 Reply Last reply
      0
      • Bilal_KhanB Offline
        Bilal_KhanB Offline
        Bilal_Khan
        wrote on last edited by Bilal_Khan
        #4

        Update (UBUNTU 20.04/ Qt-5.13 & 6.12/ SQLITE3 & PSQL):

        • I no longer override setData and directly call the parent's implementation:
        qDebug() <<  itemModel->setData(itemModel->index(0, 1), "NEW VALUE", Qt::EditRole); // false
         qDebug() << itemModel->lastError();   // QSqlError("", "", "")
        

        but no vain.

        • Surprisingly, there are no errors on passing invalid index.

        • I confirm that setRecord is also not working (no errors)

         ItemModel *itemModel = new ItemModel(nullptr,db);
         itemModel->setEditStrategy(QSqlTableModel::OnFieldChange);
         itemModel->setTable("items");
         itemModel->generateRoleNames();
         itemModel->select();
         qDebug() <<  itemModel->rowCount(); // 2
         itemModel->query().seek(0);
         QSqlRecord rec = itemModel->query().record();
        
         rec.setValue(1, QString("SOMETHING"));
         qDebug() << itemModel->setRecord(0,rec);  //true
        
         QModelIndex index = itemModel->index(0, 0);
         qDebug() << "Update" << itemModel->data(index, 258);   // old value returned
        
        1 Reply Last reply
        0
        • Bilal_KhanB Offline
          Bilal_KhanB Offline
          Bilal_Khan
          wrote on last edited by Bilal_Khan
          #5

          It's just the problem with subclassing. setData does not work.

            enum ItemRoles {
                  CONTENTROLE = Qt::UserRole + 1,
                  TYPEROLE = Qt::UserRole + 2,
                  VIEWSROLE = Qt::UserRole + 3,
                  WEIGHTROLE
              };
          QHash<int, QByteArray> ItemModel::roleNames() const  {
                 QHash<int, QByteArray> roles;
                 roles[CONTENTROLE] = "content";
                 roles[TYPEROLE] = "type";
                 roles[VIEWSROLE] = "views";
                 roles[WEIGHTROLE] = "weight";
                 return roles;
          }
          bool ItemModel::setData(const QModelIndex &index, const QVariant &value, int role) {
              QModelIndex modelIndex = this->index(index.row(), role - Qt::UserRole);
          
              qDebug() << QSqlTableModel::setData(modelIndex,value, Qt::EditRole); // true
              qDebug() << lastError();
              emit dataChanged(modelIndex,modelIndex);
              return true;
          }
          
          //
          QModelIndex index = itemModel->index(0, 0);
          itemModel->setData(index, "VALUE",  itemModel->CONTENTROLE);
           qDebug() <<  itemModel->data(index, itemModel->CONTENTROLE); // unchanged data
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            What if you call submit after setRecord ?

            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
            • Bilal_KhanB Offline
              Bilal_KhanB Offline
              Bilal_Khan
              wrote on last edited by
              #7

              Yes, I did but does not work

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

                Can you provide a minimal compilable example so that it's possible to reproduce your issue.

                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
                • Bilal_KhanB Offline
                  Bilal_KhanB Offline
                  Bilal_Khan
                  wrote on last edited by Bilal_Khan
                  #9

                  Hi,
                  Thank you @SGaist for your kind support ... so far

                  As you could see , I have made some changes. Now, after the update in setData, the data function returns qvariant(invalid).

                  Project Link:
                  Test Qt Project

                  1 Reply Last reply
                  0
                  • Bilal_KhanB Offline
                    Bilal_KhanB Offline
                    Bilal_Khan
                    wrote on last edited by
                    #10

                    @SGaist , sorry to bother you again. Did you check it, what you have found?

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

                      This works correctly.

                      #ifndef ITEMMODEL_H
                      #define ITEMMODEL_H
                      
                      #include <QSqlTableModel>
                      
                      class ItemModel : public QSqlTableModel {
                          Q_OBJECT
                      public:
                      
                          enum ItemRoles {
                              IDROLE = Qt::UserRole + 1,
                              CONTENTROLE = Qt::UserRole + 2,
                              TYPEROLE = Qt::UserRole + 3,
                              VIEWSROLE = Qt::UserRole + 4,
                              WEIGHTROLE
                          };
                          Q_ENUMS(ItemRoles)
                      
                          ItemModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase());
                      
                          QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
                          bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
                      
                          QHash<int, QByteArray> roleNames() const;
                      
                      private:
                          Q_DISABLE_COPY(ItemModel);
                      
                          QModelIndex indexForRole(const QModelIndex& index, int role) const;
                      };
                      
                      #endif // ITEMMODEL_H
                      
                      #include "ItemModel.h"
                      
                      ItemModel::ItemModel(QObject *parent, QSqlDatabase db):
                          QSqlTableModel(parent, db)
                      {
                      
                      }
                      
                      QVariant ItemModel::data(const QModelIndex & index, int role) const
                      {
                          return QSqlTableModel::data(indexForRole(index, role), Qt::EditRole);
                      }
                      
                      
                      bool ItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
                      {
                          return QSqlTableModel::setData(indexForRole(index, role), value, Qt::EditRole);
                      }
                      
                      
                      QHash<int, QByteArray> ItemModel::roleNames() const  {
                             QHash<int, QByteArray> roles;
                             roles[IDROLE] = "id";
                             roles[CONTENTROLE] = "content";
                             roles[TYPEROLE] = "type";
                             roles[VIEWSROLE] = "views";
                             roles[WEIGHTROLE] = "weight";
                             return roles;
                      }
                      
                      
                      QModelIndex ItemModel::indexForRole(const QModelIndex& index, int role) const
                      {
                          int row = index.row();
                          int column = index.column();
                          if (role > Qt::UserRole) {
                              column = role - Qt::UserRole - 1;
                          }
                          return this->index(row, column);
                      }
                      

                      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
                      1

                      • Login

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