Qt Model View TreeView及对应Model

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

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

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

一、 概述

接着之前的话题继续!

如果把之前的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 于 北京门头沟。


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

查看所有标签

猜你喜欢:

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

算法基础

算法基础

[美] 托马斯 H.科尔曼(Thomas H.Cormen) / 王宏志 / 机械工业出版社 / 2015-12 / 59.00

本书介绍了什么是计算机算法,如何描述它们,以及如何来评估它们。这些计算机算法将提供:利用计算机搜索信息的简单方式;解决各种排序问题的方法;利用有向无环图和最短路径法来解决基本问题的方法(可用于建模公路网络,任务间的依赖及金融关系);解决字符串(例如DNA结构)问题的方法;密码学背后的基本原理;数据压缩的基础知识;以及甚至一些没有人能够理解如何在计算机上用相当长的时间来解决的问题。 本书适合作......一起来看看 《算法基础》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换