//qtreewidget 默认的columncount property属性
/*
qtreewidget默认的columncount为1.可以通过setcolumncount()把它设置为一个多列的qtreewidget
*/
ui->treeWidget_2->setColumnCount(2);
QTreeWidgetItem* topItem1 = new QTreeWidgetItem(ui->treeWidget_2);
topItem1->setText(0, "百度");
topItem1->setText(1, "蒙牛");
ui->treeWidget_2->addTopLevelItem(topItem1);
QTreeWidgetItem* topItem2 = new QTreeWidgetItem(ui->treeWidget_2);
topItem2->setText(0, "腾讯");
topItem2->setText(1, "伊利");
ui->treeWidget_2->addTopLevelItem(topItem2);
//ui->treeWidget_2->setHeaderHidden(true); //隐藏qtreewidget的头部
QStringList strList;
strList << "互联网公司" << "食品公司";
ui->treeWidget_2->setHeaderLabels(strList); //自定义qtreewidget的头部
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//设置顶层节点
QTreeWidgetItem* topItem = new QTreeWidgetItem(ui->treeWidget);
topItem->setText(0, "百度");
ui->treeWidget->addTopLevelItem(topItem);
ui->treeWidget->setHeaderHidden(true);
//qtreewidget 默认的columncount property属性
/*
qtreewidget默认的columncount为1.可以通过setcolumncount()把它设置为一个多列的qtreewidget
*/
ui->treeWidget_2->setColumnCount(2);
QTreeWidgetItem* topItem1 = new QTreeWidgetItem(ui->treeWidget_2);
topItem1->setText(0, "百度");
topItem1->setText(1, "蒙牛");
ui->treeWidget_2->addTopLevelItem(topItem1);
//为顶层节点添加子节点
QTreeWidgetItem* subItem1 = new QTreeWidgetItem();
subItem1->setText(0, "人事部");
topItem1->addChild(subItem1);
QTreeWidgetItem* subItem2 = new QTreeWidgetItem();
subItem2->setText(0, "财务部");
topItem1->addChild(subItem2);
QTreeWidgetItem* topItem2 = new QTreeWidgetItem(ui->treeWidget_2);
topItem2->setText(0, "腾讯");
topItem2->setText(1, "伊利");
ui->treeWidget_2->addTopLevelItem(topItem2);
//ui->treeWidget_2->setHeaderHidden(true); //隐藏qtreewidget的头部
QStringList strList;
strList << "互联网公司" << "食品公司";
ui->treeWidget_2->setHeaderLabels(strList); //自定义qtreewidget的头部
//设置右键菜单选项
//允许右键菜单
ui->treeWidget_2->setContextMenuPolicy(Qt::CustomContextMenu);
//设置菜单
QMenu *myMenu = new QMenu(ui->treeWidget_2);
QAction *add = myMenu->addAction("添加");
QAction *del = myMenu->addAction("删除");
//响应右键菜单信号
connect(ui->treeWidget_2, &QTreeWidget::customContextMenuRequested, this, [=](const QPoint pos){
qDebug() << "x: " << pos.x() << " y: " << pos.y();
myMenu->exec(ui->treeWidget_2->mapToGlobal(pos));
});
//响应添加的action点击
connect(add, &QAction::triggered, this, [=](){
//添加一个固定的子节点
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, "营销部");
topItem1->addChild(item);
});
//响应删除的action点击
connect(del, &QAction::triggered, this, [=](){
qDebug() << topItem1->childCount();
if(topItem1->childCount() >= 1)
{
QTreeWidgetItem *item = topItem1->child(topItem1->childCount() - 1);
topItem1->removeChild(item);
delete item;
item = nullptr;
}
});
}
这样有两个缺点 1. 只能添加特定的子项。2. 不能实现不同层级节点的不同菜单。
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public:
QString partmenName;
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog),
partmenName("")
{
ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, [&](){
if(ui->lineEdit->text() != "")
{
partmenName = ui->lineEdit->text();
}
this->accept();
});
connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){
this->reject();
});
}
Dialog::~Dialog()
{
delete ui;
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//设置顶层节点
QTreeWidgetItem* topItem = new QTreeWidgetItem(ui->treeWidget);
topItem->setText(0, "百度");
ui->treeWidget->addTopLevelItem(topItem);
ui->treeWidget->setHeaderHidden(true);
//qtreewidget 默认的columncount property属性
/*
qtreewidget默认的columncount为1.可以通过setcolumncount()把它设置为一个多列的qtreewidget
*/
ui->treeWidget_2->setColumnCount(2);
QTreeWidgetItem* topItem1 = new QTreeWidgetItem(ui->treeWidget_2);
topItem1->setText(0, "百度");
topItem1->setText(1, "蒙牛");
ui->treeWidget_2->addTopLevelItem(topItem1);
//为顶层节点添加子节点
QTreeWidgetItem* subItem1 = new QTreeWidgetItem();
subItem1->setText(0, "人事部");
topItem1->addChild(subItem1);
QTreeWidgetItem* subItem2 = new QTreeWidgetItem();
subItem2->setText(0, "财务部");
topItem1->addChild(subItem2);
QTreeWidgetItem* topItem2 = new QTreeWidgetItem(ui->treeWidget_2);
topItem2->setText(0, "腾讯");
topItem2->setText(1, "伊利");
ui->treeWidget_2->addTopLevelItem(topItem2);
//ui->treeWidget_2->setHeaderHidden(true); //隐藏qtreewidget的头部
QStringList strList;
strList << "互联网公司" << "食品公司";
ui->treeWidget_2->setHeaderLabels(strList); //自定义qtreewidget的头部
//设置右键菜单选项
//允许右键菜单
ui->treeWidget_2->setContextMenuPolicy(Qt::CustomContextMenu);
//设置菜单
QMenu *myMenu = new QMenu(ui->treeWidget_2);
QAction *add = myMenu->addAction("添加");
QAction *del = myMenu->addAction("删除");
//响应右键菜单信号
connect(ui->treeWidget_2, &QTreeWidget::customContextMenuRequested, this, [=](const QPoint pos){
qDebug() << "x: " << pos.x() << " y: " << pos.y();
myMenu->exec(ui->treeWidget_2->mapToGlobal(pos));
});
//响应添加的action点击
connect(add, &QAction::triggered, this, [=](){
//添加一个固定的子节点
if(dlg.exec() == QDialog::Accepted)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, dlg.partmenName);
topItem1->addChild(item);
}
});
//响应删除的action点击
connect(del, &QAction::triggered, this, [=](){
qDebug() << topItem1->childCount();
if(topItem1->childCount() >= 1)
{
QTreeWidgetItem *item = topItem1->child(topItem1->childCount() - 1);
topItem1->removeChild(item);
delete item;
item = nullptr;
}
});
}
Widget::~Widget()
{
delete ui;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include "dialog.h"
#include "ui_dialog.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
Dialog dlg;
};
#endif // WIDGET_H
还可以通过TYPE属性来确定每一个节点的层级。
代码实现
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置这是一个多列的treewidget
ui->treeWidget->setColumnCount(2);
//设置表头
QStringList strList;
strList << "互联网公司" << "食品公司";
ui->treeWidget->setHeaderLabels(strList);
//设置topItem
QTreeWidgetItem *topItem1 = new QTreeWidgetItem(TYPE_1);
topItem1->setText(0, "百度");
topItem1->setText(1, "蒙牛");
ui->treeWidget->addTopLevelItem(topItem1);
//为topItem添加子Item
QTreeWidgetItem *item1 = new QTreeWidgetItem(TYPE_2);
item1->setText(0, "事业部");
topItem1->addChild(item1);
//为topItem添加子Item
QTreeWidgetItem *item2 = new QTreeWidgetItem(TYPE_2);
item2->setText(0, "营销部");
topItem1->addChild(item2);
//开启右键菜单属性
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
QMenu *myMenu = new QMenu(ui->treeWidget);
QAction *add = myMenu->addAction("添加");
QAction *del = myMenu->addAction("删除");
connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, [=](const QPoint pos){
auto itemList = ui->treeWidget->selectedItems(); //得到选中的itemList,默认情况下是不可以多选的
if(itemList.size() < 1) //异常情况下直接返回
{
return;
}
int type = itemList.first()->type(); //获取选中状态的item的类型
if(type == TYPE_1) //如果是一级item就弹出菜单栏
{
myMenu->exec(ui->treeWidget->mapToGlobal(pos));
}
});
//为两个action设置对应功能
connect(add, &QAction::triggered, this, [=](){
if(dlg.exec() == QDialog::Accepted)
{
QTreeWidgetItem *item = new QTreeWidgetItem(TYPE_2);
item->setText(0, dlg.getPartmentName());
topItem1->addChild(item);
}
});
connect(del, &QAction::triggered, this, [=](){
if(topItem1->childCount() >= 1)
{
topItem1->removeChild(topItem1->child(topItem1->childCount() - 1));
}
});
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Dialog dlg;
};
enum TYPE
{
TYPE_1,
TYPE_2,
};
#endif // MAINWINDOW_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, [&](){
if(ui->lineEdit->text() != "")
{
partmentName = ui->lineEdit->text();
}
this->accept();
});
connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){
this->reject();
});
}
Dialog::~Dialog()
{
delete ui;
}
QString Dialog::getPartmentName()
{
return partmentName;
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QString getPartmentName();
private:
Ui::Dialog *ui;
QString partmentName;
};
#endif // DIALOG_H