向TreeWidget中添加根/子节点,删除节点,修改节点

拖一个TreeWidget 控件 改名 tv_Source

拖拉四个Button 按钮 分别取名btn_AddRoot,  btn_Add,  btn_Del,   btn_Modify

向TreeWidget中添加根/子节点,删除节点,修改节点_第1张图片

给四个按钮添加槽函数(clicked)

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 


namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void on_tv_Source_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
    void on_btn_Add_clicked();
    void on_btn_Modify_clicked();
    void on_btn_Del_clicked();
    void on_btn_AddRoot_clicked();


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

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    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");
}

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

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

void Widget::on_tv_Source_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    if(current==NULL)
        return;
    if(previous!=NULL)
    {
        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 Widget::on_btn_Add_clicked()
{
    QTreeWidgetItem * item= ui->tv_Source->currentItem();
    if(item!=NULL)
    {
        AddTreeNode(item,"new","new");
    }
    else
    {
        AddTreeRoot("new","new");
    }
}


//修改
void Widget::on_btn_Modify_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

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

//删除
void Widget::on_btn_Del_clicked()
{
    QTreeWidgetItem * currentItem = ui->tv_Source->currentItem();

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

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

实现效果: 

向TreeWidget中添加根/子节点,删除节点,修改节点_第2张图片

源码下载地址:https://download.csdn.net/download/weixin_40569991/10730744 

参考:https://www.cnblogs.com/li-peng/p/3652287.html

你可能感兴趣的:(C++,qt,treewidget)