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

一、效果预览

在这里插入图片描述

二、编写目标

  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 <QList>
#include <QString>

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 <QMainWindow>

#include <QVBoxLayout>
#include <QTreeWidget>
#include <QMenu>
  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值