From 755f9643646d4ea4e988a838bde503438b580174 Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Date: Mon, 15 Apr 2024 12:17:13 +0200 Subject: [PATCH] Skip rendering border for the decoration area of the tree view The tree view widget draws a border for both the decoration area and the text that accompanies the first column whenever any background color is set to the row. The background color set for the row shouldn't affect the border of the decoration area. This patch fixes that issue by handling the background draw for the decoration area separately by drawing the background for the decoration area without considering the configured borders. Fixes: QTBUG-122778 Pick-to: 6.7 Change-Id: I308998a29d16c910a5370633e5bff18418c96a44 Reviewed-by: Volker Hilsheimer (cherry picked from commit 3ca615d9735f7ddb8e2ae5c13e5effd419a56300) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/itemviews/qtreeview.cpp | 3 +++ src/widgets/styles/qstylesheetstyle.cpp | 18 +++++++++++++++++- .../NoBackgroundNoBorderForBranchIndicator.qss | 3 +++ ...entBackgroundNoBorderForBranchIndicator.qss | 4 ++++ ...lidBackgroundNoBorderForBranchIndicator.qss | 4 ++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/baseline/stylesheet/qss/qtreeview/NoBackgroundNoBorderForBranchIndicator.qss create mode 100644 tests/baseline/stylesheet/qss/qtreeview/transparentBackgroundNoBorderForBranchIndicator.qss create mode 100644 tests/baseline/stylesheet/qss/qtreeview/validBackgroundNoBorderForBranchIndicator.qss diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index 1ae81023c53..621e9f220aa 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -1821,7 +1821,10 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option, const bool oldShowDecorationSelected = opt.showDecorationSelected; opt.showDecorationSelected = style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, &opt, this); + opt.features |= QStyleOptionViewItem::HasDecoration; + opt.rect = branches; style()->drawPrimitive(QStyle::PE_PanelItemViewRow, &opt, painter, this); + opt.features &= ~QStyleOptionViewItem::HasDecoration; // draw background of the item (only alternate row). rest of the background // is provided by the delegate diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 655b2246176..004326e0a44 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -4844,8 +4844,24 @@ 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()) + 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 (vopt->features & QStyleOptionViewItem::HasDecoration && + (vopt->viewItemPosition == QStyleOptionViewItem::Beginning || + vopt->viewItemPosition == QStyleOptionViewItem::OnlyOne) && rule.hasBorder()) { + if (rule.hasDrawable()) { + rule.drawBackground(p, rect); + rule.drawImage(p, rule.contentsRect(rect)); + } else { + baseStyle()->drawPrimitive(pe, opt, p, w); + } + return; + } + } + } break; case PE_PanelItemViewItem: pseudoElement = PseudoElement_ViewItem; diff --git a/tests/baseline/stylesheet/qss/qtreeview/NoBackgroundNoBorderForBranchIndicator.qss b/tests/baseline/stylesheet/qss/qtreeview/NoBackgroundNoBorderForBranchIndicator.qss new file mode 100644 index 00000000000..dda89c4b34b --- /dev/null +++ b/tests/baseline/stylesheet/qss/qtreeview/NoBackgroundNoBorderForBranchIndicator.qss @@ -0,0 +1,3 @@ +QTreeWidget::item { + border: 1px solid #32ff44; +} diff --git a/tests/baseline/stylesheet/qss/qtreeview/transparentBackgroundNoBorderForBranchIndicator.qss b/tests/baseline/stylesheet/qss/qtreeview/transparentBackgroundNoBorderForBranchIndicator.qss new file mode 100644 index 00000000000..6ed7602fa00 --- /dev/null +++ b/tests/baseline/stylesheet/qss/qtreeview/transparentBackgroundNoBorderForBranchIndicator.qss @@ -0,0 +1,4 @@ +QTreeWidget::item { + border: 1px solid #32ff44; + background-color: transparent; +} diff --git a/tests/baseline/stylesheet/qss/qtreeview/validBackgroundNoBorderForBranchIndicator.qss b/tests/baseline/stylesheet/qss/qtreeview/validBackgroundNoBorderForBranchIndicator.qss new file mode 100644 index 00000000000..d5e759d515d --- /dev/null +++ b/tests/baseline/stylesheet/qss/qtreeview/validBackgroundNoBorderForBranchIndicator.qss @@ -0,0 +1,4 @@ +QTreeWidget::item { + border: 1px solid #32ff44; + background-color: orange; +}