Qt右键菜单进阶:自定义功能与灵活位置调整
系统教程50
更新时间:2026-04-03 20:04:24
#include
<QMenu>
//右键菜单
#include
<QCursor>
//右键菜单
//在类中定义
QAction
*
set_par_only;
//右键菜单
private
slots
:
void
print_only(); //曹函数
//在主函数中
this
->set_par_only
=
new
QAction
(tr(
"打印"
),
this
);
//右键菜单,参数
connect(set_par_only,
SIGNAL
(triggered()),
this
,
SLOT
(print_only()));
setContextMenuPolicy(
Qt
::DefaultContextMenu);
//其实不用设置,默认就是这个值
//在这个函数中
void
paint::contextMenuEvent(
QContextMenuEvent
*event)
{
QMenu
*menu
=
new
QMenu
(
this
);
menu->addAction(set_par_only);
menu->move(cursor().pos());
//让菜单显示的位置在鼠标的坐标上
menu->show();
}
//选中该选项后,执行下面的函数
void
paint::print_only()
{
print<<
"not
very
bad!"
;
}
本文发布于:2026-03-04,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义功系统进阶
发布评论