当前位置:网站首页>Right click menu of QT
Right click menu of QT
2022-07-02 08:50:00 【Code pirate captain】
Qt QWidget Right click menu
QWidget And inheriting its class Qt Class by using void setContextMenuPolicy(Qt::ContextMenuPolicy) Enumeration settings right-click menu ,Qt::ContextMenuPolicy The enumeration of includes NoContextMenu,DefaultContextMenu,ActionsContextMenu,CustomContextMenu,PreventContextMenu Five types .
enumeration | type |
---|---|
Qt::NoContextMenu | Part right-click menu , The processing of the right-click menu will be transferred to the parent of the part . |
Qt::PreventContextMenu | There is no right-click menu for components , And NoContextMenu contrary , Processing is not deferred to the parent of the part . This means that all right mouse button events are guaranteed to pass QWidget::MousePresseEvent() and QWidget::mouseReleaseEvent() Passed to the widget itself . |
Qt::DefaultContextMenu | Call the... Of the part QWidget::contextMenuEvent() The handler . |
Qt::ActionsContextMenu | The component will QWidget::actions() Display as context menu . |
Qt::CustomContextMenu | Components send QWidget::customContextMenuRequested() The signal . |
DefaultContextMenu Use
Set up DefaultContextMenu After the type , Rewritable contextMenuEvent(QContextMenuEvent *event) event , Call menu
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());// Displays... At the current mouse position
}
ActionsContextMenu Use
Use Qt::ActionsContextMenu, Add... Directly to the class 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()));
Add the above code to the constructor of the form part , You can call the right-click menu .
CustomContextMenu Use
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());// Displays... At the current mouse position
});
边栏推荐
- File upload and download performance test based on the locust framework
- Openfeign is easy to use
- Linux安装Oracle Database 19c RAC
- Oracle related statistics
- C nail development: obtain all employee address books and send work notices
- Pointer initialization
- Benefits of ufcs of D
- C# 将网页保存为图片(利用WebBrowser)
- Network security - summary and thinking of easy-to-use fuzzy tester
- kubernetes部署loki日志系统
猜你喜欢
sqli-labs第2关
小米电视不能访问电脑共享文件的解决方案
Web技术发展史
Hcia - Application Layer
Analysis of the use of comparable, comparator and clonable interfaces
C language replaces spaces in strings with%20
Installing Oracle database 19C for Linux
Openfeign facile à utiliser
Solid principle: explanation and examples
OpenShift 容器平台社区版 OKD 4.10.0部署
随机推荐
sqli-labs第12关
Qt——如何在QWidget中设置阴影效果
Move a string of numbers backward in sequence
Luogu greedy part of the backpack line segment covers the queue to receive water
Minecraft air Island service
Getting started with k8s: building MySQL with Helm
Loadbalancer dynamically refreshes Nacos server
使用递归函数求解字符串的逆置问题
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
OpenFeign 简单使用
File upload and download performance test based on the locust framework
Makefile Fundamentals
Sqli labs level 8 (Boolean blind note)
Application of kotlin - higher order function
Openshift build image
Minecraft安装资源包
Openfeign is easy to use
HCIA—应用层
小米电视不能访问电脑共享文件的解决方案
Npoi export word font size correspondence