Change height and icon position of QToolBox tabs,

2019-08-01 05:55发布

问题:

Ok, so I want to customize the height and the position of icons of the tabs from a QToolBox widget. I am not able to do it using stylesheet (I tried different options, but none worked, see question: Customizing QToolBox: tab height), so I decided to give it a try to using a proxy. I know there are the controlElements:

    CE_ToolBar
    CE_ToolBoxTabShape
    CE_ToolBoxTabLabel

And so I can use them on the drawControl function to get what I want.

void drawControl(ControlElement oCtrElement,
            const QStyleOption *poStyleOptionption,
            QPainter *poPainter,
            const QWidget *poWidget) const

But I am not sure how to implement it... Any help or example? Thanks,


UPDATE: I have created the proxy, assigned it to the qtoolbox and added conditions to see if I could do something:

    ...
    ToolBoxProxy* tabStyle = new ToolBoxProxy();
    ui->toolBox->setStyle(tabStyle);
    ...

toolboxproxy.h

#include <QProxyStyle>
#include <QPainter>

class ToolBoxProxy : public QProxyStyle
{
public:

    explicit ToolBoxProxy();
    void drawControl(ControlElement oCtrElement, const QStyleOption *             poStylrOptionption, QPainter * poPainter, const QWidget * poWidget = 0) const;

};

toolboxproxy.cpp

#include "toolboxproxy.h"
#include <QDebug>

ToolBoxProxy::ToolBoxProxy(){

}

void ToolBoxProxy::drawControl(ControlElement oCtrElement, const QStyleOption *poStyleOptionption, QPainter *poPainter, const QWidget *poWidget) const
{
    if (oCtrElement == CE_ToolBar) {
        qDebug() << "CE_ToolBar";

    } else if (oCtrElement == CE_ToolBoxTabShape) {
        qDebug() << "CE_TOOLBOXTABSHAPE";

    } else if (oCtrElement == CE_ToolBoxTabLabel) {
        qDebug() << "CE_ToolBoxTabLabel";

    }
    QProxyStyle::drawControl(oCtrElement, poStyleOptionption, poPainter, poWidget);
}

I don't know why, but the software never prints out any of the 3 qDebug()... but if I set a breakpoint, it only reaches the function when the control element is any of those:

CE_ItemViewItem
CE_ShapedFrame
CE_ToolButtonLabel
标签: qt proxy toolbox