Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tableview QML not displaying data from QAbstractTableModel
Qt 6.11 is out! See what's new in the release blog

Tableview QML not displaying data from QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 322 Views
  • 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.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by
    #1

    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

    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