16-Qt右键弹出菜单使用示例-支持多级子菜单(QTreeWidget和QListWidget和QWidget)

一、效果预览

16-Qt右键弹出菜单使用示例-支持多级子菜单(QTreeWidget和QListWidget和QWidget)_第1张图片

二、编写目标

  1. 本文是在QTreeWidget的item上进行右键弹出右键菜单的使用示例。右键菜单支持多级子菜单。
  2. 建立多级右键菜单的数据结构和多级子菜单的动态生成方法。
  3. 多级QAction指向一个响应函数,在函数里区别是那个QAction被选中。并从数据结构取出对应的值。

三、源代码

本例子的代码很少,因此把代码直接附录到这里,同时提供了scdn的资源下载,资源里面包括了同样的一份代码。如果有积分直接下载会比较方便一点,避免从这里复制粘贴。
csdn资源地址为:
https://download.csdn.net/download/robert_cysy/12004955

以下为源代码:
deviceextrainfo.h

#ifndef DEVICEEXTRAINFO_H
#define DEVICEEXTRAINFO_H

#include 
#include 

class DeviceExtraInfo
{
public:
    DeviceExtraInfo();

    static QList<QString> GetChipManufacturerTypeNameList();
    static QList<QString> GetLedChipTypeNameList(int manufacturer_type);
    static QList<QString> GetLedColorTypeNameList();

private:

    const static QList<QList<QString>> chip_type_name_array_;
    const static QList<QString> chip_manufacturer_type_name_array_;
    const static QList<QString> led_color_type_name_array_;
};
#endif // DEVICEEXTRAINFO_H

deviceextrainfo.cpp

#include "deviceextrainfo.h"

const QList<QString> DeviceExtraInfo::chip_manufacturer_type_name_array_= {"child1", "child2", "child3", "child4"};
const QList<QList<QString>> DeviceExtraInfo::chip_type_name_array_ = {
    {"grandchild1"}, 
    {"grandchild2", "grandchild3", "grandchild4"}, 
    {"grandchild5", "grandchild6"},
    {"grandchild7", "grandchild8"}
};
const QList<QString> DeviceExtraInfo::led_color_type_name_array_ = {"Red", "Green", "Blue"};

DeviceExtraInfo::DeviceExtraInfo()
{
}

QList<QString> DeviceExtraInfo::GetLedChipTypeNameList(int manufacturer_type) {
    return chip_type_name_array_.at(manufacturer_type);
}

QList<QString> DeviceExtraInfo::GetChipManufacturerTypeNameList() {
     return chip_manufacturer_type_name_array_;
}

QList<QString> DeviceExtraInfo::GetLedColorTypeNameList() {
    return led_color_type_name_array_;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include 
#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    void onItemPressed(QTreeWidgetItem *item, int column);
    void InitMenu();
    void onMenuActionTriggered_ChipType(QAction *action);
    void onMenuActionTriggered_ChipColorType(QAction *action);

private:
    Ui::MainWindow *ui;

    QMenu *main_menu_p;
    QList<QAction *> first_menu_action_list_;
    QList<QAction *> chip_manufacture_action_list_;
    QList<QList<QAction *>> chip_type_action_list_;
    QList<QAction *> chip_color_type_action_list_;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include "deviceextrainfo.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(400, 400);

    InitMenu();

    QVBoxLayout *main_v_layout;
    main_v_layout = new QVBoxLayout(ui->centralwidget);

    QTreeWidget *tree_widget;
    tree_widget = new QTreeWidget(ui->centralwidget);
    tree_widget->setHeaderLabel("header title");
    connect(tree_widget, &QTreeWidget::itemPressed, this, &MainWindow::onItemPressed);
    main_v_layout->addWidget(tree_widget);

    QTreeWidgetItem* group1 = new QTreeWidgetItem(tree_widget);//this pointer is the list to add this item
    group1->setText(0, "haha1");
    group1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    group1->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem* group2 = new QTreeWidgetItem(tree_widget);//this pointer is the list to add this item
    group2->setText(0, "haha2");
    group2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    group2->setCheckState(0,Qt::Unchecked);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onItemPressed(QTreeWidgetItem *item, int column) {
    Q_UNUSED(column)
    if (!(QGuiApplication::mouseButtons() & Qt::RightButton)) return;
     main_menu_p->exec(QCursor::pos());
}

void MainWindow::InitMenu() {
    main_menu_p = new QMenu(this);
    QAction *chip_type_main_action = new QAction(main_menu_p);
    chip_type_main_action->setText(QApplication::translate("MainWindow", "Father"));
    first_menu_action_list_.append(chip_type_main_action);

    QAction *chip_color_type_main_action = new QAction(main_menu_p);
    chip_color_type_main_action->setText(QApplication::translate("MainWindow", "Color"));
    first_menu_action_list_.append(chip_color_type_main_action);

    main_menu_p->addActions(first_menu_action_list_);

    //ic type child menu
    QMenu *child_ic_type_menu = new QMenu();
    QList<QString> chip_manufacture_type_list = DeviceExtraInfo::GetChipManufacturerTypeNameList();
    for (int a = 0; a < chip_manufacture_type_list.count(); a ++) {
        QString name = chip_manufacture_type_list.at(a);
        QAction *action = new QAction();
        action->setText(name);

        //chip child child menu
        QList<QAction*> temp_chip_type_list;
        QList<QString> child_child_chip_type_name_list = DeviceExtraInfo::GetLedChipTypeNameList(a);

        QMenu *child_child_chip_type_menu = new QMenu();
        for (int b = 0; b < child_child_chip_type_name_list.count(); b ++) {
            QString chip_name = child_child_chip_type_name_list.at(b);
            QAction *chip_action = new QAction();
            chip_action->setText(chip_name);
            temp_chip_type_list.append(chip_action);
        }
        connect(child_child_chip_type_menu, &QMenu::triggered, this, &MainWindow::onMenuActionTriggered_ChipType);
        child_child_chip_type_menu->addActions(temp_chip_type_list);
        action->setMenu(child_child_chip_type_menu);
        child_ic_type_menu->addMenu(child_child_chip_type_menu);

        chip_type_action_list_.append(temp_chip_type_list);

        chip_manufacture_action_list_.append(action);
    }

    child_ic_type_menu->addActions(chip_manufacture_action_list_);
    chip_type_main_action->setMenu(child_ic_type_menu);
    main_menu_p->addMenu(child_ic_type_menu);

    //chip color type
    QMenu *child_color_type_menu = new QMenu();
    QList<QString> chip_color_type_list = DeviceExtraInfo::GetLedColorTypeNameList();
    for (int a = 0; a < chip_color_type_list.count(); a ++) {
        QString name = chip_color_type_list.at(a);
        QAction *action = new QAction();
        action->setText(name);
        chip_color_type_action_list_.append(action);
    }
    connect(child_color_type_menu, &QMenu::triggered, this, &MainWindow::onMenuActionTriggered_ChipColorType);
    child_color_type_menu->addActions(chip_color_type_action_list_);
    chip_color_type_main_action->setMenu(child_color_type_menu);
    main_menu_p->addMenu(child_color_type_menu);
}

void MainWindow::onMenuActionTriggered_ChipType(QAction *action) {
    qDebug() << "ChipType";
    for (int a = 0; a < chip_type_action_list_.count(); a ++) {
        QList<QAction *> action_list = chip_type_action_list_.at(a);
        for (int b = 0; b < action_list.count(); b ++) {
            QAction* action_src = action_list.at(b);
            if(action_src == action) {
                qDebug() << "index a = " << a << "  index b = " << b;
                QList<QString> chip_manufacture_type_list = DeviceExtraInfo::GetChipManufacturerTypeNameList();
                QList<QString> child_child_chip_type_name_list = DeviceExtraInfo::GetLedChipTypeNameList(a);

                QString child_name = chip_manufacture_type_list.at(a);
                QString grand_child_name = child_child_chip_type_name_list.at(b);
                qDebug() << "child:" << child_name << "   grand_child:" << grand_child_name;

                return;
            }
        }
    }
}

void MainWindow::onMenuActionTriggered_ChipColorType(QAction *action) {
    qDebug() << "ChipColorType";
    for (int a = 0; a < chip_color_type_action_list_.count(); a ++) {
        QAction *action_src = chip_color_type_action_list_.at(a);
        if(action_src == action) {
            qDebug() << "index a = " << a;
            QList<QString> chip_color_type_list = DeviceExtraInfo::GetLedColorTypeNameList();
            QString color_name = chip_color_type_list.at(a);
            qDebug() << "color :" << color_name;
            return;
        }
    }
}

main.cpp

#include "mainwindow.h"
#include 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

你可能感兴趣的:(Qt)