QT学习之窗口右键菜单


QWidget 及其子类都有右键菜单,主要因为其有两个与右键菜单相关联的函数:

Qt::ContextMenuPolicy contextMenuPlicy() const
void setContextMenuPolicy( Qt::ContextMenuPolicy policy)

可以看到这里使用的是一个枚举类型:

Constant Value Description
Qt::NoContextMenu 0 the widget does not feature a context menu, context menu handling is deferred to the widget's parent.
Qt::PreventContextMenu 4 the widget does not feature a context menu, and in contrast to NoContextMenu, the handling is not deferred to the widget's parent. This means that all right mouse button events are guaranteed to be delivered to the widget itself through mousePressEvent(), and mouseReleaseEvent().
Qt::DefaultContextMenu 1 the widget's QWidget::contextMenuEvent() handler is called.
Qt::ActionsContextMenu 2 the widget displays its QWidget::actions() as context menu.
Qt::CustomContextMenu 3 the widget emits the QWidget::customContextMenuRequested() signal.

Qt::NoContextMenu

没有右键菜单,右键点击功能传递给widget的父窗口处理。

Qt::DefaultContextMenu

这个是默认的。利用右键菜单事件 QWidget::contextMenuEvent() 来处理右键事件,所以需要重写此函数!

Qt::ActionsContextMenu

通过动作事件添加右键菜单,(要给动作绑定信号槽!)

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("右键菜单显示"));
//为窗口添加QActions
addAction(new QAction(tr("&Open"), this));
addAction(new QAction(QIcon(":/images/Opt.png"), tr("&Opt"), this));
addAction(new QAction(tr("&Quit"), this));
//设置contextMenuPolicy属性值为 '以Actions为弹出菜单的菜单项组成菜单
setContextMenuPolicy(Qt::ActionsContextMenu);
}
QAction *ascendSortAction = new QAction("升序", this);
QAction *descendSortAction = new QAction("降序", this);
QAction *filterAction = new QAction("过滤", this);
QAction *unfilterAction = new QAction("取消过滤", this);
connect(ascendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_ascend()));
connect(descendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_descend()));
connect(filterAction, SIGNAL(triggered(bool)), this, SLOT(filter_table()));
connect(unfilterAction, SIGNAL(triggered(bool)), this, SLOT(unfilter_table()));
datatable->horizontalHeader()->addAction(ascendSortAction);
datatable->horizontalHeader()->addAction(descendSortAction);
datatable->horizontalHeader()->addAction(filterAction);
datatable->horizontalHeader()->addAction(unfilterAction);

Qt::CustomContextMenu

这个枚举意味着会发出一个信号:

void QWidget::customContextMenuRequested(const QPoint & pos) [signal]

但其只是发送信号,所以要自己去写槽函数slot。信号发出的条件为:

用户鼠标右击且被击中的Widget的contextMenuPolicy 又是Qt::CustomContextMenu

pos是该widget接收右键菜单事件的位置,一般是在该部件的坐标系中。但是对于QAbstratScrollArea及其子类例外,是对应着其视口viewport()的坐标系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子类。

因为仅发信号,所以需自己写显示右键菜单的slot来响应。

例如窗口显示右键菜单槽:

MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
popMenu = new QMenu(this); //popMenu为类私有成员
QAction *addDir = popMenu->addAction("增加目录");
connect(addDir, SIGNAL(triggered(bool)), this, SLOT(popAddDir()));
QAction *addTemplate = popMenu->addAction("增加模板");
connect(addTemplate, SIGNAL(triggered(bool)), this, SLOT(popAddTemplate())); setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(sltShowPopMenu(const QPoint&))); }
void MyWidget::sltShowPopMenu(const QPoint& )//槽函数
{
if(popMenu){
popMenu->exec(QCursor::pos());
}
}

void QWidget::contextMenuEvent ( QContextMenuEvent * event ) [virtual protected]

重写 QWidget 的被保护的虚函数

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("Context Menu Show 2"));
setContextMenuPolicy(Qt::DefaultContextMenu); //其实不用设置,默认就是这个值
}
void MyWidget::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu(this);
menu->addAction(new QAction(tr("&Open"), menu));
menu->addAction(new QAction(QIcon(":/images/mark.png"), tr("&Mark"), menu));
menu->addAction(new QAction(tr("&Quit"), menu));
menu->move(cursor().pos()); //让菜单显示的位置在鼠标的坐标上
menu->show();
}

http://tmjfzy.blog.163.com/blog/static/6644702520126523645391/

http://blog.sina.com.cn/s/blog_98a4dde701013dzh.html

http://blog.csdn.net/addfourliu/article/details/7164923

QT学习之窗口右键菜单的更多相关文章

  1. Qt之自定义QLineEdit右键菜单

    一.QLineEdit说明 QLineEdit是单行文本框,不同于QTextEdit,他只能显示一行文本,通常可以用作用户名.密码和搜索框等.它还提供了一些列的信号和槽,方便我们使用,有兴趣的小伙伴可 ...

  2. C/C++ Qt 给ListWidget增加右键菜单

    在上一篇博文<C/C++ Qt ListWidget 列表框组件应用>中介绍了ListWidget组件的基本使用技巧,本次将给ListWidget组件增加一个右键菜单,当用户在ListWi ...

  3. Qt creator 创建鼠标右键菜单 (不新建类)

    界面 步骤 打开你的界面文件并选中你要添加右键的控件,选择“CustomContextMenu” 右键选择“转到槽...” -> customContextMenuRequested 插入下面代 ...

  4. JAVA GUI学习 - JPopupMenu鼠标右键菜单组件学习

    public class JPopmenuKnow { public void test() { //为表格添加鼠标右键菜单 JMenuItem jMenuItemFileInfo = new JMe ...

  5. JS学习笔记 - 自定义右键菜单、文本框只能输入数字

    <script> // 事件总共有2个部分, //1.点击鼠标右键的表现 oncontextmenu 2.点击鼠标左键的表现(即普通点击onclick) // 点击右键,div位置定位到鼠 ...

  6. 【Qt编程】Qt学习之窗口间的相互切换

    在用Qt设计GUI时,经常要设计两个窗口之间的相互切换,即可以从一个窗口跳转到另一个窗口,然后又从另一个窗口跳转回原窗口.下面我们来介绍具体的实现方法: 工程建立及功能描述: 首先,我们建立Qt  G ...

  7. QT学习之窗口部件

    对话框--QDialog 模态对话框与非模态对话框 模态对话框:就是相当于没关闭它之前,不能再和该应用程序的其他窗口进行交互(比如新建项目时弹出的对话框) 非模态对话框:可以与它交互,也可以与该程序中 ...

  8. Qt学习3---子窗口与父窗口

    创建子窗口后,主窗口的头文件需要  #include "子窗口头文件" 子窗口和父窗口之间相互切换 子窗口没有办法处理父窗口,子窗口此时就需要一个信号: * 信号必须有signal ...

  9. Qt之QAbstractItemView右键菜单

    一.功能概述 说起右键菜单,之前Qt之自定义QLineEdit右键菜单这篇文章中我已经讲述过3种右键菜单的实现方式,今儿也是在啰嗦一下,针对QListWidget类在定制一下右键菜单,我使用的具体方式 ...

随机推荐

  1. linux下安装使用虚拟环境

    一.导语 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同 ...

  2. 前后端分离和restful开发规范

    一.web开发的两种模式 1.前后端不分离 在前后端不分离的应用模式中,前端页面看到的效果都是由后端控制,由后端渲染页面或重定向,也就是后端需要控制前端的展示,前端与后端的耦合度很高. 这种应用模式比 ...

  3. Notepad++编译和运行Java

    首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境(这里可以参考我博客里关于Java环境配置的那篇随笔). 在Notepad++上面的选项栏中找到 插件---> ...

  4. PIE SDK Geometry的坐标转换

    1. 基于SpatialReference对象的坐标转换 1.1 示例简介 Geometry类是所有几何形体对象的父类,它是一个抽象类,IGeometry接口定义了所有的几何对象都有的方法和属性. 下 ...

  5. NFS 网络文件系统

    1, NFS存储服务概念介绍    NFS是Network File System的缩写,中文意思是网络文件系统,    它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录  ...

  6. php编译常见错误

    php PHP编译安装时常见错误解决办法[转] This article is post on https://coderwall.com/p/ggmpfa configure: error: xsl ...

  7. bind-named

    main-book: http://www.zytrax.com/books/dns resolv.conf: http://dns-learning.twnic.net.tw/bind/intro4 ...

  8. (转)8个有力的Awk内建变量

    8个有力的Awk内建变量 翻译原文:8 Powerful Awk Built-in Variableshttp://www.thegeekstuff.com/这个博客真是不错. 这篇文章是Awk Tu ...

  9. UI特效资料-----ShaderWeaver

    主页:www.shaderweaver.com教程:www.shaderweaver.com/tutorials.html 1.一款插件,挺强大的 ShaderWeaver使用教程-基本操作介绍 ht ...

  10. c#-FrameWork02泛型

    泛型 l  泛型(generic)编程是一种编程范式,它利用”参数化类型”将类型抽象化,从而可以实现更为灵活的复用.把数据类型参数化 sh泛型集合 泛型集合与集合的对比 泛型集合类 非泛型集合类 Li ...