当前位置:网站首页>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());//在当前鼠标位置显示
});
边栏推荐
- 双向链表的实现(双向链表与单向链表的简单区别联系和实现)
- The best blog to explain the basics of compilation (share)
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- sqli-labs第2关
- W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
- Programming ape learning English - imperative programming
- 文件上传-upload-labs
- HCIA—應用層
- Short video with goods source code, double-click to zoom in when watching the video
- Comparable,Comparator,Clonable 接口使用剖析
猜你喜欢

Hcia - Application Layer

Googlenet network explanation and model building

ICMP协议

Development of digital collection trading website development of metauniverse digital collection

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

C language custom type enumeration, Union (clever use of enumeration, calculation of union size)

OpenShift 部署应用

Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings

k8s入门:Helm 构建 MySQL
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]
随机推荐
C language replaces spaces in strings with%20
sqli-labs第8关(布尔盲注)
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
文件上传-upload-labs
IP协议与IP地址
Solid principle: explanation and examples
TCP/IP—传输层
c语言将字符串中的空格替换成%20
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
commands out of sync. did you run multiple statements at once
Minecraft插件服开服
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
The best blog to explain the basics of compilation (share)
Learning C
Zipkin is easy to use
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Sqli labs level 8 (Boolean blind note)
Makefile Fundamentals
Detailed explanation of NIN network
[blackmail virus data recovery] suffix Rook3 blackmail virus