您当前的位置: 首页 >  qt

lw只吃亿点.

暂无认证

  • 5浏览

    0关注

    47博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Qt之QTreeView的简单使用(含源码+注释)

lw只吃亿点. 发布时间:2022-06-12 19:38:32 ,浏览量:5

文章目录
  • 一、QTreeView操作示例图
    • 1.节点的添加删除示例图
    • 2.节点的值的获取与修改
  • 二、QTreeView(个人理解)
  • 三、源码
    • CMainWindow.h
    • CMainWindow.cpp
  • 四、拓展:上级节点的获取与判断
  • 总结
  • 相关文章

一、QTreeView操作示例图 1.节点的添加删除示例图

下图为节点添加删除示例图,其中包含添加顶级节点、添加子节点、移除节点等操作;源码在本文第三节(源码含详细注释)。 在这里插入图片描述

2.节点的值的获取与修改

下图为节点对节点值的操作,其中包含获取值、设置值等;源码在本文第三节(源码含详细注释)。 在这里插入图片描述 提示:不会使用Qt设计师设计界面的小伙伴点击这里

二、QTreeView(个人理解)

这里我们将QTreeView和QTableView对比一下

  1. 二者都是类似MVC(Model View Controller)模式,其中都包含Delegate,请查看Qt代理的实现(按钮篇)、Qt代理的实现(常规控件篇);
  2. 二者都使用QStandardItemModel存放数据;
  3. QTreeView也存在一个继承其QTreeWidget的子类;
  4. QTreeView会唯一不同的是会操作item的子节点。
三、源码 CMainWindow.h
#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()             
关注
打赏
1654352885
查看更多评论
0.1273s