Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird spacing in QToolButton with both text and icon #28

Open
JulienMaille opened this issue Dec 14, 2021 · 0 comments
Open

Weird spacing in QToolButton with both text and icon #28

JulienMaille opened this issue Dec 14, 2021 · 0 comments

Comments

@JulienMaille
Copy link

JulienMaille commented Dec 14, 2021

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant