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

QAbstractListModel in QTableView



  • 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!



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



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


Log in to reply