Qt如何让按钮的菜单出现在按钮的右侧

直接上代码,我们用到了一个eventfilter的函数功能。这个函数比较厉害和重要,大家务必经常拿出来看看。

void MainWindow::initMenu()

{

QMenu* menuLiXiang = new QMenu;

QAction* actXiangMuZhangCheng = new QAction("项目章程");

menuLiXiang->addAction(actXiangMuZhangCheng);

ui->btnLiXiang->setMenu(menuLiXiang);

ui->btnLiXiang->setArrowType(Qt::RightArrow);

menuLiXiang->installEventFilter(this);

}

bool MainWindow::eventFilter(QObject * obj, QEvent *event)

{

if (event->type() == QEvent::Show && obj == ui->btnLiXiang->menu())

{

QPoint pos = ui->btnLiXiang->mapToGlobal(ui->btnLiXiang->rect().topRight());

ui->btnLiXiang->menu()->move(pos);

return true;

}

return false;

}

相关推荐
铅笔侠_小龙虾27 分钟前
Rust 学习(4)-函数与注释
开发语言·学习·rust
tiana_40 分钟前
写了个零依赖的 Go 版本管理器,curl | bash 完事
开发语言·golang·bash
嘘嘘出差1 小时前
Python 爬虫入门实战:爬取豆瓣电影 Top 250
开发语言·爬虫·python
爱写代码的倒霉蛋1 小时前
实现协程的三种方式
开发语言·python
CS创新实验室1 小时前
NumPy数组的C风格和Fortran风格
c语言·开发语言·numpy
逝水无殇2 小时前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博2 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
前端H2 小时前
Biome & Rolldown:Rust 工具链接管前端基建
开发语言·前端·rust
天天进步20153 小时前
UI-TARS 源码解析 #4:UI-TARS 与传统 RPA 的区别:为什么它不是简单的坐标点击脚本?
开发语言
运维行者_3 小时前
广域网性能监控:分布式IT架构下的链路质量保障
开发语言·网络·分布式·后端·架构·数据库架构