qt增加右键菜单

示例

普通widget增加右键菜单,增加事件过滤器即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
QWidget::eventFilter(QObject* obj, QEvent* e) 
{
if (QEvent::MouseButtonPress == e->type()) {
if (Qt::RightButton == event->button()) {
CreateMenu(event->globalPos());
}
}
}

void CreateMenu(const QPoint& pos)
{
QMenu* menu = new QMenu(/*parent*/);
QAction* action1 = new QAction("a1", menu);
QAction* action2 = new QAction("a2", menu/*, SLOT()*/);
menu->addAction(aciton1);
menu->addSeparator();
menu->addAction(action2);
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(OnMenuTrigged(QAction*)));

// 同步
#if 1
menu->exec(pos);
delete menu;
#else
// 异步
menu->setAttribute(Qt::WA_DeleteOnClose); // 窗口关闭后自行析构
menu->popup(pos);
#endif
}

默认的菜单事件

[virtual protected] void QWidget::contextMenuEvent(QContextMenuEvent *event)

部分控件可以重写该函数,实现自定义菜单

作者

tty

发布于

2023-04-13

更新于

2023-08-02

许可协议