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

Tableview QML not displaying data from QAbstractTableModel



  • Hi

    I have been trying to add a table in QML based on the QAbstractTableModel class. The table view is not displaying any data. This is my model in C++.

    paramModel.h

    #ifndef PARAMMODEL_H
    #define PARAMMODEL_H
    
    ///
    /// \brief The paramModel class
    ///
    
    /*
     * Param table columns:
     * Index
     * Param NAME
     * Param Value
    */
    
    #include <QAbstractTableModel>
    #include<QVariantMap>
    #include<QMap>
    #include<QDebug>
    #include <QObject>
    
    class paramModel :public QAbstractTableModel
    {
        Q_OBJECT
    
        enum Role {
            IndexRole=Qt::UserRole +1,
            NameRole,
            ValueRole
        };
    public:
        explicit paramModel(QObject *parent = nullptr);
        int rowCount(const QModelIndex &parent)const override;
        int columnCount(const QModelIndex &parent) const override;
        QVariant data(const QModelIndex &index, int role) const override;
        void setParams(QMap<int, QVariantMap> paramList);
    //    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    
        QHash<int, QByteArray> roleNames() const override;
    
    
    
    //    void addParam(int paramID, QMap<QString,QVariant> param);
    //    void removeParam(int row);
    //    bool setData(const QModelIndex &index, const QVariant &value, int role);
    private:
        QMap<int, QVariantMap> _paramsList;
    };
    
    #endif // PARAMMODEL_H
    
    
    
    

    paramModel.cpp

    #include "parammodel.h"
    
    paramModel::paramModel(QObject *parent) :
        QAbstractTableModel(parent)
    {
    
    }
    
    int paramModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return _paramsList.size();
    
    }
    
    int paramModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return 3;
    
    }
    
    QVariant paramModel::data(const QModelIndex &index, int role) const
    {
        qDebug() << __func__;
    
        if(!index.isValid())
            return QVariant();
    
        if(index.row() >= _paramsList.size() || index.row() < 0)
            return QVariant();
    
        QMap<QString,QVariant> m = _paramsList[index.row()];
        QMapIterator<QString, QVariant> iter(m);
        iter.next();
        switch (role) {
        case IndexRole:
            return index.row();
        case NameRole:
    
            return  iter.key();
    
        case ValueRole:
    
            return  iter.value();
        default:
            qDebug() << "in data default";
            return QVariant();
    
    
        }
    }
    
    void paramModel::setParams(QMap<int, QVariantMap> paramList)
    {
        _paramsList = paramList;
    
    }
    
    
    QHash<int, QByteArray> paramModel::roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[IndexRole] = "index";
        roles[NameRole] = "paramName";
        roles[ValueRole] = "paramValue";
        return roles;
    }
    
    
    
    

    Params.qml

    import QtQuick 2.14
    import GCS.ScreenController 1.0
    import ParamModel 1.0  //have registered this model
    import '../QML'
    
    TableView{
    
        //    anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        clip: true
    
    
        model:  vehicle.paramModel
    
        delegate :
            Rectangle {
            implicitWidth: 100
            implicitHeight: 50
            border.width: 1
    
            Text {
                text: display
                anchors.centerIn: parent
            }
        }
    }
    
    

    paramModel attached in QML is a QProperty in vehicle class.
    I have not used QML much and will be very thankful for any suggestions.

    Thanks


Log in to reply