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. QAbstractListModel in QTableView
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 797 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by
    #1

    Hi,
    i have a subclassed QAbstractListModel which i use in QML with a ListView. My list model has different roles to use them in QML. Now I want to use the same model for a QTableView, but I have absolutely no idea how the tell the QListView to use wich role for which column.

    I don't want to implement a new QAbstractTableModel.

    Is this possible? Thanks!

    JonBJ 1 Reply Last reply
    1
    • beeckscheB beecksche

      Hi,
      i have a subclassed QAbstractListModel which i use in QML with a ListView. My list model has different roles to use them in QML. Now I want to use the same model for a QTableView, but I have absolutely no idea how the tell the QListView to use wich role for which column.

      I don't want to implement a new QAbstractTableModel.

      Is this possible? Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @beecksche said in QAbstractListModel in QTableView:

      but I have absolutely no idea how the tell the QListView to use wich role for which column

      I admit I know nothing about QML, but what do you mean by "using roles for columns"? You don't use roles for columns. You use roles for context actions, e.g. value of data, color of data, etc.

      beeckscheB 1 Reply Last reply
      0
      • JonBJ JonB

        @beecksche said in QAbstractListModel in QTableView:

        but I have absolutely no idea how the tell the QListView to use wich role for which column

        I admit I know nothing about QML, but what do you mean by "using roles for columns"? You don't use roles for columns. You use roles for context actions, e.g. value of data, color of data, etc.

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @JonB

        I built my model from this examples .

        This is my model:

        #include <QAbstractListModel>
        
        #include "hmaterial.h"
        
        class HMaterialModel : public QAbstractListModel
        {
            Q_OBJECT
        
        public:
            enum {
                MaterialName = Qt::UserRole,
                MaterialID
            };
        
            explicit HMaterialModel(QObject *parent = nullptr);
        
            int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        
            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        
            virtual QHash<int, QByteArray> roleNames() const override;
        
            const QVector<HMaterial> &materials() const;
            void setMaterials(const QVector<HMaterial> &materials);
        
        private:
            QVector<HMaterial> m_materials;
        };
        
        #include "hmaterialmodel.h"
        
        HMaterialModel::HMaterialModel(QObject *parent)
            : QAbstractListModel(parent)
        {}
        
        int HMaterialModel::rowCount(const QModelIndex &parent) const
        {
            return m_materials.size();
        }
        
        QVariant HMaterialModel::data(const QModelIndex &index, int role) const
        {
            if (!index.isValid() || m_materials.isEmpty())
                return QVariant();
        
        
            switch (role) {
            case HMaterialModel::MaterialName:
                return QVariant(m_materials.at(index.row()).name());
            case HMaterialModel::MaterialID:
                return QVariant(m_materials.at(index.row()).id());
            }
        
            return QVariant();
        }
        
        QHash<int, QByteArray> HMaterialModel::roleNames() const
        {
            QHash<int, QByteArray> names;
            names[MaterialName] = "name";
            names[MaterialID] = "materialID";
        
            return names;
        }
        
        const QVector<HMaterial>& HMaterialModel::materials() const
        {
            return m_materials;
        }
        
        void HMaterialModel::setMaterials(const QVector<HMaterial> &materials)
        {
            beginResetModel();
            m_materials = materials;
            endResetModel();
        }
        

        In QML:

        ComboBox {
            model: materialModel
            textRole: name
        }
        

        EDIT:

        I changed and added my model to:

        QVariant HMaterialModel::data(const QModelIndex &index, int role) const
        {
            if (!index.isValid() || m_materials.isEmpty())
                return QVariant();
        
            if (role == Qt::DisplayRole)
                role = index.column() + Qt::UserRole;
        
            switch (role) {
            case HMaterialModel::MaterialName:
                return QVariant(m_materials.at(index.row()).name());
            case HMaterialModel::MaterialID:
                return QVariant(m_materials.at(index.row()).id());
            }
        
            return QVariant();
        }
        
        int HMaterialModel::columnCount(const QModelIndex &parent) const
        {
            return 2;
        }
        
        QVariant HEventModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
            if (role == Qt::DisplayRole)
            {
                if (orientation == Qt::Horizontal) {
                    switch (section)
                    {
                    case 0:
                        return QString("Name");
                    case 1:
                        return QString("Material-ID");
                    }
                }
            }
            return QVariant();
        }
        

        Which seems to work!

        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