0
votes

Im trying to get my first QML TableView to work in Qt 5.2 (since we are stuck on that version right now at work) using a QAbstractTableModel on the backend.

My main issue is that for some reason the itemDelegate is never firing so I never see anything in the View except the outline of the TableView. I have also verified that theData_ is filled with 2 dimensional numbers in every row/column in the constructor and I do an emit layoutChanged() as well as an emit dataChanged() in the constructor.

I realize I have no error checking for an invalid QModelIndex in the data() call at this time.

I also did not implement index() at all.

Also is there any need to use a ROLE here?

The data Im displaying is a single integer (as a QString) per cell, nothing more at this time.

Thanks for your help

qml:

TableView {
  width: 600
  height: 600

  model: myModel
  visible: true

  itemDelegate: Rectangle {
     color: "lightgray"
     width: 100
     height: 20

     Text {
        text: styleData.value
        color: "black"
     }
   }
}

relevant code from subclassed QAbstractTableModel:

int MyModel::rowCount(const QModelIndex&) const
{
return 10;
}

int MyModel::columnCount(const QModelIndex&) const
{
return 3;
}

QVariant MyModel::data(const QModelIndex& index, int role) const
{
const int row = index.row();
const int col = index.column();

return QString("%1").arg(this->theData_[col][row]);
}
1

1 Answers

0
votes

Before Qt 5.12 there was only a TableView component that belongs to Qt QuickControl 1 that only supports a list type model where each column reflects the information of a role so this is probably your problem since you have not created any TableViewColumn. On the other hand, as of >= Qt5.12, another TableView already exists, which it supports as a table type model.

mymodel.h

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractListModel>

struct Data{
    int number;
    QString text;
};

class MyModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum CustomRoles{
        NumberRole = Qt::UserRole,
        TextRole
    };
    explicit MyModel(QObject *parent = nullptr);
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;
private:
    QList<Data> m_data;
};

#endif // MYMODEL_H

mymodel.cpp

#include "mymodel.h"

MyModel::MyModel(QObject *parent)
    : QAbstractListModel(parent)
{
    // for test
    for(int i=0; i< 10; i++){
        Data d{i, QString::number(i)};
        m_data.push_back(d);
    }
}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_data.count();
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    if(role == NumberRole)
        return m_data.at(index.row()).number;
    if(role == TextRole)
        return m_data.at(index.row()).text;
    return QVariant();
}

QHash<int, QByteArray> MyModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NumberRole] = "number";
    roles[TextRole] = "text";
    return roles;
}

main.qml

// ...
TableView {
    width: 600
    height: 600

    model: myModel
    visible: true

    TableViewColumn {
        role: "number"
        title: "Number"
        width: 100
    }
    TableViewColumn {
        role: "text"
        title: "Text"
        width: 200
    }

    itemDelegate: Rectangle {
        color: "lightgray"
        width: 100
        height: 20

        Text {
            text: styleData.value
            color: "black"
        }
    }
}
// ...