#include "MyTestTree.h" #include const int MyTestTree::NUM_COLUMNS = 15; MyTestTree::MyTestTree(QObject *parent) : QAbstractItemModel(parent) { } int MyTestTree::rowCount(const QModelIndex &parent) const { if(parent.column() > 0) return 0; // Only one level if(!parent.isValid()) return 1; return 0; } int MyTestTree::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return NUM_COLUMNS; } QVariant MyTestTree::headerData(int section, Qt::Orientation orientation, int role) const { if(orientation == Qt::Horizontal && role == Qt::DisplayRole) { return QString::number(section); } return QVariant(); } QVariant MyTestTree::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); if(role == Qt::DisplayRole) { return QString("(%1,%2)").arg(index.row()).arg(index.column()); } return QVariant(); } QModelIndex MyTestTree::index(int row, int column, const QModelIndex &parent) const { if(!hasIndex(row, column, parent)) return QModelIndex(); if(!parent.isValid()) { return createIndex(row, column); } return QModelIndex(); } QModelIndex MyTestTree::parent(const QModelIndex &index) const { if(!index.isValid()) return QModelIndex(); return createIndex(0, 0); }