Modify QAbstractListModel List from Qml



  • 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



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



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



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



  • @Eeli-K yes
    does not compile



  • @fireghostea What's the error message?



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



  • This post is deleted!


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.