QToolBar: Update implicit icon size if style changes
Add logic in QToolBar::changeEvent() to update icon size set via stylesheet. Fixes: QTBUG-45949 Pick-to: 6.8 Change-Id: I7fce830a969af8774116f0229153668252b55598 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 18733355689fc3ea336631b9c5ff17e3e983fb28) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
43f4706ee8
commit
a77b11c548
@ -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:
|
||||
|
@ -62,6 +62,7 @@ private slots:
|
||||
void task197996_visibility();
|
||||
|
||||
void extraCpuConsumption(); // QTBUG-54676
|
||||
void iconSizeStyleSheet();
|
||||
};
|
||||
|
||||
|
||||
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user