From b780eaf6a063a7efe03417cf6b7008a188e222a4 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 5 Nov 2024 17:03:45 +0100 Subject: [PATCH] StyleSheet/QTreeView: draw empty area and background of semi-transparent items After 3ca615d9735f7ddb8e2ae5c13e5effd419a56300, the style sheet style will not draw rows unless the item has a border. Otherwise, it will assume that the item drawing later on will fill the background anyway. We also never painted the background for empty rows. However, if the item's background is semi-transparent, then the row needs to be drawn first, so that the correct background shows through. And for empty rows, we need to draw just the background. This is especially important for trees with alternate background colors. Add baseline test case for this particular combination. This will result in a minor change to the results of the existing transparentBackgroundNoBorderForBranchIndicator test case, where now the background is drawn behind the indicator area when the item's background is semi-transparent. I don't think this is a bug though. Pick-to: 6.8 Fixes: QTBUG-123632 Change-Id: I2cd4efb748aaefc03a3b576458b750ee47479e97 Reviewed-by: Santhosh Kumar --- src/widgets/styles/qstylesheetstyle.cpp | 15 ++++++++++----- .../transparentItemBackgroundOnAlternating.qss | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/baseline/stylesheet/qss/qtreeview/transparentItemBackgroundOnAlternating.qss diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 363662585d3..edce3f3eeb2 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -4870,11 +4870,15 @@ void QStyleSheetStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *op // only indirectly through the background of the item. To get the // same background for all parts drawn by QTreeView, we have to // use the background rule for the item here. - if (renderRule(w, opt, PseudoElement_ViewItem).hasBackground()) { - pseudoElement = PseudoElement_ViewItem; - // Skip border for the branch and draw only the brackground - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { - QRenderRule rule = renderRule(w, opt, PseudoElement_ViewItem); + if (const QStyleOptionViewItem *vopt = qstyleoption_cast(opt)) { + // default handling for drawing empty space + if (vopt->viewItemPosition == QStyleOptionViewItem::Invalid) + break; + if (QRenderRule rule = renderRule(w, opt, PseudoElement_ViewItem); rule.hasBackground()) { + // if the item background is not fully opaque, then we have to paint the row + if (rule.background()->brush.color().alpha() != 1.0) + baseStyle()->drawPrimitive(pe, opt, p, w); + // Skip border for the branch and draw only the brackground if (vopt->features & QStyleOptionViewItem::HasDecoration && (vopt->viewItemPosition == QStyleOptionViewItem::Beginning || vopt->viewItemPosition == QStyleOptionViewItem::OnlyOne) && rule.hasBorder()) { @@ -4886,6 +4890,7 @@ void QStyleSheetStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *op } return; } + pseudoElement = PseudoElement_ViewItem; } } break; diff --git a/tests/baseline/stylesheet/qss/qtreeview/transparentItemBackgroundOnAlternating.qss b/tests/baseline/stylesheet/qss/qtreeview/transparentItemBackgroundOnAlternating.qss new file mode 100644 index 00000000000..d78be3af800 --- /dev/null +++ b/tests/baseline/stylesheet/qss/qtreeview/transparentItemBackgroundOnAlternating.qss @@ -0,0 +1,11 @@ +QTreeView { + qproperty-alternatingRowColors: true; + alternate-background-color: fuchsia; + background-color: orange; +} + +QTreeView::item { + background-color: "#A00000FF"; + color: #3C3C3C; + padding: 2px; +}