如何左对齐单击按钮后创建的QMenu?
我希望菜单从按钮的左侧打开
void MyMenu::cppSlot()
{
QMenu *xmenu = new QMenu;
xmenu->addMenu("A -> Setup");
xmenu->addMenu("B -> Setup");
xmenu->addMenu("C -> Setup");
xmenu->addMenu("D -> Setup");
xmenu->addMenu("E -> Setup");
xmenu->addMenu("F -> Setup");
xmenu->addMenu("G -> Setup");
xmenu->setFont(QFont ("Courier", 10));
xmenu->setFixedWidth(250);
xmenu->setAutoFillBackground(true);
QPalette palette=xmenu->palette();
palette.shadow();
xmenu->setPalette(palette);
xmenu->show();
}发布于 2014-11-27 23:15:58
我知道这是一个古老的主题,但我仍然在为未来需要帮助的人提供解决方案。当我想要从按钮的bottom-right角打开菜单时,我遇到了类似的问题。为了实现这一点,我必须重写eventFilter并更改弹出菜单相对于全局坐标的位置。在你的情况下,我假设你需要它在bottom-left上?它应该是这样的:
bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if (event->type() == QEvent::Show && obj == ui->myButton->menu()){
ui->myButton->menu()->move(ui->myButton->mapToGlobal(QPoint(0,ui->helpButton->height())));
return true;
}
return false;
}在代码中的某个地方,最好是在初始化QMenu并将其添加到button之后,必须添加下面这一行:
ui->myButton->setMenu(helpMenu);
helpMenu->installEventFilter(this);发布于 2012-12-10 16:11:16
首先,请阅读QMenu here 的文档。阅读QMenu::exec (),看看你是否能达到你的要求。或者,如果您正在尝试更改menu-indicator子控件的位置,您可以使用Qt Style Sheets,如下所示。
QPushButton::menu-indicator {
image: url(menu_indicator.png);
subcontrol-origin: padding;
subcontrol-position: left;
}https://stackoverflow.com/questions/13796243
复制相似问题