QtreeWidget简介「建议收藏」

QtreeWidget简介「建议收藏」设置右键菜单并实现添加一个子项删除一个子项的功能这样有两个缺点1.只能添加特定的子项。2.不能实现不同层级节点的不同菜单。dialog.cppwidget.cppwidget.h还可以通过TYPE属性来确定每一个节点的层级。代码实现mainwindow.cppmainwindow.hdialog.cppdialog.h参考博客…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

QtreeWidget简介

QtreeWidget会有一个默认的 columncount的property。如下:

在这里插入图片描述

    //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的头部

Jetbrains全家桶1年46,售后保障稳定

设置右键菜单并实现添加一个子项删除一个子项的功能

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. 不能实现不同层级节点的不同菜单。

  1. 通过弹出dialog的方式询问用户,添加用户自定义的子项
    添加一个dialog界面
    在这里插入图片描述
    dialog.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QString>

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 <QString>
#include <QStringList>
#include <QMenu>
#include <QPoint>
#include <QDebug>

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 <QWidget>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#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 <QMainWindow>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QString>
#include <QStringList>
#include <QMenu>
#include <QAction>
#include <QPoint>
#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 <QDialog>
#include <QString>

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

在这里插入图片描述
参考博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/226856.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号