文章目录
一、QTreeView操作示例图
1.节点的添加删除示例图
- 一、QTreeView操作示例图
- 1.节点的添加删除示例图
- 2.节点的值的获取与修改
- 二、QTreeView(个人理解)
- 三、源码
- CMainWindow.h
- CMainWindow.cpp
- 四、拓展:上级节点的获取与判断
- 总结
- 相关文章
下图为节点添加删除示例图,其中包含添加顶级节点、添加子节点、移除节点等操作;源码在本文第三节(源码含详细注释)。
下图为节点对节点值的操作,其中包含获取值、设置值等;源码在本文第三节(源码含详细注释)。 提示:不会使用Qt设计师设计界面的小伙伴点击这里
这里我们将QTreeView和QTableView对比一下
- 二者都是类似MVC(Model View Controller)模式,其中都包含Delegate,请查看Qt代理的实现(按钮篇)、Qt代理的实现(常规控件篇);
- 二者都使用QStandardItemModel存放数据;
- QTreeView也存在一个继承其QTreeWidget的子类;
- QTreeView会唯一不同的是会操作item的子节点。
#ifndef CMAINWINDOW_H
#define CMAINWINDOW_H
#include
#include //数据模型类
namespace Ui {
class CMainWindow;
}
class CMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit CMainWindow(QWidget *parent = 0);
~CMainWindow();
private slots:
/**
* @brief on_getCurNodeDataBtn_clicked 获取当前节点值
*/
void on_getCurNodeDataBtn_clicked();
/**
* @brief on_setCurNodeDataBtn_clicked 设置当前节点值
*/
void on_setCurNodeDataBtn_clicked();
/**
* @brief on_addTopNodeBtn_clicked 添加顶级节点
*/
void on_addTopNodeBtn_clicked();
/**
* @brief on_addChildNodeBtn_clicked 添加子节点
*/
void on_addChildNodeBtn_clicked();
/**
* @brief on_removeCurNodeBtn_clicked 移除当前节点
*/
void on_removeCurNodeBtn_clicked();
private:
Ui::CMainWindow *ui;
QStandardItemModel *m_pModel; //数据模型对象指针
};
CMainWindow.cpp
#include "CMainWindow.h"
#include "ui_CMainWindow.h"
CMainWindow::CMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CMainWindow)
{
ui->setupUi(this);
//设置窗口标题
this->setWindowTitle("QTreeView的简单使用");
//===============数据模型(QStandardItemModel)===============
//建立数据模型对象空间并指定父对象
m_pModel = new QStandardItemModel(ui->treeView);
//将数据模型设置到树形视图上
ui->treeView->setModel(m_pModel);
//设置水平表头列平均分
ui->treeView->header()->setSectionResizeMode(QHeaderView::Stretch);
//添加列标题
m_pModel->setHorizontalHeaderLabels(QStringList() setText(curIndex.data().toString());
}
void CMainWindow::on_setCurNodeDataBtn_clicked()
{
//获取当前行列
QModelIndex curIndex = ui->treeView->currentIndex();
int row = curIndex.row();
int column = curIndex.column();
//当前位置包含-1值返回
if( -1 == row || -1 == column)
{
return;
}
///需要获取item的情况///
/// \brief curItem 当需要拿到具体item/位置时使用
/// 比如设置item值
QStandardItem *curItem;
int parentRow = curIndex.data(Qt::UserRole + 1).toInt();
//判断顶级节点值选择相应操作
if(-1 == parentRow)
{
curItem = m_pModel->item(row, column);
}
else
{
//获取当前位置的顶级节点
QStandardItem *parentItem = m_pModel->item(parentRow);
//通过顶级节点获取子节点
curItem = parentItem->child(row, column);
}
//获取值编辑框中的值并设置到item上
curItem->setText(ui->valueEdit->text());
}
void CMainWindow::on_addTopNodeBtn_clicked()
{
int index = m_pModel->rowCount();
QList topList;
//链表容器添加顶级节点
topList treeView->currentIndex();
int row = curIndex.row();
int column = curIndex.column();
int parentRow = curIndex.data(Qt::UserRole + 1).toInt();
//当前行列值包含-1值或当前节点非顶级节点时返回
if( -1 == row || -1 == column || -1 != parentRow)
{
return;
}
//获取指定行的首个item
QStandardItem *curTopItem = m_pModel->item(row);
//为顶级节点添加子节点
QList childList;
childList setData(row, Qt::UserRole + 1);
//添加子节点
curTopItem->appendRow(childList);
}
void CMainWindow::on_removeCurNodeBtn_clicked()
{
//获取当前行列
QModelIndex curIndex = ui->treeView->currentIndex();
int row = curIndex.row();
int column = curIndex.column();
//当前行列值包含-1值或当前节点非顶级节点时返回
if( -1 == row || -1 == column)
{
return;
}
int parentRow = curIndex.data(Qt::UserRole + 1).toInt();
//判断顶级节点值选择相应的移除操作
if(-1 == parentRow)
{
m_pModel->removeRow(row);
}
else
{
//移除某个子节点需要找到其顶级节点
QStandardItem *parentItem = m_pModel->item(parentRow);
parentItem->removeRow(row);
}
}
四、拓展:上级节点的获取与判断
本文对于节点的判断是通过data设置用户定义值操作的,还有一种方法,是通过获取当前位置的QModelIndex对象获取其父对象的QModelIndex判断,代码如下(下方代码仅适用于本文): 提示:据说以下方法不严谨,如手动指定父类对象时。
void CMainWindow::parentIndex()
{
//获取当前节点的QModelIndex对象
QModelIndex index = ui->treeView->currentIndex();
//获取当前节点父对象的QModelIndex对象
QModelIndex parentIndex = index.parent();
//判断其父节点的行列值,顶级节点的父节点行列值为-1
if(-1 == parentIndex.row() || -1 == parentIndex.column())
{
qDebug()
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?