当前位置:网站首页>Qt的右键菜单
Qt的右键菜单
2022-07-02 06:30:00 【代码海贼团船长】
QWidget和继承其类的Qt类通过使用void setContextMenuPolicy(Qt::ContextMenuPolicy)的枚举设置右键菜单,Qt::ContextMenuPolicy的枚举包括 NoContextMenu,DefaultContextMenu,ActionsContextMenu,CustomContextMenu,PreventContextMenu五种类型。
| 枚举 | 类型 |
|---|---|
| Qt::NoContextMenu | 部件右键菜单,右键菜单的处理会传到部件的父级。 |
| Qt::PreventContextMenu | 部件没有右键菜单,与NoContextMenu相反,处理不会延迟到部件的父级。这意味着所有鼠标右键事件都保证通过QWidget::MousePresseEvent()和QWidget::mouseReleaseEvent()传递到小部件本身。 |
| Qt::DefaultContextMenu | 调用部件的QWidget::contextMenuEvent()处理程序。 |
| Qt::ActionsContextMenu | 部件将其QWidget::actions()显示为上下文菜单。 |
| Qt::CustomContextMenu | 部件发出QWidget::customContextMenuRequested()信号。 |
DefaultContextMenu的使用
设置DefaultContextMenu类型后,可重写contextMenuEvent(QContextMenuEvent *event)事件,调用菜单
void ZPushButton::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *mpMenu = new QMenu(this);
QAction* m_actionOne = new QAction(this);
QAction* m_actionTwo = new QAction(this);
QAction* m_actionThree = new QAction(this);
m_actionOne->setText("ActionOne");
m_actionTwo->setText("ActionTwo");
m_actionThree->setText("ActionThree");
mpMenu->addAction(m_actionOne);
mpMenu->addAction(m_actionTwo);
mpMenu->addAction(m_actionThree);
mpMenu->exec(QCursor::pos());//在当前鼠标位置显示
}
ActionsContextMenu的使用
使用Qt::ActionsContextMenu,在类中直接添加QAction
setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* m_actionOne = new QAction(this);
QAction* m_actionTwo = new QAction(this);
QAction* m_actionThree = new QAction(this);
m_actionOne->setText("ActionOne");
m_actionTwo->setText("ActionTwo");
m_actionThree->setText("ActionThree");
addAction(m_actionOne);
addAction(m_actionTwo);
addAction(m_actionThree);
connect(m_actionOne, SIGNAL(triggered()), this, SLOT(actionOneSlot()));
connect(m_actionTwo, SIGNAL(triggered()), this, SLOT(actionTwoSlot()));
connect(m_actionThree, SIGNAL(triggered()), this, SLOT(actionThreeSlot()));
将上述代码添加到窗体部件的构造函数中,即可调用右键菜单。
CustomContextMenu的使用
setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(this,&ZPushButton::customContextMenuRequested,[this](const QPoint &pos){
qDebug()<<"pos:"<<pos;
QMenu *mpMenu = new QMenu(this);
QAction* m_actionOne = new QAction(this);
QAction* m_actionTwo = new QAction(this);
QAction* m_actionThree = new QAction(this);
m_actionOne->setText("ActionOne");
m_actionTwo->setText("ActionTwo");
m_actionThree->setText("ActionThree");
mpMenu->addAction(m_actionOne);
mpMenu->addAction(m_actionTwo);
mpMenu->addAction(m_actionThree);
mpMenu->exec(QCursor::pos());//在当前鼠标位置显示
});
边栏推荐
- Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
- HCIA - data link layer
- kubeadm部署kubernetes v1.23.5集群
- Detailed explanation of NIN network
- Kubedm deploys kubernetes v1.23.5 cluster
- Shortcut key to comment code and cancel code in idea
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
- 整理秒杀系统的面试必备!!!
- 类和对象(类和类的实例化,this,static关键字,封装)
猜你喜欢

OpenShift 容器平台社区版 OKD 4.10.0部署

W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it

Chrome debugging

Openshift deployment application
![[dynamic planning] p4170: coloring (interval DP)](/img/52/76f8baebb19fe10db91c74fec9a697.jpg)
[dynamic planning] p4170: coloring (interval DP)

群辉 NAS 配置 iSCSI 存储

HCIA - data link layer

2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers

Nacos download, start and configure MySQL database
![[blackmail virus data recovery] suffix Hydra blackmail virus](/img/27/f44334cf98229d0f8b33c70a878ca8.jpg)
[blackmail virus data recovery] suffix Hydra blackmail virus
随机推荐
Learning C
Flex layout
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
gocv拆分颜色通道
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Pointer initialization
Gateway 简单使用
[untitled]
类和对象(类和类的实例化,this,static关键字,封装)
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
Minecraft安装资源包
HCIA—應用層
使用wireshark抓取Tcp三次握手
Hcia - Application Layer
Linux安装Oracle Database 19c
Nacos 下载启动、配置 MySQL 数据库