Basically like in this tutorial: https://doc.qt.io/qt-6/modelview.html#2-1-a-read-only-table
39 lines
940 B
C++
39 lines
940 B
C++
#include "tablemodel.h"
|
|
|
|
TableModel::TableModel(QObject* parent)
|
|
: QAbstractTableModel{parent} {}
|
|
|
|
int TableModel::rowCount(const QModelIndex& parent) const { return 5; }
|
|
|
|
int TableModel::columnCount(const QModelIndex& parent) const { return 5; }
|
|
|
|
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|
const int row = index.row();
|
|
const int column = index.column();
|
|
|
|
if (!index.isValid()) {
|
|
return QVariant();
|
|
}
|
|
if (row >= 5 || column >= 5) {
|
|
return QVariant();
|
|
}
|
|
|
|
switch (role) {
|
|
case Qt::DisplayRole:
|
|
return QString("Data %1/%2").arg(row).arg(column);
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
if (role == Qt::DisplayRole) {
|
|
if (orientation == Qt::Horizontal) {
|
|
return QString("Section %1").arg(section);
|
|
} else {
|
|
return QString("%1").arg(section);
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|