当前位置:网站首页>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());//在当前鼠标位置显示
});
边栏推荐
猜你喜欢

HCIA—應用層

Detailed explanation of NIN network

Gateway is easy to use

OpenShift 部署应用

Rotating linked list (illustration)

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

Minecraft模组服开服

旋转链表(图解说明)

HCIA - data link layer

整理秒杀系统的面试必备!!!
随机推荐
程序猿学英语-Learning C
Minecraft安装资源包
gocv图片读取并展示
What are the platforms for selling green label domain names? What is the green label domain name like?
Smart agriculture solutions smart agriculture system development
STM32-新建工程(参考正点原子)
Pclpy projection filter -- projection of point cloud to cylinder
Qunhui NAS configuring iSCSI storage
Mutex
[blackmail virus data recovery] suffix Rook3 blackmail virus
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
Openshift deployment application
Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
IP protocol and IP address
Nacos 下载启动、配置 MySQL 数据库
Matlab other
ICMP Protocol
PCL calculates the intersection of three mutually nonparallel planes
Minecraft群组服开服