QTreeView常见功能(添加节点、排序、右键菜单)

Qt 专栏收录该内容
28 篇文章 0 订阅

      效果

h文件

#ifndef CBASETREE_H
#define CBASETREE_H

#include <QObject>
#include <QTreeView>
#include <QStandardItemModel>

class CBaseTree : public QTreeView
{
    Q_OBJECT
public:
    CBaseTree(QWidget * parent = nullptr);
    ~CBaseTree();
private:
    QStandardItemModel *m_model;

private:
    void iniVar();           //初始化变量
    void iniUi();            //初始化界面
    void iniConnect();       //初始化连接

    void addNode();
private slots:
    void onCustomContextMenuRequested(const QPoint& pos); //右键菜单
    void onGoToFromTree();//跳转
    void onShowAttriFromTree();//属性
};

#endif // CBASETREE_H

cpp文件

#include "cbasetree.h"
#include <QSortFilterProxyModel>
#include <QHeaderView>
#include <QMenu>

#ifdef _MSC_VER
#pragma execution_character_set("utf-8")
#endif

CBaseTree::CBaseTree(QWidget * parent)
    :QTreeView (parent)
{
    iniVar();
    iniUi();
    iniConnect();
    addNode();
}

CBaseTree::~CBaseTree()
{

}

void CBaseTree::iniVar()
{
     m_model = new QStandardItemModel(this);
}

void CBaseTree::iniUi()
{
    m_model->setColumnCount(2);//设置为2列
    this->setHeaderHidden(false);//设置列头可见
    m_model->setHorizontalHeaderLabels(QStringList()<<tr("姓名")<<tr("分数"));
    this->setEditTriggers(QTreeView::NoEditTriggers);//设置双击不能编辑
    this->setExpandsOnDoubleClick(true);//设置双击节点可展开或折叠树
    this->setMouseTracking(true);

    //设置树自动排序显示
    this->setSortingEnabled(true);//设置自动排序
    this->sortByColumn(0,Qt::AscendingOrder);//设置第0列  以 aaa-zzz的顺序排序
    this->setContextMenuPolicy(Qt::CustomContextMenu);//设置菜单可定义菜单
    QSortFilterProxyModel * m_proxyModel= new QSortFilterProxyModel(this);
    m_proxyModel->setDynamicSortFilter(true);//设置树动态排序,比如添加一个节点就立马排序
    m_proxyModel->setSortRole(Qt::DisplayRole);//设置以哪项角色排序
    m_proxyModel->setSourceModel(m_model);//设置数据源
    this->setModel(m_proxyModel);//设置模型
}

void CBaseTree::iniConnect()
{
    connect(this, &CBaseTree::customContextMenuRequested,
            this, &CBaseTree::onCustomContextMenuRequested);
}

void CBaseTree::addNode()
{
    QStandardItem * item1 = new QStandardItem(tr("1——班"));
    QStandardItem * item2 = new QStandardItem(tr("2——班"));
    QStandardItem * item1_1 = new QStandardItem(tr("子轩"));
    QStandardItem * item1_2 = new QStandardItem(tr("紫萱"));
    QStandardItem * item1_11 = new QStandardItem(tr("88"));
    QStandardItem * item1_21 = new QStandardItem(tr("99"));
    m_model->appendRow(item1);
    m_model->appendRow(item2);
    QList<QStandardItem *> one,two;
    one<<item1_1<<item1_11;
    two<<item1_2<<item1_21;
    item1->appendRow(two);
    item1->appendRow(one);
}

void CBaseTree::onCustomContextMenuRequested(const QPoint &pos)
{
    QModelIndex index = currentIndex();
    if(index.isValid())
    {

        QMenu menu;
        menu.addAction(tr("跳转"),this,SLOT(onGoToFromTree()));
        menu.addSeparator();
        menu.addAction(tr("属性"),this,SLOT(onShowAttriFromTree()));
        menu.exec(QCursor::pos());
    }
}

void CBaseTree::onGoToFromTree()
{

}

void CBaseTree::onShowAttriFromTree()
{

}

 

  • 1
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值