博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt_自定义菜单
阅读量:6640 次
发布时间:2019-06-25

本文共 2848 字,大约阅读时间需要 9 分钟。

一、右键菜单

右键菜单实现:通过重写contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美实现!

重写voidcontextMenuEvent(QContextMenuEvent*event)事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void 
Form::contextMenuEvent(QContextMenuEvent *
event
)
{
    
//创建菜单、菜单项
    
QMenu *pop_menu = 
new 
QMenu();
    
QAction *add_images_action = 
new 
QAction(
this
);
    
QAction *add_folder_action = 
new 
QAction(
this
);
    
QAction *remove_selected_action = 
new 
QAction(
this
);
    
QAction *remove_all_action = 
new 
QAction(
this
);
    
add_images_action->setText(tr(
"add images"
));
    
add_folder_action->setText(tr(
"add folder"
));
    
remove_selected_action->setText(tr(
"remove selected images"
));
    
remove_all_action->setText(tr(
"remove all images"
));
    
//清除原有菜单
    
pop_menu->addAction(add_images_action);
    
pop_menu->addAction(add_folder_action);
    
pop_menu->addAction(remove_selected_action);
    
pop_menu->addAction(remove_all_action);
    
//菜单出现的位置为当前鼠标的位置
    
pop_menu->exec(QCursor::pos());
}

二、按钮菜单

使用QPushButton(QToolButton)+QMenu+QAction即可完美实现!

界面拖入一个pushButton按钮;代码实现

1
2
3
4
5
6
7
8
9
QMenu *remove_menu = 
new 
QMenu();
 
QAction *remove_selected_action = 
new 
QAction(remove_menu);
 
QAction *remove_all_action = 
new 
QAction(remove_menu);
 
remove_menu->addAction(remove_selected_action);
 
remove_menu->addAction(remove_all_action);
 
ui->pushButton->setMenu(remove_menu);
 
ui->pushButton->setText(tr(
"remove"
));
 
remove_selected_action->setText(tr(
"remove selected watermarks"
));
 
remove_all_action->setText(tr(
"remove all watermarks"
));

三、托盘菜单

托盘菜单实现:通过QSystemTrayIcon+QMenu+QAction实现;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
QSystemTrayIcon *system_tray = 
new 
QSystemTrayIcon();
//放在托盘提示信息、托盘图标
system_tray ->setToolTip(QString(
"我就是托盘"
));
system_tray ->setIcon(QIcon(
":/sx1.png"
));
//TrayMenu *tray_menu = new TrayMenu();
QMenu *remove_menu = 
new 
QMenu();
QAction *remove_selected_action = 
new 
QAction(remove_menu);
QAction *remove_all_action = 
new 
QAction(remove_menu);
remove_menu->addAction(remove_selected_action);
remove_menu->addAction(remove_all_action);
remove_selected_action->setText(tr(
"remove selected watermarks"
));
remove_all_action->setText(tr(
"remove all watermarks"
));
system_tray->setContextMenu(remove_menu);
 
//点击托盘执行的事件
connect(system_tray , SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 
this
, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
     
connect(remove_menu, SIGNAL(showWidget()), 
this
, SLOT(showNormal()));
//显示托盘
system_tray->show();
//托盘显示提示信息
system_tray->showMessage(QString(
"托盘标题"
), QString(
"托盘显示内容"
));

注:关闭和最小化进入到托盘代码

1
2
3
4
5
6
7
8
9
10
11
12
13
void 
Form::closeEvent(QCloseEvent *e)
{
    
this
->hide();
    
e->ignore();
}
void 
Form::changeEvent(QEvent *e)
{
    
if
((e->type()==QEvent::WindowStateChange)&&
this
->isMinimized())
    
{
        
this
->hide();
        
e->ignore();
    
}
}

 

本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/p/3365617.html,如需转载请自行联系原作者
你可能感兴趣的文章
Linux常用命令整理
查看>>
数据的间距问题(重载+函数模板)
查看>>
利用Python实现从百度下载图片到本地磁盘
查看>>
ubuntu18.04安装mac主题
查看>>
Log4j的简单使用
查看>>
大数据实践-数据同步篇tungsten-relicator(mysql->mongo)
查看>>
定时任务发展史(一)
查看>>
rails跨域请求配置
查看>>
【枚举】AtCoder Regular Contest 095 C - Symmetric Grid
查看>>
比较与分析Groovy与Java
查看>>
expect用法指南
查看>>
天坑之旅之--restful设计(express,mongoose)
查看>>
Response.Redirect引起的“无法在发送HTTP标头之后进行重定向”
查看>>
apk获取源码(dex2jar、jd-gui)
查看>>
plsql中文乱码
查看>>
贪心算法之最优装载
查看>>
codeforces 609C Load Balancing
查看>>
Aizu 2300 Calender Colors(暴力)
查看>>
$rootScope
查看>>
Asp.net core 学习笔记 ( DI 依赖注入 )
查看>>