Qt 学习笔记 TreeWidget 增删改

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

布局如图

Qt 学习笔记 TreeWidget 增删改

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

.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());

    }

}

看一下效果

Qt 学习笔记 TreeWidget 增删改

完整代码
.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));

    }

}

 

 

 

 

 

你可能感兴趣的:(widget)