Skip to content

Commit

Permalink
QToolBar: Update implicit icon size if style changes
Browse files Browse the repository at this point in the history
Add logic in QToolBar::changeEvent() to update icon size set via
stylesheet.

Fixes: QTBUG-45949
Pick-to: 6.8 6.9
Change-Id: I7fce830a969af8774116f0229153668252b55598
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
  • Loading branch information
Dheerendra Purohit authored and vohi committed Dec 10, 2024
1 parent 3bec937 commit 1873335
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/widgets/widgets/qtoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,13 @@ void QToolBar::changeEvent(QEvent *event)
break;
case QEvent::StyleChange:
d->layout->invalidate();
if (!d->explicitIconSize)
setIconSize(QSize());
if (!d->explicitIconSize) {
QStyleOptionToolBar opt;
initStyleOption(&opt);
const int metric = style()->pixelMetric(QStyle::PM_ToolBarIconSize, &opt, this);
setIconSize({metric, metric});
d->explicitIconSize = false;
}
d->layout->updateMarginAndSpacing();
break;
case QEvent::LayoutDirectionChange:
Expand Down
41 changes: 41 additions & 0 deletions tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private slots:
void task197996_visibility();

void extraCpuConsumption(); // QTBUG-54676
void iconSizeStyleSheet();
};


Expand Down Expand Up @@ -1164,5 +1165,45 @@ void tst_QToolBar::extraCpuConsumption()
QCOMPARE(eventCounter->hideEventsCount(), uint(1));
}

void tst_QToolBar::iconSizeStyleSheet()
{
auto resetStyleSheet = qScopeGuard([]{
qApp->setStyleSheet({});
});

QMainWindow mw;
QToolBar tb1;
QToolBar tb2;
mw.addToolBar(&tb1);
mw.addToolBar(&tb2);

const QString styleSheet = "QToolBar { icon-size: %1px; }";

const int targetIconSize1 = 48;
const int targetIconSize2 = 58;
const int targetIconSize3 = 68;
const int targetIconSize4 = 78;

// Set iconSize via stylesheet using icon-size property first time
qApp->setStyleSheet(styleSheet.arg(targetIconSize1));
QTRY_COMPARE(tb1.iconSize(), QSize(targetIconSize1, targetIconSize1));
QTRY_COMPARE(tb2.iconSize(), QSize(targetIconSize1, targetIconSize1));

qApp->setStyleSheet(styleSheet.arg(targetIconSize2));
QTRY_COMPARE(tb1.iconSize(), QSize(targetIconSize2, targetIconSize2));
QTRY_COMPARE(tb2.iconSize(), QSize(targetIconSize2, targetIconSize2));

// Set tb2's icon size explicitly via setIconSize()
tb2.setIconSize(QSize(targetIconSize3, targetIconSize3));
QCOMPARE(tb1.iconSize(), QSize(targetIconSize2, targetIconSize2));
QCOMPARE(tb2.iconSize(), QSize(targetIconSize3, targetIconSize3));

// Set iconSize via stylesheet using icon-size property after setting using setIconSize()
qApp->setStyleSheet(styleSheet.arg(targetIconSize4));
// setIconSize has precedence over iconSize set via stylesheet using icon-size property
QTRY_COMPARE(tb1.iconSize(), QSize(targetIconSize4, targetIconSize4));
QCOMPARE(tb2.iconSize(), QSize(targetIconSize3, targetIconSize3));
}

QTEST_MAIN(tst_QToolBar)
#include "tst_qtoolbar.moc"

0 comments on commit 1873335

Please sign in to comment.