内容简介:点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~一、接着之前的话题继续!
点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~
一、 概述
接着之前的话题继续!
如果把之前的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;
}
效果图如下:
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
更改后的title及层级
三、小结
① Model/View 中要想通过 TreeView 显示树型结构,需要在QStandardItemModel中组织树形数据结构
②通过 index 计算树形结构层级的方式
③通过 index 可以 Item 的内容
④使用 **View 时必须设置 Model ,因为 Model 中存储着数据结构
学不可以已
20200202 于 北京门头沟。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- FreeMarker对应各种数据结构解析
- Vue和MVVM的对应关系
- nhibernate一表对应多个实体类问题
- 调试V8中JS对应的汇编代码
- 二分查找及对应的几道经典题目
- 二分查找及对应的几道经典题目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
创业的艺术2.0
〔美〕盖伊·川崎 / 刘悦、段歆玥 / 译言·东西文库/电子工业出版社 / 2016-9 / 68
“创业者导师”——盖伊•川崎的《创业的艺术2.0》被阿丽亚娜•赫芬顿评为“终极的创业手册”。无论您是企业家、小企业主、企业开拓者还是非盈利组织的领导人,都可以让你的产品、服务或理念获得成功。 盖伊选取了不用角度,探索前十年商界的巨大变化,并寻求解决之道。曾经所向披靡的市场巨头深陷水深火热之中,社交媒体也取代了人际关系和广告,成为营销推广的主要渠道。众筹也成为广大投资者的可行之举。“云”更是每......一起来看看 《创业的艺术2.0》 这本书的介绍吧!