Tree view与Tree Widget 相比而言,需要为tree view 设置一个model。使用tree view 能有效降低内存的使用率。但是需要自定义model。下面参考Qt官方提供的demo,讨论如何自定义Tree View Model。
依据名为simple tree model官方demo可知道:要自定义tree view model需要定义一个树形结构的底层数据结构,该类型结构的每一个对象对应一个view中的Item(如下图每个正方形表示一个Item)。每个对象保存自己的父Item指针、子Item指针列表、数据段列表。自定义的tree model包含一个root Item,以该Item为根建立起item树。对于view中的Item都有与之关联的QModelIndex,可以通过index获取该Item的位置(row)、父Item的index以及底层数据的指针等信息。
在 tree model中顶级项(上图中的 A、C以及同级的Item)对应的模型索引的父索引(QModelIndex::parent()函数获得)是无效的。各项的数据保存在QModelIndex中。
先看底层数据结构类:
#include
#include
class TreeItem
{
public:
TreeItem(const QList &data, TreeItem *parent = nullptr);
~TreeItem();
// 构建子Item列表;
void appendChild(TreeItem *child);
// 获取该Item指定行号的子Item;
TreeItem *child(int row);
// 获取该Item的子Item个数;
int childCount() const;
// 获取该Item的列数(数据段数);
int columnCount() const;
// 获取该Item指定段的数据;
QVariant data(int column) const;
// 获取该item所在parent的row;
int row() const;
// 该Item的父Item;
TreeItem *parentItem();
private:
TreeItem* m_parentItem; // 该Item的父Item;
QList m_childItems; // 该Item的子Item列表;
QList m_itemData; // 该Item的各列数据;
};
#include "tree_item.h"
TreeItem::TreeItem(const QList &data, TreeItem *parent)
:m_itemData(data),
m_parentItem(parent)
{
}
TreeItem::~TreeItem()
{
qDeleteAll(m_childItems);
}
void TreeItem::appendChild(TreeItem *child)
{
m_childItems.append(child);
}
TreeItem *TreeItem::child(int row)
{
// 无效的row;
if (row < 0 || row >= m_childItems.size())
return nullptr;
return m_childItems.at(row);
}
int TreeItem::childCount() const
{
return m_childItems.count();
}
int TreeItem::columnCount() const
{
return m_itemData.count();
}
QVariant TreeItem::data(int column) const
{
// 无效的索引返回空的QVariant;
if (column < 0 || column >= m_itemData.size())
return QVariant();
return m_itemData.at(column);
}
int TreeItem::row() const
{
if (m_parentItem)
m_parentItem->m_childItems.indexOf(const_cast(this));
// 尽管根项(没有父项)被自动分配了行号0,但模型从不使用此信息。
return 0;
}
TreeItem *TreeItem::parentItem()
{
return m_parentItem;
}
接下来子类化QAbstractItemModel是自定义tree model的关键。需要重写父类的函数(override标记)每个函数有较详细的解释。
#include
class TreeItem;
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel(const QString &data, QObject *parent = 0);
~TreeModel();
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 = Qt::DisplayRole) const override;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
private:
void setupModelData(const QStringList &lines, TreeItem *parent);
private:
TreeItem *rootItem;
};
TreeModel::TreeModel(const QString &data, QObject *parent)
:QAbstractItemModel(parent)
{
QList rootData;
rootData << "Title" << "Summary";
rootItem = new TreeItem(rootData);
setupModelData(data.split(QString("\n")), rootItem);
}
TreeModel::~TreeModel()
{
delete rootItem;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeItem *item = static_cast(index.internalPointer());
return item->data(index.column());
}
// We use the flags() function to ensure that views know that the model is read-only
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
// 该函数被view或delegate调用,用于获取某个item的特定row的子item的QModelIndex
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
// 判定指定的parent Item是否有(row, column) 的子Item;
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *pParentItem = nullptr;
if (!parent.isValid()) { // parent 是一个无效的索引,证明它是根Item;
pParentItem = rootItem;
} else {
pParentItem = (TreeItem *)parent.internalPointer();
}
TreeItem *pChildItem = pParentItem->child(row);
// 得到指定parent的第 row 个Item的 Model index;
if (pChildItem)
return createIndex(row, column, pChildItem);
return QModelIndex();
}
// 返回指定索引的父Item的index;
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *pChildItem = static_cast(index.internalPointer());
TreeItem *pParentItem = pChildItem->parentItem();
// 根Item有一个无效的index;
if (pParentItem == rootItem)
return QModelIndex();
// 自己作为parent(相对 index), 但也是别人的child;
return createIndex(pParentItem->row(), 0, pParentItem);
}
// 某个Item挂接多少子Item;
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
// 假如index 无效, 那就是根Item的子Item个数;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast(parent.internalPointer());
return parentItem->childCount();
}
// 列数是固定的;
int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
QList parents;
QList indentations;
parents << parent;
indentations << 0;
int number = 0;
while (number < lines.count()) {
int position = 0;
while (position < lines[number].length()) {
if (lines[number].at(position) != ' ')
break;
position++;
}
QString lineData = lines[number].mid(position).trimmed();
if (!lineData.isEmpty()) {
// Read the column data from the rest of the line.
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
QList columnData;
for (int column = 0; column < columnStrings.count(); ++column)
columnData << columnStrings[column];
if (position > indentations.last()) {
// The last child of the current parent is now the new parent
// unless the current parent has no children.
if (parents.last()->childCount() > 0) {
parents << parents.last()->child(parents.last()->childCount() - 1);
indentations << position;
}
}
else {
while (position < indentations.last() && parents.count() > 0) {
parents.pop_back();
indentations.pop_back();
}
}
// Append a new item to the current parent's list of children.
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
}
++number;
}
}