• QT之 给控件添加右键菜单


    一、效果预览

    在这里插入图片描述

    二、代码

    cpp文件

    //listView右键菜单
    void MainWindow::Rightclicklistview()
    {
    
    
        //初始化一级菜单
        TotalRightclick =new QMenu(this);
        AddDevice = new QMenu(this);
        upDevice = new QAction(this);
        DownDevice = new QAction(this);
        Delete = new QAction(this);
        EditDevice = new QAction(this);
    
        //初始化二级菜单
        DeviceIS6201A =new QMenu(this);
        DeviceIS6202A = new QMenu(this);
        DeviceIS6203A = new QMenu(this);
    
        //初始化三级菜单
        DeviceIS6201A_IIC = new QAction(this);
        DeviceIS6201A_PMBV1 = new QAction(this);
        DeviceIS6201A_PMBV2= new QAction(this);
    
        //各级图标
        //一级图标
        QIcon iconadd = QIcon(":/Res/tools/add.png");  //加载图片
        QIcon iconUp = QIcon(":/Res/tools/moveup.png");  //加载图片
        QIcon iconDown = QIcon(":/Res/tools/movedown.png");  //加载图片
        QIcon iconDelete = QIcon(":/Res/tools/delete.png");  //加载图片
        QIcon iconEdit = QIcon(":/Res/tools/paste.png");  //加载图片
        
        
        //二级图标
        QIcon iconUser = QIcon(":/Res/tools/user.png");  //加载图片
        
        //三级图标
        QIcon iconChip = QIcon(":/Res/tools/xinpian.png");  //加载图片
    
        //子对象在父对象里面从属关系匹配
        AddDevice  = TotalRightclick->addMenu(iconadd,"  Add Device");
        upDevice = TotalRightclick->addAction(iconUp,"  Move Up Device");
        DownDevice = TotalRightclick->addAction(iconDown,"  Move Down Device");
        Delete = TotalRightclick->addAction(iconDelete,"  Delete Device");
        EditDevice = TotalRightclick->addAction(iconEdit,"  Edit Device");
    
        DeviceIS6201A = AddDevice->addMenu(iconUser,"  IS6201A");
        DeviceIS6202A = AddDevice->addMenu(iconUser,"  IS6202A");
        DeviceIS6203A = AddDevice->addMenu(iconUser,"  IS6203A");
    
        DeviceIS6201A_IIC = DeviceIS6201A->addAction(iconChip,  "  IS6201A_IIC");
        DeviceIS6201A_PMBV1 = DeviceIS6201A->addAction(iconChip,"  IS6201A_PMBV1");
        DeviceIS6201A_PMBV2= DeviceIS6201A->addAction(iconChip, "  IS6201A_PMBV2");
    
    
          //给动作设置信号槽
          connect( upDevice, &QAction::triggered, [=]()
          {
              qDebug()<<"I'm upDevice";
          });
          connect( DownDevice, &QAction::triggered, [=]()
          {
              qDebug()<<"I'm DownDevice";
          });
          //给动作设置信号槽
          connect( Delete, &QAction::triggered, [=]()
          {
    		}
    
    
              qDebug()<<"I'm DeleteAction";
          });
          connect( EditDevice, &QAction::triggered, [=]()
          {
              qDebug()<<"I'm btnSecondAction";
          });
    
    
      
          connect( DeviceIS6201A_PMBV1, &QAction::triggered, [=]()
          {
     		 qDebug()<<"I'm DeviceIS6201A_PMBV1";
          });
    
          connect( DeviceIS6201A_PMBV2, &QAction::triggered, [=]()
          {
              qDebug()<<"I'm DeviceIS6201A_PMBV2";
          });
    
    
    
          //绑定控件显示右键菜单给控件设置上下文菜单策略
          ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
          //鼠标右键点击控件时会发送一个void QWidget::customContextMenuRequested(const QPoint &pos)信号
          //给信号设置相应的槽函数
          connect(ui->listView,&QLabel::customContextMenuRequested,[=](const QPoint &pos)
          {
              qDebug()<<pos;//参数pos用来传递右键点击时的鼠标的坐标,这个坐标一般是相对于控件左上角而言的
              TotalRightclick->exec(QCursor::pos());
          });
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    h文件

    private:
    
        void Rightclicklistview();
        // 声明动作
        QAction * upDevice;
        QAction * DownDevice;
        QAction * Delete;
        QAction * EditDevice;
        QAction * DeviceIS6201A_IIC;
        QAction * DeviceIS6201A_PMBV1;
        QAction * DeviceIS6201A_PMBV2;
    
    
        //声明菜单
        QMenu * TotalRightclick;
        QMenu * AddDevice;
        QMenu * DeviceIS6201A;
        QMenu * DeviceIS6202A;
        QMenu * DeviceIS6203A;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    文档解释参考一

    参考二

    参考三

  • 相关阅读:
    私有云:【3】NFS存储服务器的安装
    【Gradio-Windows-Linux】解决share=True无法创建共享链接,缺少frpc_windows_amd64_v0.2
    【无标题】
    [Pyhton] SimPy 离散事件模拟框架详解 —— 以一个简单的汽车充电排队模拟为例
    《STM32 HAL库》RCC 相关系列函数详尽解析—— HAL_RCC_OscConfig()
    单链表OJ题——10.环形链表2
    【申博攻略】六.如何联系心仪的导师以及前期注意事项
    计算机网络(八) | Tomcat
    什么是GraphQL?它与传统的REST API有什么不同?
    23.基于springboot + vue实现的前后端分离-在线旅游网站系统(项目 + 论文PPT)
  • 原文地址:https://blog.csdn.net/liqifff/article/details/128135974