[SOLVED] QMenu and custom painting of the items


  • Qt Champions 2016

    Hi
    How would I go about painting the Menu items ?
    I could not find any delegate for QMenu.

    I overrode
    paintEvent ( QPaintEvent* e )

    But then I was in doubt if to paint all items or what
    the paint event really want me to paint.
    Like when to draw the item highlighted.

    So what is the correct / better way ?


  • Moderators

    The easiest way is to subclass QWidgetAction to return your custom item widget or don't subclass and just set a default widget if there's only one .

    Since it's gonna be your own widget you'll get easy access to all the usual widget events like mouse enter/move/click, the state of it like enabled/disabled/focused etc., which you can use to decide how your widget looks like at any given time.


  • Qt Champions 2016

    @Chris-Kawa
    Ahh, I see
    so my "menuitem" can be any widget I like.

    Is there any way besides
    setStyleSheet ( "QMenu::item { height: 64px; margin: 0px; }" );
    and making a QProxyStyle
    For controlling The height of each "row"?

    Seems to not care for the actual size of the returned widget. :)


  • Moderators

    @mrjj said:

    Seems to not care for the actual size of the returned widget. :)

    What size do you mean?
    The menu will honor both the sizeHint() of the widget and any minimum or maximum size it has set.

    If you mean something like resize() or setGeometry() then yes, they will be ignored, as is true for any widget in a layout.


  • Qt Champions 2016

    @Chris-Kawa
    Hi the size "my widget" when inserted.
    If I make it say 64 it will just be cut and not be shown as 64 pix but seems to be clipped to a default Menu Item height.
    But I see your point about layouts. Did not know the menu was/used one.
    Will try with minimum and see what happens.Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.