Qt 学习笔记 TreeWidget 增删改

在窗体上放一个TreeWidget控件和四个PushButton加一个Horizontal Spacer

布局如图

给树添加元素节点的方法和实现

.h文件

    QTreeWidgetItem * AddTreeRoot(QString name,QString desc);
    QTreeWidgetItem * AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc);

.cpp文件

QTreeWidgetItem * TreeViewView::AddTreeRoot(QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    ui->tv_Source->addTopLevelItem(item);
    return item;
}

QTreeWidgetItem * TreeViewView::AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    parent->addChild(item);
    return item;
}

在构造方法里添加元素设置树的列数和标题的名称 当然可以设置Header不显示,根据你的需求定

 ui->tv_Source->setColumnCount(2);
    ui->tv_Source->setHeaderLabels(QStringList()<<"Name"<<"Description");
    //ui->tv_Source->setHeaderHidden(true);
    QTreeWidgetItem * beiJingItem = AddTreeRoot("bejing","city");
    AddTreeNode(beiJingItem,"haidian","HaiDian");
    AddTreeNode(beiJingItem,"chaoYang","chaoYang");
    AddTreeNode(beiJingItem,"fengTai","fengTai");
    QTreeWidgetItem * HeiBeiItem =AddTreeRoot("hebei","city");
    AddTreeNode(HeiBeiItem,"baoDing","baoDing");
    AddTreeNode(HeiBeiItem,"shiJiaZhuang","shiJiaZhuang");

添加事件相对来说比较简单 直接调用我的前边写的方法,因为TreeWidget一直是选中状态就加了一个

添加根节点的槽

void TreeViewView::on_btn_Add_clicked()
{
    QTreeWidgetItem * item= ui->tv_Source->currentItem();
    if(item!=Q_NULLPTR)
    {
        AddTreeNode(item,"new","new");
    }
    else
    {
        AddTreeRoot("new","new");
    }
}

void TreeViewView::on_btn_AddRoot_clicked()
{
    AddTreeRoot("new","new");
}

修改方法也很简单

void TreeViewView::on_btn_Modify_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

    if(currentItem==Q_NULLPTR)
    {
        return;
    }
    for(int i=0;i<currentItem->columnCount();i++)
    {
        currentItem->setText(i,tr("Modify")+QString::number(i));
    }
}

删除槽就要考虑的多一点 要看它有没有父节点

void TreeViewView::on_btn_Del_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

    if(currentItem==Q_NULLPTR)
    {
        return;
    }
    //如果没有父节点就直接删除
    if(currentItem->parent()==Q_NULLPTR)
    {
        delete ui->tv_Source->takeTopLevelItem(ui->tv_Source->currentIndex().row());
    }
    else
    {
        //如果有父节点就要用父节点的takeChild删除节点
        delete currentItem->parent()->takeChild(ui->tv_Source->currentIndex().row());
    }
}

看一下效果

完整代码 .h文件

#ifndef TREEVIEWVIEW_H
#define TREEVIEWVIEW_H

#include <QDialog>
#include<QString>
#include<QTreeWidgetItem>
namespace Ui {
class TreeViewView;
}

class TreeViewView : public QDialog
{
    Q_OBJECT

public:
    explicit TreeViewView(QWidget *parent = 0);
    ~TreeViewView();

private slots:
    void on_tv_Source_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

    void on_btn_Add_clicked();

    void on_btn_AddRoot_clicked();

    void on_btn_Del_clicked();

    void on_btn_Modify_clicked();

private:
    Ui::TreeViewView *ui;
    QTreeWidgetItem * AddTreeRoot(QString name,QString desc);
    QTreeWidgetItem * AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc);
};

#endif // TREEVIEWVIEW_H

.cpp文件

#include "treeviewview.h"
#include "ui_treeviewview.h"
#include <QStringList>
TreeViewView::TreeViewView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TreeViewView)
{
    ui->setupUi(this);

    ui->tv_Source->setColumnCount(2);
    ui->tv_Source->setHeaderLabels(QStringList()<<"Name"<<"Description");
    //ui->tv_Source->setHeaderHidden(true);
    QTreeWidgetItem * beiJingItem = AddTreeRoot("bejing","city");
    AddTreeNode(beiJingItem,"haidian","HaiDian");
    AddTreeNode(beiJingItem,"chaoYang","chaoYang");
    AddTreeNode(beiJingItem,"fengTai","fengTai");
    QTreeWidgetItem * HeiBeiItem =AddTreeRoot("hebei","city");
    AddTreeNode(HeiBeiItem,"baoDing","baoDing");
    AddTreeNode(HeiBeiItem,"shiJiaZhuang","shiJiaZhuang");
}

TreeViewView::~TreeViewView()
{
    delete ui;
}

QTreeWidgetItem * TreeViewView::AddTreeRoot(QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    ui->tv_Source->addTopLevelItem(item);
    return item;
}

QTreeWidgetItem * TreeViewView::AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    parent->addChild(item);
    return item;
}

void TreeViewView::on_tv_Source_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    if(current==Q_NULLPTR)return;
    if(previous!=Q_NULLPTR)
    {
        previous->setBackground(0,Qt::transparent);
        previous->setBackground(1,Qt::transparent);
        previous->setTextColor(0,Qt::black);
        previous->setTextColor(1,Qt::black);
    }
    current->setTextColor(0,Qt::blue);
    current->setTextColor(1,Qt::blue);
    current->setBackground(0,Qt::red);
    current->setBackground(1,Qt::red);
}

void TreeViewView::on_btn_Add_clicked()
{
    QTreeWidgetItem * item= ui->tv_Source->currentItem();
    if(item!=Q_NULLPTR)
    {
        AddTreeNode(item,"new","new");
    }
    else
    {
        AddTreeRoot("new","new");
    }
}

void TreeViewView::on_btn_AddRoot_clicked()
{
    AddTreeRoot("new","new");
}

void TreeViewView::on_btn_Del_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

    if(currentItem==Q_NULLPTR)
    {
        return;
    }
    //如果没有父节点就直接删除
    if(currentItem->parent()==Q_NULLPTR)
    {
        delete ui->tv_Source->takeTopLevelItem(ui->tv_Source->currentIndex().row());
    }
    else
    {
        //如果有父节点就要用父节点的takeChild删除节点
        delete currentItem->parent()->takeChild(ui->tv_Source->currentIndex().row());
    }
}

void TreeViewView::on_btn_Modify_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

    if(currentItem==Q_NULLPTR)
    {
        return;
    }
    for(int i=0;i<currentItem->columnCount();i++)
    {
        currentItem->setText(i,tr("Modify")+QString::number(i));
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏liulun

使用开源项目Alipay.AopSdk.Core完成支付宝网页登录

核心方法: public IActionResult UserInfoCallBackAli() { var alipa...

37970
来自专栏生信技能树

linux 命令中英文对照,收集

听说markdown排版得用浏览器打开,点击最下面的阅读原文也可以! Is Linux CLI case-sensitive? The answer is, y...

41060
来自专栏程序猿

安全工具Aircrack-ng的使用

开启了尘封数月的Kali Linux。 其实安全也是数据库学习方向的一个分支哦~~ 官方网站:http://aircrack-ng.org root@kali:...

59590
来自专栏游戏杂谈

cocos2d-x 2.x版本接入bugly的总结

最开始项目使用的是自己DIY的很简陋的上报系统,后来改成google breakpad来上报,发现其实都做的不太理想,游戏引擎因为版本历史问题存在一些崩溃问题。...

21000
来自专栏TechBox

iOS开发之UICollectionViewController系列(二) :详解CollectionView各种回调

27340
来自专栏技术小黑屋

Android签名相关知识整理

不止一次有用到Android签名相关的知识,每次都几乎从零开始在Google上搜索找,不想在继续这样了,找了个时间好好整理了一下自己用到的一些碎片知识,于是乎放...

13640
来自专栏黑白安全

ctf工具包 ctf Toolkit 渗透测试工具包 隐秘数据破解

│ ├── caidao-20160620-www.maicaidao.com.7z

1.1K20
来自专栏我和未来有约会

silverlight beta 2 将在本周末发布.

太开心了,刚刚收到微软silverlight.net的邮件.  确定了 将在本周末发布beta2. 以下是部分邮件内容 引用: There are some...

21560
来自专栏zaking's

RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—单词注释版)

16220
来自专栏技术小黑屋

Jar Mismatch! Fix Your Dependencies

There was a requirement of my work. It requires me to integrated my current proj...

10120

扫码关注云+社区

领取腾讯云代金券