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;

}

相关推荐
隐积5 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
名字还没想好☜8 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏9 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
酷在前行10 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr82810 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农11 小时前
功能测试--Day07--Python编程基础
开发语言·python
fengci.12 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
程序员zgh12 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
念何架构之路12 小时前
restartmanager-重启管理子系统
java·开发语言
SomeB1oody13 小时前
【RustyML入门】2.0. 经典机器学习
开发语言·后端·机器学习·rust·教程