Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMap and data model.



  • Hello. I wanted to know if it is possible to create a QAbstractItemList model with a QMap storing objects data. I'm trying to do it and have problems to implement functions data and setData(). Anyone can help?
    For example

    //#ifndef CANMESSAGEMODEL_H
    #define CANMESSAGEMODEL_H
    
    #include "cancontroller.h"
    
    class CanMessageModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource NOTIFY dataSourceChanged)
    
        enum CanMessageDetails{
            idRole=Qt::UserRole,
            dataRole=Qt::UserRole+1,
            sizeRole=Qt::UserRole+2,
            timeRole=Qt::UserRole+3
        };
    public:
        explicit CanMessageModel(QObject *parent=nullptr);
        int rowCount(const QModelIndex &index=QModelIndex()) const;
        QVariant data(const QModelIndex &index,int role=Qt::DisplayRole) const;
        Qt::ItemFlags flags(const QModelIndex &index) const;
        bool setData(const QModelIndex &index, const QVariant &value,int role);
        void setDataSource(CanController * dataSource);
        QHash<int,QByteArray> roleNames() const;
        CanController *dataSource() const;
    
    signals:
    
        void dataSourceChanged(CanController* dataSource);
    
    private:
         QMap<QString,CanMessage*> m_canMessages;
        CanController *m_dataSource;
        bool m_signalConnected;
    };
    //Q_DECLARE_METATYPE(CanMessageModel)
    #endif // CANMESSAGEMODEL_H
    
    


  • Hey,
    Could you please explain more about your problem?
    You can have your own setdata and getdata to set and get data that's enough to emit the right signals to refresh new data.



  • @Alien I'm trying to create a model of data based on a QMap. The problem is i have difficulties to acess the index of an objects in the QMap. Therefore i have difficulties implementing my data and setData functions below:

    //QVariant CanMessageModel::data(const QModelIndex &index, int role) const
    {
        if (index.row() < 0 || index.row() >= m_dataSource->dataItems().size())
            return QVariant();
        CanMessage *message= m_dataSource->dataItems().at(index.row());
        if(role == idRole)
            return message->getID();
        else if(role == dataRole)
            return message->getData();
        else if(role == sizeRole)
            return message->getLength();
        else if(role == timeRole)
            return message->getTimeStamp();
        return QVariant();
    }
    


  • @Babs you've done that; what's the difficulty?
    And i don't know why you define
    Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource NOTIFY dataSourceChanged)

    CanController should be a qvariant is that works fine? In the other hand why you need Q_PROPERTY?



  • @Alien this is not working at all this is the function i wrote for a QList<CanMessage*>. Now i'm trying to do it with a QMap


  • Lifetime Qt Champion

    Hi,

    Unless you are mapping an int to your CanMessage*, I don't see how you expect to use a QMap in a list model.



  • @SGaist My CanMessage class contain a lot of information like the ID of my data. I don't want to add to my Model message wich ID is already present. I want to refresh it. That's the reason why i try to use QMap::insert() in order to not add messages with existing identifiers


  • Lifetime Qt Champion

    What type is ID ?



  • @SGaist quint32. I use QCanBusFrame to get my data


  • Lifetime Qt Champion

    And you would like to show them ordered by that ID ?



  • @SGaist not necessary i just want to replace messages with existing ID with new one. I don't want to add them to my list



  • @SGaist I created my list model based on Qmap. The problem know is when my data update i get the error: Unable to assign [undefined] to QString.
    Here is what i get:
    0_1551347754150_qmap.PNG

    When a new message with existing ID is received the data should be modified in my display.
    The problem is the first received message is displayed and when messages are updated is says:
    Unable to assign [undefined] to QString.

    For example in first line i have 2ff as ID, and 2A as data. If i receive a new message with the ID 2ff and data 2B, 2A should be replaced with 2B in that line


  • Lifetime Qt Champion

    Can you show how you implemented setData ?



  • @SGaist of course

    //your code here
    bool CanMessageModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        CanMessage *message= m_dataSource->dataItems().values().at(index.row());
        //    CanMessage *message= m_dataSource->dataItems().at(index.row());
        //CanMessage *message= (m_dataSource->dataItems().constBegin()+index.row()).value();
    
    
        bool somethingChanged=false;
    
        switch (role) {
        case idRole:{
            if(message->getID()!=value.toString()){
                message->setID(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case dataRole:{
            if(message->getData()!=value.toString()){
                message->setData(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case sizeRole:{
            if(message->getLength()!=value.toString()){
                message->setLength(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case timeRole:{
            if(message->getTimeStamp()!=value.toUInt()){
                message->setTimeStamp(value.toUInt());
                somethingChanged=true;
    
            }
        }
            break;
        case periodRole:{
            if(message->getPeriod()!=value.toUInt())
                message->setPeriod(value.toUInt());
            somethingChanged=true;
        }
    
    
        }
        if(somethingChanged){
            emit dataChanged(index,index,QVector<int>()<<role);
            return true;
        }
        return false;
    
    }
    
    

  • Lifetime Qt Champion

    Something is a bit strange here...

    You want to order your message by idea, yet there's no real sign of a list to order. You seem to have a pretty convoluted setup right now.



  • @SGaist thank you for your answers. I successed in creating my model like i wanted. I used a QAbstractTable model and stored my data in a QMap.


  • Lifetime Qt Champion

    So everything is working now ?



  • @SGaist Yes


  • Lifetime Qt Champion

    What did you do to fix your issue ?

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)



  • @SGaist here is my code
    I created a QAbstractListModel based on tableView

    //your code here
    #ifndef CANMESSAGEMODEL_H
    #define CANMESSAGEMODEL_H
    
    #include "cancontroller.h"
    #include "canmessage.h"
    
    class CanMessageModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(CanController* dataSource READ dataSource WRITE setDataSource )
    
        enum CanMessageDetails{
            idRole=Qt::UserRole,
            dataRole=Qt::UserRole+1,
            sizeRole=Qt::UserRole+2,
            timeRole=Qt::UserRole+3,
            periodRole=Qt::UserRole+4,
            nodeIDRole=Qt::UserRole+5,
            fctTypeRole=Qt::UserRole+6
        };
    public:
        explicit CanMessageModel(QObject *parent=nullptr);
        int rowCount(const QModelIndex &index=QModelIndex()) const;
        QVariant data(const QModelIndex &index,int role=Qt::DisplayRole) const;
        Qt::ItemFlags flags(const QModelIndex &index) const;
        bool setData(const QModelIndex &index, const QVariant &value,int role);
        Q_INVOKABLE void setDataSource(CanController * dataSource);
        QHash<int,QByteArray> roleNames() const;
        CanController *dataSource() const;
    
    signals:
    
    
    private:
        // QList<CanMessage*> m_canMessages;
        CanController *m_dataSource;
        bool m_signalConnected;
    };
    //Q_DECLARE_METATYPE(CanMessageModel)
    #endif // CANMESSAGEMODEL_H
    
    
    //model.cpp
    #include <QDebug>
    #include "canmessagemodel.h"
    
    CanMessageModel::CanMessageModel(QObject *parent):QAbstractListModel (parent),m_signalConnected(false)
    {
        setDataSource(new CanController(this));
    }
    
    int CanMessageModel::rowCount(const QModelIndex &index) const
    {
        Q_UNUSED(index)
        return m_dataSource->dataItems().size();
    }
    
    
    QVariant CanMessageModel::data(const QModelIndex &index, int role) const
    {
        Q_UNUSED(role)
        if (index.row() < 0 || index.row() >= m_dataSource->dataItems().size())
            return QVariant();
        CanMessage *message= m_dataSource->dataItems().values().at(index.row());
    
        if(role == idRole)
            return message->getID();
        else if(role == dataRole)
            return message->getData();
        else if(role == sizeRole)
            return message->getLength();
        else if(role == timeRole)
            return message->getTimeStamp();
        else if(role == periodRole)
            return message->getPeriod();
        else if(role ==nodeIDRole)
            return message->nodeId();
        else if(role ==fctTypeRole)
            return message->fct_Type();
        return QVariant();
    }
    
    
    
    Qt::ItemFlags CanMessageModel::flags(const QModelIndex &index) const
    {
        if (!index.isValid())
            return Qt::NoItemFlags;
        return Qt::ItemIsEditable;
    }
    
    QHash<int, QByteArray> CanMessageModel::roleNames() const
    {
    
        QHash<int, QByteArray> roles;
        roles[idRole] = "identifier";
        roles[dataRole] = "payload";
        roles[sizeRole] = "size";
        roles[timeRole] = "timeStamp";
        roles[periodRole]="period";
        roles[nodeIDRole]="nodeID";
        roles[fctTypeRole]="functionType";
        return roles;
    }
    
    bool CanMessageModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        CanMessage *message= m_dataSource->dataItems().values().at(index.row());
    
        bool somethingChanged=false;
    
        switch (role) {
        case idRole:{
            if(message->getID()!=value.toString()){
                message->setID(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case dataRole:{
            if(message->getData()!=value.toString()){
                message->setData(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case sizeRole:{
            if(message->getLength()!=value.toString()){
                message->setLength(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case timeRole:{
            if(message->getTimeStamp()!=value.toString()){
                message->setTimeStamp(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case periodRole:{
            if(message->getPeriod()!=value.toString()){
                message->setPeriod(value.toString());
                somethingChanged=true;
    
            }
        }
            break;
        case nodeIDRole:{
            if(message->nodeId()!=value.toString()){
                message->setnodeId(value.toString());
                somethingChanged=true;
            }
        }
            break;
        case fctTypeRole:{
            if(message->fct_Type()!=value.toString()){
                message->setFct_Type(value.toString());
                somethingChanged=true;
            }
        }
    
        }
        if(somethingChanged){
            emit dataChanged(index,index,QVector<int>()<<role);
            return true;
        }
        return false;
    
    }
    
    CanController *CanMessageModel::dataSource() const
    {
        return m_dataSource;
    }
    
    
    
    void CanMessageModel::setDataSource(CanController *dataSource)
    {
        beginResetModel();
        if(m_dataSource && m_signalConnected)
            m_dataSource->disconnect(this);
        m_dataSource=dataSource;
    
        connect(m_dataSource,&CanController::preMessageAdded,this,[=](){
            const int index=m_dataSource->dataItems().size();
            beginInsertRows(QModelIndex(),index,index);
        });
        connect(m_dataSource,&CanController::postMessageAdded,this,[=](){
            endInsertRows();
        });
        connect(m_dataSource,&CanController::preMessageSent,this,[=](){
            const int index=m_dataSource->dataItems().size();
            beginInsertRows(QModelIndex(),index,index);
        });
        connect(m_dataSource,&CanController::postMessageSent,this,[=](){
            endInsertRows();
        });
        m_signalConnected=true;
        endResetModel();
    }
    
    
    

    And it qml i create my Tableview with following delegate

    //your code here
    import QtQuick 2.12
    import QtQuick.Controls 2.12
    
    
    
    
    
    Column{
    
        TableView{
            implicitHeight: 200
            id : mListView
            width: parent.width
            height: parent.height
    
            model : myModel
            delegate: Item {
                height: parent.height
    
                width: parent.width
                Row{
                    //spacing: 0
                    anchors.fill: parent
                    //anchors.margins:0
                    TextField{
                        focus: true
                        text: model.identifier;
                        //wrapMode: Text.WordWrap
                        //canUndo: true
    
                    }
    
                    TextField{
    
                        focus: true
                        id : m_payload
                        text: model.payload
    
                        // canUndo:  true
                    }
                    TextField{
                        focus: true
                        id:m_length
                        text: model.size
                    }
                    TextField{
                        focus: true
                        text: model.timeStamp
                    }
                    TextField{
                        focus:true
                        text: model.period
                    }
                    TextField{
                        focus:true
                        text: model.nodeID
                    }
                    TextField{
                        focus:true
                        text: model.functionType
                    }
    
                }
    
            }
    
        }
    
    }
    
    

Log in to reply