QMenu菜单

QMenu是一个Top-Level的构件,menu.exec(pos)这里的pos总是相对于屏幕的。

示例1: 向上弹出

1 void Dialog::on_btn_clicked()
2 {
3     QPoint pos;
4     pos.setX(0);
5     pos.setY(-menu->sizeHint().height());
6     menu->exec(ui->btnOperator->mapToGlobal(pos));
7 }

效果:

示例2:

 1 void MainWindow::showOptionPopMenu()
 2 {
 3     this->setContextMenuPolicy(Qt::CustomContextMenu);
 4     QMenu menu;
 5     //添加菜单项,指定图标、名称、响应函数
 6     menu.addAction(QIcon(":/Image/upgrade.png"), QStringLiteral("检测升级"), this, SLOT(onCheckUpgradeAction()));
 7     menu.addSeparator();
 8     menu.addAction(QIcon(":/Image/about.png"), QStringLiteral("关于软件"), this, SLOT(onShowAboutAction()));
 9     //在鼠标位置显示
10     QPoint pos;
11     pos.setX(this->x() + ui.titleBar->width() - 143);
12     pos.setY(this->y() + ui.titleBar->height() - 10);
13     menu.exec(pos); //QCursor::pos()
14 }

效果:
原文地址:https://www.cnblogs.com/acmexyz/p/11677693.html