Results 1 to 2 of 2

Thread: QPushButton with menu -> How to set position of the menu?

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QPushButton with menu -> How to set position of the menu?

    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

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QPushButton with menu -> How to set position of the menu?

    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

    Qt Code:
    1. QMenu* menu = new Menu();
    2. somePushButton->setMenu(menu);
    3. menu->installEventFilter(this);
    4.  
    5. bool myWidget::eventFilter(QObject * obj, QEvent *event)
    6. {
    7. if (event->type() == QEvent::Show && obj == somePushButton->menu())
    8. {
    9. QPoint pos = calculateposition
    10. somePushButton->menu()->move(pos);
    11. return true;
    12. }
    13. return false;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Berryblue031; 18th May 2012 at 12:40.

Similar Threads

  1. how to change the position of menu of QMainWindow
    By yxmaomao in forum Qt Programming
    Replies: 11
    Last Post: 17th September 2016, 17:12
  2. Right-click menu position
    By thuswa in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2009, 23:00
  3. QPushButton PopUp menu
    By Preeteesh in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2008, 18:03
  4. QPushButton - menu
    By hgedek in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2007, 15:33
  5. How to get a position of the menu bar item?
    By Mad Max in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2006, 04:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.