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;

}

相关推荐
用户805533698031 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner10 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner10 天前
DicomViewer (添加模型类)3
qt
xcyxiner11 天前
DicomViewer (目录调整) 2
qt
xcyxiner11 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00613 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术13 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript