I'm trying to implement a model in which different first-level parents have a child tables of different sizes. In this code there is only one first level parent index and child table of 8 by 8, and the application when displaying clearly buggy and slow, although the data is correct. What am I doing wrong?
testqtmodel.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testqtmodel
TEMPLATE = app
SOURCES += main.cpp
main.cpp
#include <QApplication>
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QTableView>
class TreeModel : public QAbstractItemModel
{
public:
explicit TreeModel(QObject *parent = 0)
: QAbstractItemModel(parent)
{
for (int i = 0; i < 8; ++i)
for (int j = 0; j < 8; ++j)
foo[i][j] = i + j;
}
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE
{
if (!index.isValid())
retu QVariant();
if (role != Qt::DisplayRole)
retu QVariant();
if (index.intealId() == 0)
retu QVariant();
if (index.intealId() == 1)
retu foo[index.row()][index.column()];
}
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
if (!parent.isValid())
retu createIndex(row, column, (quint64)0);
else
retu createIndex(row, column, (quint64)1);
}
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE
{
if (index.intealId() == 0)
retu QModelIndex();
if (index.intealId() == 1)
retu createIndex(0, 0, (quint64)0);
}
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
if (!parent.isValid())
retu 1;
if (parent.intealId() == 0)
retu 8;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
if (!parent.isValid())
retu 1;
if (parent.intealId() == 0)
retu 8;
}
private:
int foo[8][8];
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView *v = new QTableView();
TreeModel *m = new TreeModel();
v->setModel(m);
v->setRootIndex(m->index(1, 1));
v->show();
retu a.exec();
}
