当前位置:网站首页>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());//在当前鼠标位置显示
});
边栏推荐
- CarSim learning experience - rough translation 1
- Openshift deployment application
- Minecraft插件服开服
- Simple implementation scheme of transcoding and streaming (I)
- 16: 00 interview, came out at 16:08, the question is really too
- Matlab-其它
- Analysis of the use of comparable, comparator and clonable interfaces
- How to uninstall SQL Server cleanly
- ICMP协议
- Linux安装Oracle Database 19c RAC
猜你喜欢

How to build the alliance chain? How much is the development of the alliance chain

顺序表基本功能函数的实现

Smart agriculture solutions smart agriculture system development

web安全--逻辑越权

Openfeign is easy to use

Tcp/ip - transport layer

kubernetes部署loki日志系统

Nacos download, start and configure MySQL database

文件上传-upload-labs

Openfeign facile à utiliser
随机推荐
gocv图片读取并展示
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
Web技术发展史
Minecraft群组服开服
Don't know mock test yet? An article to familiarize you with mock
Qunhui NAS configuring iSCSI storage
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
Kubedm deploys kubernetes v1.23.5 cluster
Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
ICMP协议
Nacos 下载启动、配置 MySQL 数据库
KubeSphere 虚拟化 KSV 安装体验
First week of JS study
Routing foundation - dynamic routing
Realization of basic function of sequence table
使用wireshark抓取Tcp三次握手
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
Short video with goods source code, double-click to zoom in when watching the video
When a custom exception encounters reflection
Minecraft group service opening