当前位置:网站首页>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());//在当前鼠标位置显示
});
边栏推荐
- What are the platforms for selling green label domain names? What is the green label domain name like?
- When a custom exception encounters reflection
- [untitled]
- 链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
- Openshift deployment application
- 群辉 NAS 配置 iSCSI 存储
- Pointer initialization
- STM32-新建工程(参考正点原子)
- gocv拆分颜色通道
- OpenFeign 簡單使用
猜你喜欢
IP protocol and IP address
Openfeign facile à utiliser
类和对象(类和类的实例化,this,static关键字,封装)
Sqli labs (post type injection)
Jumping | Blue Bridge Cup
ARP及ARP欺骗
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
sqli-labs第1关
Linux安装Oracle Database 19c
[dynamic planning] p4170: coloring (interval DP)
随机推荐
Getting started with k8s: building MySQL with Helm
IP protocol and IP address
OpenShift 容器平台社区版 OKD 4.10.0部署
Nacos 下载启动、配置 MySQL 数据库
Use Wireshark to grab TCP three handshakes
程序猿学英语-Learning C
gocv图片读取并展示
Openshift build image
Minecraft模组服开服
Network security - summary and thinking of easy-to-use fuzzy tester
Matlab other
k8s入门:Helm 构建 MySQL
Realize bidirectional linked list (with puppet node)
Web security -- Logical ultra vires
Shortcut key to comment code and cancel code in idea
Programming ape learning English - imperative programming
Minecraft安装资源包
Sqli labs (post type injection)
Honeypot attack and defense drill landing application scheme
File upload Labs