Qt Model View TreeView及对应Model

栏目: IT技术 · 发布时间: 6年前

内容简介:点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~一、接着之前的话题继续!

点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~

一、 概述

接着之前的话题继续!

如果把之前的QTableView改成 QTreeView ,我们在不改变 Model 的情况下可以直接得到一个没有结构层次的“树”;因为 QAbstractTableModel 不具有数据层次结构,如果我们想要实现有层次的数据结构,需要使用 QStandardItemModel

该模型。为了显示一棵树, QStandardItemModel 需要使用 QStandardItem 来进行填充。

下面梳理下几个类的关系:

QObject

||

QAbstractItemModel

||

QAbstractTableModel(Table 层次结构 ) QStandardItemModel(Tree 层次结构 )

如果以后构建自己的代码库时,各个模块划分的越详细则越方便复用。

二、程序举例

1. 使用QStandardItemModel构建Tree

以Qt自带的 treeview 来说明

//实例化model

standardModel = new QStandardItemModel ;

//QStandardItem 节点数据

QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third");


// root 节点

QStandardItem *item = standardModel->invisibleRootItem();

//root 节点添加数据

item->appendRow(preparedRow);


//又一个QStandardItem 节点数据

QList<QStandardItem *> secondRow =prepareRow("111", "222", "333");

//在first节点上再添加一个数据

preparedRow.first()->appendRow(secondRow);

//view 设置model并全部展开

treeView->setModel(standardModel);

treeView->expandAll();

//添加数据节点的函数

QList<QStandardItem *> MainWindow::prepareRow(const QString &first,

const QString &second,

const QString &third)

{

QList<QStandardItem *> rowItems;

rowItems << new QStandardItem(first);

rowItems << new QStandardItem(second);

rowItems << new QStandardItem(third);


return rowItems;

}

效果图如下:

Qt Model View TreeView及对应Model

2. 获得所选Item的内容以及层级

有了上面的基础,接下来进行扩展:

当 treeView 的 Item 被选中时, treeView 的 selection M odel 会发出 selectionChanged 的信号,将该信号与槽函数进行连接,在槽函数中我们可以通过 index 获得所选 Item 的内容;通过顶层节点没有 parent 的特点来计算所选 Item 的层级。

主要代码如下:

//信号函数 连接信号与槽

QItemSelectionModel *selectionModel= treeView->selectionModel();


connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));


//槽函数如下

void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection & /*oldSelection*/)

{

//get the text of the selected item

const QModelIndex index = treeView->selectionModel()->currentIndex();

QString selectedText = index.data(Qt::DisplayRole).toString();

//find out the hierarchy level of the selected item

int hierarchyLevel=1;

QModelIndex seekRoot = index;

while(seekRoot.parent() != QModelIndex())

{

seekRoot = seekRoot.parent();

hierarchyLevel++;

}

QString showString = QString("%1, Level %2").arg(selectedText)

.arg(hierarchyLevel);

setWindowTitle(showString);

}

效果如下:

默认title

Qt Model View TreeView及对应Model

更改后的title及层级

Qt Model View TreeView及对应Model

三、小结

① Model/View 中要想通过 TreeView 显示树型结构,需要在QStandardItemModel中组织树形数据结构

②通过 index 计算树形结构层级的方式

③通过 index 可以 Item 的内容

④使用 **View 时必须设置 Model ,因为 Model 中存储着数据结构

学不可以已

Qt Model View TreeView及对应Model

20200202 于 北京门头沟。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

数据结构与算法

数据结构与算法

2009-8 / 32.00元

《数据结构与算法》系统地介绍了数据结构的基本概念和基本算法,主要内容包括:绪论,线性表,栈与队列,串,数组、特殊矩阵和广义表,树,图,排序,查找,算法的分析与设计,实验与上机指导。《数据结构与算法》特别注重突出应用性和实践性,实例和习题丰富,并在附录中给出了各章习题的答案。 《数据结构与算法》适合作为应用型本科院校和成人教育计算机专业数据结构课程的教材,也可作为数据结构培训班的教材以及软件从......一起来看看 《数据结构与算法》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具