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

2021-03-23 10:40:50  阅读 3462 次 评论 0 条

原贴地址:https://blog.csdn.net/weixin_40569991/article/details/83154308


#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include <QDialog>
#include <QString>
#include <QTreeWidgetItem>
 
 
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 <QStringList>
 
 
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()<<name<<desc);
    ui->tv_Source->addTopLevelItem(item);
    return item;
}
 
QTreeWidgetItem * Widget::AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    parent->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;i<currentItem->columnCount();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");
}


20181018223934334.png

本文地址:http://jinesc.cn/?id=262
免责声明:本文为原创文章,版权归 jinesc 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?