QToolbar stylesheet



  • Hello everyone, when a toolbar has too many buttons or resizing the window, you see a button to display the missing buttons. (See image below)
    Screenshot
    I wanted to know if it is possible to increase the width. Looking at the documentation I found nothing, maybe I have not tried the exact name.
    Thank you



  • @Gianluca86 said:

    I wanted to know if it is possible to increase the width

    This you can do using set Geometry, toolbar->setGeometry(x,y,width,height)

    Are you looking some thing like resize the toolbar basing on the mainWindow size? or fixed width of toolbar?



  • No, maybe I explained badly. I will not enlarge the toolbar, but the size of the ">>" button that is circled in red in the image.


  • Moderators

    @Gianluca86
    example QSS selector:

    QToolBar > QToolBarExtension#qt_toolbar_ext_button {
        ...
    }
    

    now you can play around with the min-width, margin, etc properties



  • I tried:

    MainToolBar.setStyleSheet("QToolBar > QToolBarExtension#qt_toolbar_ext_button { background-color: red; min-width: 28px;}");
    

    It seems to work, but cuts the button.
    Screenshot


  • Moderators

    @Gianluca86
    ah sorry my fault.
    This can't be achieved with QSS only.
    You would need to subclass QProxyStyle and reimplement pixelMetric().
    In there return the desired value for QStyle::PM_ToolBarExtensionExtent

    But i am afraid you can't use QProxyStyle in combination with stylesheets (anywhere in the parent hierarchy), since they rid themselves out mutually.



  • Ok, I tried this:

    // MyStyle.h
    #include <QStyle>
    
    class MyStyle : public QStyle
    {
    public:
        MyStyle();
        virtual int pixelMetric(PixelMetric pm, const QStyleOption* option, const QWidget* widget) const ;
    };
    
    // MYStyle.cpp
    #include "MyStyle.h"
    
    MyStyle::MyStyle()
    {
    
    }
    
    int MyStyle::pixelMetric(PixelMetric pm, const QStyleOption* option, const QWidget* widget) const
    {
      if (pm == QStyle::PM_ToolBarExtensionExtent)
        return 50; 
      return QStyle::pixelMetric(pm, option, widget);
    }
    

    Is it correct?

    The line

    return QStyle::pixelMetric(pm, option, widget);
    

    give me this error:
    error: undefined reference to `QStyle::pixelMetric(QStyle::PixelMetric, QStyleOption const*, QWidget const*) const'


Log in to reply
 

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