QT5 QTreeView添加右键菜单
C++ QT5学习——QTreeView控件创建右键菜单
QTreeView是QWidget的子类,我们再改写QTreeView类的时候,注意的是继承关系。
1.TreeView.h
class TreeView : public QTreeView//记得加public 不然是私有继承
{
Q_OBJECT //使用信号与槽所必需的
public:
TreeView();
public slots:
void slotCustomContextMenu(const QPoint &point);//创建右键菜单的槽函数
};
切入正题。
对于QTreeView实现右键菜单是通过信号与槽实现的。
我们在点击右键的时候会发生customContextMenuRequested(const QPoint &)信号。我们根据这个信号创建菜单就行了
2.TreeView.cpp
TreeView::TreeView() :QTreeView() //构造函数
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(slotCustomContextMenu(const QPoint &)));
}
void TreeView::slotCustomContextMenu(const QPoint &point) //槽函数定义
{
QMenu *menu = new QMenu(this);
QAction *a1=new QAction(tr("上传"));
menu->addAction(a1);
QAction *a2=new QAction(tr("移动"));
menu->addAction(a2);
QAction *a3=new QAction(tr("复制"));
menu->addAction(a3);
QAction *a4=new QAction(tr("删除"));
menu->addAction(a4);
menu->exec(this->mapToGlobal(point));
}
这样就实现了右键的菜单显示
3.效果显示

QT5 QTreeView添加右键菜单的更多相关文章
- pyqt5-为QListWidget添加右键菜单
如何在pyqt5下为QListWidget添加右键菜单? 能百度到的均是pyqt4下的,有些貌似并不好用. 在尝试了很多方法后,下面贴出可用的方法: from PyQt4 import QtCore, ...
- 添加右键菜单命令 在此处打开命令窗口(E)(带图标)
@color 0A @title 添加右键菜单命令 在此处打开命令窗口(^&E)(带图标) by wjshan0808 @echo off reg add HKCR\Directory\Bac ...
- 仅在TabControl中的Tab中添加右键菜单
若想实现仅在TabControl中的Tab中添加右键菜单,可在XAML中通过使用样式得到: <TabControl> <TabControl.ItemContainerStyle&g ...
- [cb] Unity Editor 添加右键菜单
需求 为Unity的Editor窗口添加右键菜单 实现代码 // This example shows how to create a context menu inside a custom Edi ...
- Arcengine 二次开发添加右键菜单
最近在搞arcengine 二次开发,遇到了好多问题,也通过网上查资料试着慢慢解决了,把解决的步骤记录下来,有需要帮助的可以看一下,也欢迎各位来批评指正. 想给自己的map application在图 ...
- Beyond Compare 3添加右键菜单
目前是在Beyond Compare 3.1.9版本上试验可行,其他版本上尚未测试. 添加右键菜单步骤: 1.新建为.bat后缀的文本,将下面“添加右键菜单批处理”复制到此文本中. 2.将批处理移动到 ...
- 给tkinter文本框添加右键菜单
给tkinter文本框添加右键菜单 需求:直接右键点击使用tkinter创建的文本框是不会弹出菜单的.我们需要实现右键点击tkinter框架下的Entry对象.Text对象后弹出右键菜单可复制.粘贴和 ...
- DevExpress使用教程:XtraGridControl动态添加右键菜单
在使用 GridControl 的时候经常需要添加右键菜单.一般的做法是自己创建菜单项,然后注册GridView的Mouse-Click事件,然后Show出定义好的菜单.但是涉及到一些单击事件会收到编 ...
- 『实践』百度地图给map添加右键菜单(判断是否为marker)
var map; var s;//经度 var w;//纬度 $(document).ready(function(){ $(".mune").load("jsp/c ...
随机推荐
- java对象的方法属性和代码块的加载顺序
1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性 for example: package com.JavaTest2; publi ...
- CentOS 7 关闭启动防火墙
关闭firewall:systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewa ...
- git比较重要但是又容易忘记的操作
git rebase head~1 把多次commit合并成一次 git reset head 撤销已缓存的内容 git checkout . git stash 去除改动未提交的代码
- Mybatis-第N篇配置log4j1、log4j2打印执行的sql语句
1.log4j1配置 目录结构: conf.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- Collections与Arrays工具类
Collections工具类: 排序操作: void reverse(List list)//反转 void shuffle(List list)//随机排序 void sort(List list) ...
- 3486 ( Interviewe )RMQ
Problem Description YaoYao has a company and he wants to employ m people recently. Since his company ...
- web前端工程化
目标 1.能够了解模块化的相关规范 2.了解webpack 3.了解使用Vue单文件组件 4.能够搭建Vue脚手架 5.掌握Element-UI的使用 1.模块化的分类 A.浏览器端的模块化 1).A ...
- adb 链接网络 connect 安装apk install 断开IP链接 kill-server 连接数devices
https://blog.csdn.net/zhonglunshun/article/details/78362439 ./adb connetc 192.168.1.11 ./adb install ...
- 一、Rabbit使用-安装教程
首先我去官网上面下载RabbitMQ安装包:https://www.rabbitmq.com/which-erlang.html 现在下载的版本是3.7.17 因为我erlong安装的是20.3
- 2018-8-10-WPF-控件继承树
title author date CreateTime categories WPF 控件继承树 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 17:23: ...