前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt Model View TreeView及对应Model

Qt Model View TreeView及对应Model

作者头像
用户5908113
发布2020-02-17 13:44:36
2.5K0
发布2020-02-17 13:44:36
举报
文章被收录于专栏:Pou光明Pou光明

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

一、概述

接着之前的话题继续!

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

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

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

QObject

||

QAbstractItemModel

||

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

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

二、程序举例

1. 使用QStandardItemModel构建Tree

以Qt自带的treeview来说明

代码语言:javascript
复制
//实例化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 的selectionModel会发出selectionChanged的信号,将该信号与槽函数进行连接,在槽函数中我们可以通过index获得所选Item的内容;通过顶层节点没有parent的特点来计算所选Item的层级。

主要代码如下:

代码语言:javascript
复制
//信号函数 连接信号与槽
 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 于 北京门头沟。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Pou光明 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档