Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qmenu icon size



  • Is it possible to change the qmenu icon size using a style sheet?


  • Lifetime Qt Champion

    Hi,

    Take a look here for tips on how to customize a QMenu. Note that depending on the OS you are using, these modifications might not apply e.g. on OS X.


  • Moderators

    @ravas
    The icon size of a QMenu is taken from the set style and not from a widget property.

    So for example you can't use something like

    QMenu {
    qproperty-iconSize: 16px 16px;
    }
    

    Instead it's taken from the set (platform-)style itself.

    So you can try to implement a QProxyStyle:

    class MyProxyStyle: public QProxyStyle
    {
    	Q_OBJECT
    
    	public:
                   MyProxyStyle(QStyle * style = 0) : QProxyStyle(style)
                   {
                   }
    
    	       MyProxyStyle(const QString & key) : QProxyStyle(key)
                   {
                   }
    
    	       virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const
                   {
    		        switch ( metric )
                            {
                                  case QStyle::PM_SmallIconSize:
                                           return 20;
                                  default:
    			              return QProxyStyle::pixelMetric( metric, option, widget );
                            }
    	}
    };
    

    Not sure if it will be ignored when your widget also has a (inherited) QStylesheetStyle set. Since a QStylesheetStyle is also a proxy style and has a special handling in QWidget.



  • @raven-worx Thanks. Is there any chance of adding functions to QMenu, such as setTextSize and setIconSize? Or maybe just one of those, which automatically sets the other. (I'm aware we can set the text size via style sheet.)


  • Lifetime Qt Champion

    Very highly unlikely, Qt follows the platform style and thus the settings your users have.

    Note that modifying menus text size and icon size is likely to interfere with most platforms human interface guidelines which is usually a bad idea.



  • Whow! I just stumbled across this after all these years. I remember I had this problem once and now again. This time I actually managed to solve it somewhat. It IS kinda weird tho and should receive some love at least documentation-wise.

    The key is: You need to style QMenu AND QMenu::item If you just set the icon size via:

    QMenu {icon-size: 40px;}
    

    it will remain ignored until you also set something like

    QMenu::item {background: transparent;}
    

    Unfortunately this resets the menu stylesheet and you need to do something about the hover state to make it usable. But well.
    Seems this works for me.
    Can someone confirm please?


Log in to reply