使用 QTreeWidget 实现复杂树控件功能的详细教程_treewidget 加控件-CSDN博客
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTreeWidget * treeWidget;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
treeWidget = new QTreeWidget;
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(treeWidget);
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
treeWidget->setColumnCount(3);
QStringList headers;
headers << QString("班级")
<< QString("分配科目")
<< QString("是否达标");
treeWidget->setHeaderLabels(headers);
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
item->setText(0, QString("某班级"));
item->setCheckState(2, Qt::Unchecked);
QTreeWidgetItem *topLevelItem = new QTreeWidgetItem(treeWidget);
topLevelItem->setText(0, QString("顶层项目"));
treeWidget->addTopLevelItem(topLevelItem);
}
MainWindow::~MainWindow()
{
delete ui;
}
QTreeWidgetItem *newTopLevelItem = new QTreeWidgetItem();
newTopLevelItem->setText(0, QString("插入顶层项目"));
newTopLevelItem->setText(1,"第1列");
newTopLevelItem->setText(2,"第2列");
treeWidget->insertTopLevelItem(0, newTopLevelItem);
QTreeWidgetItem *childItem = new QTreeWidgetItem();
childItem->setText(0, QString("子项目"));
topLevelItem->addChild(childItem);
QTreeWidgetItem *childItem1 = new QTreeWidgetItem();
childItem1->setText(0, QString("子项目1"));
topLevelItem->addChild(childItem1);
topLevelItem->removeChild(childItem);
QTreeWidgetItem *removedItem = treeWidget->takeTopLevelItem(0);
delete removedItem;
QTreeWidgetItem *childItem2 = new QTreeWidgetItem();
childItem1->setText(1, QString("子项目2"));
topLevelItem->addChild(childItem2);
一个demo:
一个有两种类型的QTreeWidgetItem:节点和名称
可以添加节点,添加名称,删除,修改QTreeWidgetItem的名称
右键弹出菜单
(根据右键的位置:分3种:1.点击节点 2.点击名称 3.点击空白处)
弹出相关的菜单按钮
#ifndef MAINWINDOW1_H
#define MAINWINDOW1_H
#include <QMainWindow>
#include <QMenu>
#include <QTreeWidget>
namespace Ui {
class MainWindow1;
}
class MainWindow1 : public QMainWindow
{
Q_OBJECT
enum TreeItemType
{
none = 0,
point = 1,
name = 2
};
public:
explicit MainWindow1(QWidget *parent = 0);
~MainWindow1();
private slots:
void on_actionAddPoint_triggered();
void on_actionAddName_triggered();
void PopMenu(QPoint);
void on_actionDel_triggered();
void on_actionChangeItemName_triggered();
private:
void setActionsState(bool addPoint,bool addName,bool del,bool changeName);
void RemoveItem(QTreeWidgetItem *pItem);
protected:
private:
Ui::MainWindow1 *ui;
int mPoint;
int mName;
QMenu* mMenu;
};
#endif // MAINWINDOW1_H
#include "mainwindow1.h"
#include "ui_mainwindow1.h"
#include <QMouseEvent>
#include <QInputDialog>
#include <QDebug>
MainWindow1::MainWindow1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow1)
{
ui->setupUi(this);
ui->treeWidget->setHeaderLabel("HeaderLabel");
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeWidget,&QTreeWidget::customContextMenuRequested,this,&MainWindow1::PopMenu);
mMenu = new QMenu(this);
mMenu->addAction(ui->actionAddPoint);
mMenu->addAction(ui->actionAddName);
mMenu->addAction(ui->actionDel);
mMenu->addAction(ui->actionChangeItemName);
mPoint = 0;
mName = 0;
}
MainWindow1::~MainWindow1()
{
delete ui;
}
void MainWindow1::on_actionAddPoint_triggered()
{
QTreeWidgetItem *Item = ui->treeWidget->currentItem();
if(Item == nullptr)
{
QStringList strList;
strList<<QString("节点%1").arg(mPoint);
QTreeWidgetItem* pItem = new QTreeWidgetItem(strList);
pItem->setData(0,Qt::UserRole+1,TreeItemType::point);
ui->treeWidget->addTopLevelItem(pItem);
mPoint++;
}
else
{
QStringList strList;
strList<<QString("节点%1").arg(mPoint);
QTreeWidgetItem* pItem = new QTreeWidgetItem(strList);
pItem->setData(0,Qt::UserRole+1,TreeItemType::point);
Item->addChild(pItem);
mPoint++;
}
}
void MainWindow1::on_actionAddName_triggered()
{
QTreeWidgetItem *Item = ui->treeWidget->currentItem();
if(Item == nullptr)
{
QStringList strList;
strList<<QString("名称%1").arg(mName);
QTreeWidgetItem* pItem = new QTreeWidgetItem(strList);
pItem->setData(0,Qt::UserRole+1,TreeItemType::name);
ui->treeWidget->addTopLevelItem(pItem);
mName++;
}
else
{
QStringList strList;
strList<<QString("名称%1").arg(mName);
QTreeWidgetItem* pItem = new QTreeWidgetItem(strList);
pItem->setData(0,Qt::UserRole+1,TreeItemType::name);
Item->addChild(pItem);
mName++;
}
}
void MainWindow1::PopMenu(QPoint pos)
{
QModelIndex index = ui->treeWidget->indexAt(pos);
if(!index.isValid())
{
ui->treeWidget->setCurrentIndex(QModelIndex());
}
QTreeWidgetItem *pItem = ui->treeWidget->currentItem();
if(nullptr == pItem)
{
setActionsState(1,1,0,0);
}
else
{
TreeItemType type = (TreeItemType)pItem->data(0,Qt::UserRole+1).toInt();
switch (type) {
case TreeItemType::point:
setActionsState(1,1,1,1);
break;
case TreeItemType::name:
setActionsState(0,0,1,1);
break;
default:
break;
}
qDebug()<<pItem->text(0).toStdString().c_str();
}
mMenu->exec(QCursor::pos());
}
void MainWindow1::on_actionDel_triggered()
{
QTreeWidgetItem *pItem = ui->treeWidget->currentItem();
if(nullptr == pItem)
{
}
else
{
RemoveItem(pItem);
}
}
void MainWindow1::on_actionChangeItemName_triggered()
{
QTreeWidgetItem *pItem = ui->treeWidget->currentItem();
TreeItemType type = (TreeItemType)pItem->data(0,Qt::UserRole+1).toInt();
QString str;
switch (type) {
case TreeItemType::point:
str="节点";
break;
case TreeItemType::name:
str="名称";
break;
default:
break;
}
QInputDialog dialog;
dialog.exec();
pItem->setData(0,Qt::DisplayRole,str+dialog.textValue());
}
void MainWindow1::setActionsState(bool addPoint, bool addName, bool del, bool changeName)
{
ui->actionAddPoint->setVisible(addPoint);
ui->actionAddName->setVisible(addName);
ui->actionChangeItemName->setVisible(changeName);
ui->actionDel->setVisible(del);
}
void MainWindow1::RemoveItem(QTreeWidgetItem *pItem)
{
int nChildCount = pItem->childCount();
if(nChildCount == 0)
{
delete pItem;
pItem = nullptr;
return ;
}
for(int i = 0; i < nChildCount; ++i)
{
QTreeWidgetItem *pTmp = pItem->child(0);
RemoveItem(pTmp);
}
delete pItem;
pItem = nullptr;
}