PDA

View Full Version : QPushButton with menu -> How to set position of the menu?



olidem
18th May 2012, 08:18
Hi there,

i've got a QPushButton with a menu (setMenu()).
That works fine, but:

Currently, when opening the menu, it is left-aligned with the button.
So, the menu is shown below the button and its left border has the same x-coodinate as the button's left border.

I would like to right-align the menu, so that the menu' and button's right border have the same x-coordinate.

I tried the property subcontrol-position in QPushButton::menu-indicator, but it does not help.

Thanks for your help in advance.

Olli

Berryblue031
18th May 2012, 11:36
Stylesheets won't help you here :) QPushButton::menu-indicator is to style the little dropdown arrow you can draw on the pushbutton to show there is a menu ( I believe anyways )

Set an event filter on your menu, and re-position it on show



QMenu* menu = new Menu();
somePushButton->setMenu(menu);
menu->installEventFilter(this);

bool myWidget::eventFilter(QObject * obj, QEvent *event)
{
if (event->type() == QEvent::Show && obj == somePushButton->menu())
{
QPoint pos = calculateposition
somePushButton->menu()->move(pos);
return true;
}
return false;
}



If this is something you want to do in general on all of your QPushButtons you should subclass QPushButton and apply the menu event filter in the setMenu function, then use promotion in designer to always create your custom push buttons instead.