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. Modify QAbstractListModel List from Qml
Qt 6.11 is out! See what's new in the release blog

Modify QAbstractListModel List from Qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 2 Posters 2.5k 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.
  • F Offline
    F Offline
    fireghostea
    wrote on last edited by
    #1

    this is my code

    devicemodel.h

    class Device
    {
    public:
        Device(const int &nodeId ,const QString &type, const int &lampVoltage);
    //![0]
    
        QString type() const;
        int lampVoltage() const;
        int nodeId() const;
    public:
        QString m_type;
        int m_lampVoltage;
        int m_nodeId;
    //![1]
    
    
    };
    
    class DeviceModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        enum DeviceRoles {
            NodeIdRole = Qt::UserRole + 1,
            TypeRole ,
            LampVoltageRole
    
        };
    
        DeviceModel(QObject *parent = 0);
    //![1]
    
        void addDevice(const Device &Device);
    
        int rowCount(const QModelIndex & parent = QModelIndex()) const;
    
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    
    protected:
        QHash<int, QByteArray> roleNames() const;
    private:
        QList<Device> m_Devices;
    //![2]
    
    public slots :
        void callFromQml(int index);
    
    };
    
    

    devicemodel.cpp

    Device::Device(const int &nodeId ,const QString &type, const int &lampVoltage)
        : m_nodeId(nodeId) , m_type(type), m_lampVoltage(lampVoltage)
    {
    }
    
    QString Device::type() const
    {
        return m_type;
    }
    
    int Device::nodeId() const
    {
        return m_nodeId;
    }
    
    int Device::lampVoltage() const
    {
        return m_lampVoltage;
    }
    
    DeviceModel::DeviceModel(QObject *parent)
        : QAbstractListModel(parent)
    {
    }
    
    
    
    void DeviceModel::callFromQml(int index){
    
       Device k= m_Devices.at(index);
       qDebug() << k.type() << k.lampVoltage();
       k.m_lampVoltage = 5;
        emit dataChanged(createIndex(index,0), createIndex(index,0), {0,1,2} );
    }
    
    void DeviceModel::addDevice(const Device &Device)
    {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_Devices << Device;
        endInsertRows();
    }
    
    int DeviceModel::rowCount(const QModelIndex & parent) const {
        Q_UNUSED(parent);
        return m_Devices.count();
    }
    
    QVariant DeviceModel::data(const QModelIndex & index, int role) const {
        if (index.row() < 0 || index.row() >= m_Devices.count())
            return QVariant();
    
        const Device &Device = m_Devices[index.row()];
        if (role == NodeIdRole)
            return Device.nodeId();
        else if (role == TypeRole)
            return Device.type();
        else if (role == LampVoltageRole)
            return Device.lampVoltage();
        return QVariant();
    }
    
    //![0]
    QHash<int, QByteArray> DeviceModel::roleNames() const {
        QHash<int, QByteArray> roles;
        roles[NodeIdRole] = "nodeId";
        roles[TypeRole] = "type";
        roles[LampVoltageRole] = "lampVoltage";
        return roles;
    }
    

    main.qml

    ListView {
        width: 200; height: 250
        spacing: 10
        model: myModel
        delegate:
            Text { text: "my values :: " + type + ", " + lampVoltage
    
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("egwegweg");
                    myModel.callFromQml(index);
                    //lampVoltage = 10;
    
                   // myModel.setData(index , 10 , 2);
                }
            }
        }
    }
    

    main.cpp

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        DeviceModel model;
        model.addDevice(Device(1, "Medium" , 200));
        model.addDevice(Device(1, "Medium" , 200));
        model.addDevice(Device(1, "Medium" , 200));
        QQmlContext *ctxt = engine.rootContext();
        ctxt->setContextProperty("myModel", &model);
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        model.addDevice(Device(1, "Medium" , 200));
        model.addDevice(Device(1, "Medium" , 200));
        model.addDevice(Device(1, "Medium" , 200));
    
        return app.exec();
    

    want modify my item values from Qml i wrote a slot named callFromQml and pass index of item from qml to c++ code and want update the values from it but can not do it i don not know emit signal works or not and do not know pass index to it correctly or not and don not

    E 1 Reply Last reply
    0
    • F fireghostea

      this is my code

      devicemodel.h

      class Device
      {
      public:
          Device(const int &nodeId ,const QString &type, const int &lampVoltage);
      //![0]
      
          QString type() const;
          int lampVoltage() const;
          int nodeId() const;
      public:
          QString m_type;
          int m_lampVoltage;
          int m_nodeId;
      //![1]
      
      
      };
      
      class DeviceModel : public QAbstractListModel
      {
          Q_OBJECT
      public:
          enum DeviceRoles {
              NodeIdRole = Qt::UserRole + 1,
              TypeRole ,
              LampVoltageRole
      
          };
      
          DeviceModel(QObject *parent = 0);
      //![1]
      
          void addDevice(const Device &Device);
      
          int rowCount(const QModelIndex & parent = QModelIndex()) const;
      
          QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
      
      protected:
          QHash<int, QByteArray> roleNames() const;
      private:
          QList<Device> m_Devices;
      //![2]
      
      public slots :
          void callFromQml(int index);
      
      };
      
      

      devicemodel.cpp

      Device::Device(const int &nodeId ,const QString &type, const int &lampVoltage)
          : m_nodeId(nodeId) , m_type(type), m_lampVoltage(lampVoltage)
      {
      }
      
      QString Device::type() const
      {
          return m_type;
      }
      
      int Device::nodeId() const
      {
          return m_nodeId;
      }
      
      int Device::lampVoltage() const
      {
          return m_lampVoltage;
      }
      
      DeviceModel::DeviceModel(QObject *parent)
          : QAbstractListModel(parent)
      {
      }
      
      
      
      void DeviceModel::callFromQml(int index){
      
         Device k= m_Devices.at(index);
         qDebug() << k.type() << k.lampVoltage();
         k.m_lampVoltage = 5;
          emit dataChanged(createIndex(index,0), createIndex(index,0), {0,1,2} );
      }
      
      void DeviceModel::addDevice(const Device &Device)
      {
          beginInsertRows(QModelIndex(), rowCount(), rowCount());
          m_Devices << Device;
          endInsertRows();
      }
      
      int DeviceModel::rowCount(const QModelIndex & parent) const {
          Q_UNUSED(parent);
          return m_Devices.count();
      }
      
      QVariant DeviceModel::data(const QModelIndex & index, int role) const {
          if (index.row() < 0 || index.row() >= m_Devices.count())
              return QVariant();
      
          const Device &Device = m_Devices[index.row()];
          if (role == NodeIdRole)
              return Device.nodeId();
          else if (role == TypeRole)
              return Device.type();
          else if (role == LampVoltageRole)
              return Device.lampVoltage();
          return QVariant();
      }
      
      //![0]
      QHash<int, QByteArray> DeviceModel::roleNames() const {
          QHash<int, QByteArray> roles;
          roles[NodeIdRole] = "nodeId";
          roles[TypeRole] = "type";
          roles[LampVoltageRole] = "lampVoltage";
          return roles;
      }
      

      main.qml

      ListView {
          width: 200; height: 250
          spacing: 10
          model: myModel
          delegate:
              Text { text: "my values :: " + type + ", " + lampVoltage
      
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      console.log("egwegweg");
                      myModel.callFromQml(index);
                      //lampVoltage = 10;
      
                     // myModel.setData(index , 10 , 2);
                  }
              }
          }
      }
      

      main.cpp

      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          DeviceModel model;
          model.addDevice(Device(1, "Medium" , 200));
          model.addDevice(Device(1, "Medium" , 200));
          model.addDevice(Device(1, "Medium" , 200));
          QQmlContext *ctxt = engine.rootContext();
          ctxt->setContextProperty("myModel", &model);
          engine.load(QUrl(QLatin1String("qrc:/main.qml")));
      
          model.addDevice(Device(1, "Medium" , 200));
          model.addDevice(Device(1, "Medium" , 200));
          model.addDevice(Device(1, "Medium" , 200));
      
          return app.exec();
      

      want modify my item values from Qml i wrote a slot named callFromQml and pass index of item from qml to c++ code and want update the values from it but can not do it i don not know emit signal works or not and do not know pass index to it correctly or not and don not

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @fireghostea I think you should use index() instead of createIndex().

      F 1 Reply Last reply
      0
      • E Eeli K

        @fireghostea I think you should use index() instead of createIndex().

        F Offline
        F Offline
        fireghostea
        wrote on last edited by
        #3

        @Eeli-K base on documentation type of firs parameter is Qmodelindex

        E 1 Reply Last reply
        0
        • F fireghostea

          @Eeli-K base on documentation type of firs parameter is Qmodelindex

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @fireghostea Yes, but did you check what index(int) returns?

          F 1 Reply Last reply
          0
          • E Eeli K

            @fireghostea Yes, but did you check what index(int) returns?

            F Offline
            F Offline
            fireghostea
            wrote on last edited by
            #5

            @Eeli-K yes
            does not compile

            E 1 Reply Last reply
            0
            • F fireghostea

              @Eeli-K yes
              does not compile

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @fireghostea What's the error message?

              F 2 Replies Last reply
              0
              • E Eeli K

                @fireghostea What's the error message?

                F Offline
                F Offline
                fireghostea
                wrote on last edited by
                #7

                @Eeli-K no matching function for call to ‘DeviceModel::dataChanged(int&, int&, <brace-enclosed initializer list>)’
                emit dataChanged(index, index, {0,1,2} );
                ^

                F 1 Reply Last reply
                0
                • F fireghostea

                  @Eeli-K no matching function for call to ‘DeviceModel::dataChanged(int&, int&, <brace-enclosed initializer list>)’
                  emit dataChanged(index, index, {0,1,2} );
                  ^

                  F Offline
                  F Offline
                  fireghostea
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • E Eeli K

                    @fireghostea What's the error message?

                    F Offline
                    F Offline
                    fireghostea
                    wrote on last edited by
                    #9

                    @Eeli-K i find out my problem with index is just because my parameter name is index
                    i rename it and my code now is

                    Device *k= &m_Devices[inxd];
                    qDebug() << k->type() << k->lampVoltage();
                    k->m_lampVoltage = 5;
                     emit dataChanged(index(inxd), index(inxd), {0,1,2} );
                    

                    my parameter name is "inxd"

                    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