Skip to content

Weird spacing in QToolButton with both text and icon #28

@JulienMaille

Description

@JulienMaille

I'm trying to understand where in the code I should try to rewrite the spacing/sizing definition in order to get a more natural spaces.
With current code, this is what we get:
image

auto tb0 = new QToolButton;
tb0->setText("Just text");
auto tb1 = new QToolButton;
tb1->setText("Just text");
tb1->setIcon(w->style()->standardIcon(QStyle::SP_DesktopIcon));
tb1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

It looks like the total extra spacing is calculated correctly (the button's width could be ok) but the distribution between the 3 spacing (left, middle, right) is wrong.
Looking at forks, I was hoping @vladimir-kraus or @jcelerier could show me the way :)

As far as I know, it starts here:

case CC_ToolButton: {
auto tbopt = qstyleoption_cast<const QStyleOptionToolButton*>(option);
if (Ph::AllowToolBarAutoRaise || !tbopt || !widget || !widget->parent() ||
!widget->parent()->inherits("QToolBar")) {
QCommonStyle::drawComplexControl(control, option, painter, widget);
break;

Then QCommonStyle::drawComplexControl() will call proxy()->drawControl(CE_ToolButtonLabel, &label, p, widget) which is not handled by PhantomStyle, so it will default to QCommonStyle::drawControl(element, option, painter, widget)

The icon is drawn here (note the comment)

https://github.com/qt/qtbase/blob/6652bf2353d807f724f398a15cb22c188830f57c/src/widgets/styles/qcommonstyle.cpp#L1729-L1733

pr.setWidth(pmSize.width() + 4); //### 4 is currently hardcoded in QToolButton::sizeHint()
tr.adjust(pr.width(), 0, 0, 0);
pr.translate(shiftX, shiftY);
if (!hasArrow) {
    proxy()->drawItemPixmap(p, QStyle::visualRect(opt->direction, rect, pr), Qt::AlignCenter, pm);

PhantomStyle::drawItemPixmap() will call QCommonStyle::drawItemPixmap()

Then the text is drawn here

https://github.com/qt/qtbase/blob/6652bf2353d807f724f398a15cb22c188830f57c/src/widgets/styles/qcommonstyle.cpp#L1739-L1743

tr.translate(shiftX, shiftY);
const QString text = d->toolButtonElideText(toolbutton, tr, alignment);
proxy()->drawItemText(p, QStyle::visualRect(opt->direction, rect, tr), alignment, toolbutton->palette,
    toolbutton->state & State_Enabled, text,
    QPalette::ButtonText);

Which will be handled by PhantomStyle::drawItemText()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions