Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using QAbstractModel
Forum Updated to NodeBB v4.3 + New Features

Using QAbstractModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
20 Posts 4 Posters 1.4k Views 1 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
    #4

    It depends on how you implemented it.

    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
    • M Offline
      M Offline
      Mihaill
      wrote on last edited by
      #5

      In main c++ class

      Q_PROPERTY(ModelRegistration* modelRegistration MEMBER m_modelRegistration CONSTANT)
      

      Model code:

      #ifndef MODELREGISTRATION_H
      #define MODELREGISTRATION_H
      
      #include <QObject>
      #include <QAbstractListModel>
      
      struct RegistrationObject
      {
          RegistrationObject(int id, QString name, bool isDone) {
              m_id = id;
              m_name = name;
              m_isDone = isDone;
          }
          int m_id;
          QString m_name;
          bool m_isDone;
      };
      
      class ModelRegistration : public QAbstractListModel
      {
          Q_OBJECT
      public:
          ModelRegistration(QObject *parent = nullptr);
      
          enum DeviceRoles {
              IdRole = 0,
              NameRole,
              IsDoneRole
          };
      
          QVector<RegistrationObject> vectorDataRegistration;
      
          int rowCount(const QModelIndex& parent = QModelIndex()) const override;
          QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
      
      public slots:
          void emitSignalUpdateModel();
      
      protected:
          QHash<int, QByteArray> roleNames() const override;
      };
      
      #endif // MODELREGISTRATION_H
      
      
      #include "modelregistration.h"
      
      ModelRegistration::ModelRegistration(QObject *parent) : QAbstractListModel(parent)
      {
          vectorDataRegistration.append(RegistrationObject(1, "Legs (back)", false));
          vectorDataRegistration.append(RegistrationObject(2, "Hips (back)", false));
          vectorDataRegistration.append(RegistrationObject(3, "Back", false));
          vectorDataRegistration.append(RegistrationObject(3, "Arms (back)", false));
          vectorDataRegistration.append(RegistrationObject(3, "Hips (front)", false));
          vectorDataRegistration.append(RegistrationObject(4, "Belly", false));
          vectorDataRegistration.append(RegistrationObject(3, "Arms (front)", false));
      }
      
      int ModelRegistration::rowCount(const QModelIndex &parent) const
      {
          return vectorDataRegistration.count();
      }
      
      QVariant ModelRegistration::data(const QModelIndex &index, int role) const
      {
          if (index.row() < 0 || index.row() >= vectorDataRegistration.count())
              return QVariant();
      
          switch (role)
          {
          case IdRole:
              return vectorDataRegistration[index.row()].m_id;
          case NameRole:
              return vectorDataRegistration[index.row()].m_name;
          case IsDoneRole:
              return vectorDataRegistration[index.row()].m_isDone;
          }
      
          return QVariant();
      }
      
      void ModelRegistration::emitSignalUpdateModel()
      {
          auto index = this->index(0);
          emit dataChanged(index, index, QVector<int>{0,1,2});
      }
      
      QHash<int, QByteArray> ModelRegistration::roleNames() const
      {
          QHash<int, QByteArray> roles;
          roles[IdRole] = "id";
          roles[NameRole] = "name";
          roles[IsDoneRole] = "isDone";
          return roles;
      }
      
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        I do not see any setData reimplementation.
        Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.

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

        GrecKoG 1 Reply Last reply
        0
        • M Offline
          M Offline
          Mihaill
          wrote on last edited by
          #7

          But whot me need do? Need reimplement setData ?

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

            First: fix your DeviceRoles enumeration.

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

              I changed name from DeviceRoles to RoleNames. Whot me need still fix ?

              1 Reply Last reply
              0
              • M Mihaill

                But whot me need do? Need reimplement setData ?

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #10

                @Mihaill said in Using QAbstractModel:

                But whot me need do? Need reimplement setData ?

                You roles must starts with Qt::UserRole (cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)

                So change your enumeration to:

                    enum DeviceRoles {
                        IdRole = Qt::UserRole,
                        NameRole,
                        IsDoneRole
                    }
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mihaill
                  wrote on last edited by
                  #11

                  @KroMignon said in Using QAbstractModel:

                  Qt::UserRole

                  It's not help me. Whot me need still fix ?

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

                    Did you also fix emitSignalUpdateModel ?

                    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
                    • M Offline
                      M Offline
                      Mihaill
                      wrote on last edited by
                      #13

                      @SGaist said in Using QAbstractModel:

                      emitSignalUpdateModel

                      Whot me need doing with emitSignalUpdateModel?

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

                        Fix the roles values for example.

                        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
                        • M Offline
                          M Offline
                          Mihaill
                          wrote on last edited by
                          #15

                          I'm not understent whot you mean. Please write code example.

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

                            As I explained and @KroMignon showed you, your enum values were wrong. You hard coded the same values in that method:

                            @Mihaill said in Using QAbstractModel:

                            void ModelRegistration::emitSignalUpdateModel()
                            {
                                auto index = this->index(0);
                                emit dataChanged(index, index, QVector<int>{0,1,2});
                            }
                            
                            

                            Change them and use your enum values.

                            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
                            • M Offline
                              M Offline
                              Mihaill
                              wrote on last edited by
                              #17

                              It is not work, and this too not work ```
                              void ModelSchedule::emitSignalUpdateModel()
                              {
                              for (int q = 0; q < vectorDataRegistration.count(); q ++){
                              auto index = this->index(q);
                              QVector <int> roles;
                              for (int i = Qt::UserRole; i < Qt::UserRole + 3; i++) {
                              roles.append(i);
                              }
                              qDebug()<<"roles"<<roles;
                              emit dataChanged(index, index, roles);
                              }
                              }

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                I do not see any setData reimplementation.
                                Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.

                                GrecKoG Offline
                                GrecKoG Offline
                                GrecKo
                                Qt Champions 2018
                                wrote on last edited by
                                #18

                                @SGaist said in Using QAbstractModel:

                                I do not see any setData reimplementation.
                                Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.

                                @KroMignon said in Using QAbstractModel:

                                @Mihaill said in Using QAbstractModel:

                                But whot me need do? Need reimplement setData ?

                                You roles must starts with Qt::UserRole (cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)

                                So change your enumeration to:

                                    enum DeviceRoles {
                                        IdRole = Qt::UserRole,
                                        NameRole,
                                        IsDoneRole
                                    }
                                

                                This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.

                                As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?

                                SGaistS M 2 Replies Last reply
                                0
                                • GrecKoG GrecKo

                                  @SGaist said in Using QAbstractModel:

                                  I do not see any setData reimplementation.
                                  Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.

                                  @KroMignon said in Using QAbstractModel:

                                  @Mihaill said in Using QAbstractModel:

                                  But whot me need do? Need reimplement setData ?

                                  You roles must starts with Qt::UserRole (cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)

                                  So change your enumeration to:

                                      enum DeviceRoles {
                                          IdRole = Qt::UserRole,
                                          NameRole,
                                          IsDoneRole
                                      }
                                  

                                  This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.

                                  As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?

                                  SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @GrecKo said in Using QAbstractModel:

                                  This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.

                                  AFAIK, the engine will request the role names which shall be associated with integer values so that when requesting them, the model does know what to return. Thus these values are still important to properly set otherwise your model will start serving wrong information to the view.

                                  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
                                  • GrecKoG GrecKo

                                    @SGaist said in Using QAbstractModel:

                                    I do not see any setData reimplementation.
                                    Your DeviceRoles contains wrong values. These IDs are already in use by Qt in the Roles enum.

                                    @KroMignon said in Using QAbstractModel:

                                    @Mihaill said in Using QAbstractModel:

                                    But whot me need do? Need reimplement setData ?

                                    You roles must starts with Qt::UserRole (cf. https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass)

                                    So change your enumeration to:

                                        enum DeviceRoles {
                                            IdRole = Qt::UserRole,
                                            NameRole,
                                            IsDoneRole
                                        }
                                    

                                    This is not important when using a QML View, the views use the role names anyway and do no depend on the default Qt roles such as DisplayRole.

                                    As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?

                                    M Offline
                                    M Offline
                                    Mihaill
                                    wrote on last edited by
                                    #20

                                    @GrecKo said in Using QAbstractModel:

                                    As for @Mihaill , nowhere in your code is your underlying data changed. Where/how do you change it?

                                    I change data in QVector<RegistrationObject> vectorDataRegistration; and after use function emitSignalUpdateModel()

                                    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