diff options
author | Volker Hilsheimer <[email protected]> | 2024-04-10 15:25:59 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <[email protected]> | 2024-04-11 21:44:07 +0000 |
commit | 09dd50939a0be89d744588bb3b277289ef5ceb5d (patch) | |
tree | c67538a66eef8be2d3b1551a288102635b92d612 | |
parent | 190d695b6c55036afd2858760d90b69f0d5b22d0 (diff) |
StyleSheet: respect a font weight set for header sections
QCommonStyle sets the font for a selected header section to bold.
This overrides the font weight a calling style might already have set,
e.g. when a style sheet is applied to explicitly set a weight for a
checked header section:
QHeaderView::section:checked {
font-size: 20px
font-weight: normal
}
Since setting the weight on a font sets the respective resolve-mask
bit, we can avoid overwriting a weight that is already set explicitly.
Add baseline test coverage using a QTableWidget.
Fixes: QTBUG-122180
Change-Id: I8c6279ad2fd8c5718ebea26e27c64ae823625748
Reviewed-by: Axel Spoerl <[email protected]>
(cherry picked from commit 74e0ed217fdec8e32227f9f845eccac7f1552297)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
(cherry picked from commit 294a2646db4412bf9eb65a7c365bb0398d2747c5)
-rw-r--r-- | src/widgets/styles/qcommonstyle.cpp | 9 | ||||
-rw-r--r-- | tests/baseline/stylesheet/qss/qheaderview/selectedFontWeight.qss | 16 | ||||
-rw-r--r-- | tests/baseline/stylesheet/tst_baseline_stylesheet.cpp | 19 |
3 files changed, 41 insertions, 3 deletions
diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index ee32e7d93dc..cb725410f57 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -1669,9 +1669,12 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, QFontMetrics fm(header->fontMetrics); if (header->state & QStyle::State_On) { QFont fnt = p->font(); - fnt.setBold(true); - p->setFont(fnt); - fm = QFontMetrics((p->font())); + // the font already has a weight set; don't override that + if (!(fnt.resolveMask() & QFont::WeightResolved)) { + fnt.setBold(true); + p->setFont(fnt); + fm = QFontMetrics((p->font())); + } } QString text = header->text; if (const QStyleOptionHeaderV2 *headerV2 = qstyleoption_cast<const QStyleOptionHeaderV2 *>(header)) { diff --git a/tests/baseline/stylesheet/qss/qheaderview/selectedFontWeight.qss b/tests/baseline/stylesheet/qss/qheaderview/selectedFontWeight.qss new file mode 100644 index 00000000000..1c45a997672 --- /dev/null +++ b/tests/baseline/stylesheet/qss/qheaderview/selectedFontWeight.qss @@ -0,0 +1,16 @@ +QHeaderView::section { + background-color: red; + font-size: 10px; +} + +QHeaderView::section:checked { + background-color: green; + font-size: 20px; + font-weight: bold; +} + +QHeaderView::section:first { + background-color: yellow; + font-size: 20px; + font-weight: normal; +} diff --git a/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp b/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp index 3a4d8e8f15d..dde3dcc7178 100644 --- a/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp +++ b/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp @@ -27,6 +27,9 @@ private slots: void tst_QTreeView_data(); void tst_QTreeView(); + void tst_QHeaderView_data(); + void tst_QHeaderView(); + private: QDir styleSheetDir; }; @@ -205,6 +208,22 @@ void tst_Stylesheet::tst_QTreeView() QBASELINE_CHECK_DEFERRED(takeSnapshot(), "itemSelected"); } +void tst_Stylesheet::tst_QHeaderView_data() +{ + loadTestFiles(); +} + +void tst_Stylesheet::tst_QHeaderView() +{ + QHBoxLayout *layout = new QHBoxLayout; + QTableWidget *tw = new QTableWidget(10, 10); + tw->setCurrentCell(1, 1); + layout->addWidget(tw); + testWindow()->setLayout(layout); + makeVisible(); + QBASELINE_TEST(takeSnapshot()); +} + #define main _realmain QTEST_MAIN(tst_Stylesheet) #undef main |