Certainly! Below is a basic example of a new KTreeModel class inheriting from QAbstractItemModel, which holds KTreeNode items that support multiple columns. The implementation will be split into header (.h) and source (.cpp) files.
ktreenode.h
#ifndef KTREENODE_H
#define KTREENODE_H
#include <QVector>
#include <QVariant>
class KTreeNode
{
public:
explicit KTreeNode(const QVector<QVariant> &data, KTreeNode *parent = nullptr);
~KTreeNode();
void appendChild(KTreeNode *child);
KTreeNode *child(int row) const;
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
KTreeNode *parent();
void removeChild(int row);
private:
QVector<KTreeNode*> m_children;
QVector<QVariant> m_itemData;
KTreeNode *m_parent;
};
#endif // KTREENODE_H
ktreenode.cpp
#include "ktreenode.h"
KTreeNode::KTreeNode(const QVector<QVariant> &data, KTreeNode *parent)
: m_itemData(data), m_parent(parent)
{
}
KTreeNode::~KTreeNode()
{
qDeleteAll(m_children);
}
void KTreeNode::appendChild(KTreeNode *child)
{
m_children.append(child);
}
KTreeNode *KTreeNode::child(int row) const
{
if(row < 0 || row >= m_children.size())
return nullptr;
return m_children.at(row);
}
int KTreeNode::childCount() const
{
return m_children.count();
}
int KTreeNode::columnCount() const
{
return m_itemData.count();
}
QVariant KTreeNode::data(int column) const
{
if(column < 0 || column >= m_itemData.size())
return QVariant();
return m_itemData.at(column);
}
int KTreeNode::row() const
{
if(m_parent)
return m_parent->m_children.indexOf(const_cast<KTreeNode*>(this));
return 0;
}
KTreeNode *KTreeNode::parent()
{
return m_parent;
}
void KTreeNode::removeChild(int row)
{
if(row < 0 || row >= m_children.size())
return;
delete m_children.takeAt(row);
}
ktree_model.h
#ifndef KTREE_MODEL_H
#define KTREE_MODEL_H
#include <QAbstractItemModel>
#include "ktreenode.h"
class KTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit KTreeModel(const QVector<QString> &headers, QObject *parent = nullptr);
~KTreeModel();
QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
// Custom utility functions
KTreeNode *getNode(const QModelIndex &index) const;
void setupModelData(const QVector<QVector<QVariant>> &data);
private:
KTreeNode *m_rootNode;
QVector<QString> m_headers;
};
#endif // KTREE_MODEL_H
ktree_model.cpp
#include "ktree_model.h"
#include <QDebug>
KTreeModel::KTreeModel(const QVector<QString> &headers, QObject *parent)
: QAbstractItemModel(parent),
m_headers(headers)
{
QVector<QVariant> rootData;
for(const auto &header : headers) {
rootData.append(header);
}
m_rootNode = new KTreeNode(rootData);
}
KTreeModel::~KTreeModel()
{
delete m_rootNode;
}
int KTreeModel::columnCount(const QModelIndex &parent) const
{
if(parent.isValid())
return static_cast<KTreeNode*>(parent.internalPointer())->columnCount();
return m_rootNode->columnCount();
}
int KTreeModel::rowCount(const QModelIndex &parent) const
{
KTreeNode *parentNode;
if (!parent.isValid())
parentNode = m_rootNode;
else
parentNode = static_cast<KTreeNode*>(parent.internalPointer());
return parentNode->childCount();
}
QModelIndex KTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
KTreeNode *parentNode;
if(!parent.isValid())
parentNode = m_rootNode;
else
parentNode = static_cast<KTreeNode*>(parent.internalPointer());
KTreeNode *childNode = parentNode->child(row);
if(childNode)
return createIndex(row, column, childNode);
else
return QModelIndex();
}
QModelIndex KTreeModel::parent(const QModelIndex &index) const
{
if(!index.isValid())
return QModelIndex();
KTreeNode *childNode = static_cast<KTreeNode*>(index.internalPointer());
KTreeNode *parentNode = childNode->parent();
if(parentNode == m_rootNode || parentNode == nullptr)
return QModelIndex();
return createIndex(parentNode->row(), 0, parentNode);
}
QVariant KTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
KTreeNode *node = static_cast<KTreeNode*>(index.internalPointer());
return node->data(index.column());
}
Qt::ItemFlags KTreeModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
QVariant KTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section < m_headers.size())
return m_headers.at(section);
return QVariant();
}
bool KTreeModel::insertRows(int row, int count, const QModelIndex &parent)
{
KTreeNode *parentNode = getNode(parent);
beginInsertRows(parent, row, row + count - 1);
for(int i = 0; i < count; ++i) {
QVector<QVariant> emptyData(m_rootNode->columnCount());
KTreeNode *newNode = new KTreeNode(emptyData, parentNode);
parentNode->appendChild(newNode);
}
endInsertRows();
return true;
}
bool KTreeModel::removeRows(int row, int count, const QModelIndex &parent)
{
KTreeNode *parentNode = getNode(parent);
if(row < 0 || (row + count) > parentNode->childCount())
return false;
beginRemoveRows(parent, row, row + count - 1);
for(int i=0; i < count; ++i) {
parentNode->removeChild(row);
}
endRemoveRows();
return true;
}
KTreeNode *KTreeModel::getNode(const QModelIndex &index) const
{
if(index.isValid()) {
KTreeNode *node = static_cast<KTreeNode*>(index.internalPointer());
if(node)
return node;
}
return m_rootNode;
}
void KTreeModel::setupModelData(const QVector<QVector<QVariant>> &data)
{
// Clear old children
beginResetModel();
delete m_rootNode;
QVector<QVariant> headerData;
for(const auto &s : m_headers)
headerData.append(s);
m_rootNode = new KTreeNode(headerData);
for(const auto &rowData : data) {
KTreeNode *node = new KTreeNode(rowData, m_rootNode);
m_rootNode->appendChild(node);
}
endResetModel();
}
Usage Notes
KTreeNode holds its column data and children and knows its parent.
KTreeModel is a tree model with multiple columns.
- The model supports basic operations with tree navigation (
index(), parent(), rowCount(), columnCount(), data(), etc.).
- Insert/remove rows are implemented with minimal logic (simplified, you might want to expand that).
- Edit support requires overriding
setData(), which you can add as needed.
- You can extend
setupModelData() for more complex population from nested data if needed.
If you want me to add editing support or other features like drag/drop, just ask!