summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/widgets/itemviews/qtreeview.cpp30
-rw-r--r--src/widgets/itemviews/qtreeview.h3
-rw-r--r--src/widgets/itemviews/qtreeview_p.h5
-rw-r--r--src/widgets/styles/qcommonstyle.cpp3
-rw-r--r--src/widgets/styles/qstyle.cpp3
-rw-r--r--src/widgets/styles/qstyle.h1
-rw-r--r--tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp43
7 files changed, 82 insertions, 6 deletions
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index 3f952ff768..ae3994e30a 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -371,7 +371,9 @@ void QTreeView::setAutoExpandDelay(int delay)
horizontal distance from the viewport edge to the items in the first column;
for child items, it specifies their indentation from their parent items.
- By default, this property has a value of 20.
+ By default, the value of this property is style dependent. Thus, when the style
+ changes, this property updates from it. Calling setIndentation() stops the updates,
+ calling resetIndentation() will restore default behavior.
*/
int QTreeView::indentation() const
{
@@ -382,12 +384,22 @@ int QTreeView::indentation() const
void QTreeView::setIndentation(int i)
{
Q_D(QTreeView);
- if (i != d->indent) {
+ if (!d->customIndent || (i != d->indent)) {
d->indent = i;
+ d->customIndent = true;
d->viewport->update();
}
}
+void QTreeView::resetIndentation()
+{
+ Q_D(QTreeView);
+ if (d->customIndent) {
+ d->updateIndentationFromStyle();
+ d->customIndent = false;
+ }
+}
+
/*!
\property QTreeView::rootIsDecorated
\brief whether to show controls for expanding and collapsing top-level items
@@ -2081,6 +2093,12 @@ QModelIndex QTreeView::indexBelow(const QModelIndex &index) const
void QTreeView::doItemsLayout()
{
Q_D(QTreeView);
+ if (!d->customIndent) {
+ // ### Qt 6: move to event()
+ // QAbstractItemView calls this method in case of a style change,
+ // so update the indentation here if it wasn't set manually.
+ d->updateIndentationFromStyle();
+ }
if (d->hasRemovedItems) {
//clean the QSet that may contains old (and this invalid) indexes
d->hasRemovedItems = false;
@@ -3025,6 +3043,8 @@ bool QTreeView::isIndexHidden(const QModelIndex &index) const
void QTreeViewPrivate::initialize()
{
Q_Q(QTreeView);
+
+ updateIndentationFromStyle();
updateStyledFrameWidths();
q->setSelectionBehavior(QAbstractItemView::SelectRows);
q->setSelectionMode(QAbstractItemView::SingleSelection);
@@ -3911,6 +3931,12 @@ int QTreeViewPrivate::accessibleTree2Index(const QModelIndex &index) const
return (q->visualIndex(index) + (q->header() ? 1 : 0)) * index.model()->columnCount() + index.column();
}
+void QTreeViewPrivate::updateIndentationFromStyle()
+{
+ Q_Q(const QTreeView);
+ indent = q->style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, q);
+}
+
/*!
\reimp
*/
diff --git a/src/widgets/itemviews/qtreeview.h b/src/widgets/itemviews/qtreeview.h
index 429b9d3add..8ac53b8bb7 100644
--- a/src/widgets/itemviews/qtreeview.h
+++ b/src/widgets/itemviews/qtreeview.h
@@ -56,7 +56,7 @@ class Q_WIDGETS_EXPORT QTreeView : public QAbstractItemView
{
Q_OBJECT
Q_PROPERTY(int autoExpandDelay READ autoExpandDelay WRITE setAutoExpandDelay)
- Q_PROPERTY(int indentation READ indentation WRITE setIndentation)
+ Q_PROPERTY(int indentation READ indentation WRITE setIndentation RESET resetIndentation)
Q_PROPERTY(bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated)
Q_PROPERTY(bool uniformRowHeights READ uniformRowHeights WRITE setUniformRowHeights)
Q_PROPERTY(bool itemsExpandable READ itemsExpandable WRITE setItemsExpandable)
@@ -83,6 +83,7 @@ public:
int indentation() const;
void setIndentation(int i);
+ void resetIndentation();
bool rootIsDecorated() const;
void setRootIsDecorated(bool show);
diff --git a/src/widgets/itemviews/qtreeview_p.h b/src/widgets/itemviews/qtreeview_p.h
index 5d6333e856..c44d6d7ced 100644
--- a/src/widgets/itemviews/qtreeview_p.h
+++ b/src/widgets/itemviews/qtreeview_p.h
@@ -89,7 +89,7 @@ public:
uniformRowHeights(false), rootDecoration(true),
itemsExpandable(true), sortingEnabled(false),
expandsOnDoubleClick(true),
- allColumnsShowFocus(false), current(0), spanning(false),
+ allColumnsShowFocus(false), customIndent(false), current(0), spanning(false),
animationsEnabled(false), columnResizeTimerID(0),
autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false),
treePosition(0) {}
@@ -191,6 +191,7 @@ public:
bool sortingEnabled;
bool expandsOnDoubleClick;
bool allColumnsShowFocus;
+ bool customIndent;
// used for drawing
mutable QPair<int,int> leftAndRight;
@@ -241,6 +242,8 @@ public:
int accessibleTree2Index(const QModelIndex &index) const;
+ void updateIndentationFromStyle();
+
// used for spanning rows
QVector<QPersistentModelIndex> spanningIndexes;
diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp
index ce6d3d708f..ec289e896b 100644
--- a/src/widgets/styles/qcommonstyle.cpp
+++ b/src/widgets/styles/qcommonstyle.cpp
@@ -4695,6 +4695,9 @@ int QCommonStyle::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWid
case PM_SubMenuOverlap:
ret = -proxy()->pixelMetric(QStyle::PM_MenuPanelWidth, opt, widget);
break;
+ case PM_TreeViewIndentation:
+ ret = int(QStyleHelper::dpiScaled(20.));
+ break;
default:
ret = 0;
break;
diff --git a/src/widgets/styles/qstyle.cpp b/src/widgets/styles/qstyle.cpp
index fb5a5c9003..c3be177732 100644
--- a/src/widgets/styles/qstyle.cpp
+++ b/src/widgets/styles/qstyle.cpp
@@ -1470,6 +1470,9 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment,
\value PM_TabCloseIndicatorWidth The default width of a close button on a tab in a tab bar.
\value PM_TabCloseIndicatorHeight The default height of a close button on a tab in a tab bar.
+ \value PM_TreeViewIndentation The indentation of items in a tree view.
+ This enum value has been introduced in Qt 5.4.
+
\value PM_CustomBase Base value for custom pixel metrics. Custom
values must be greater than this value.
diff --git a/src/widgets/styles/qstyle.h b/src/widgets/styles/qstyle.h
index a2de839404..c647513dca 100644
--- a/src/widgets/styles/qstyle.h
+++ b/src/widgets/styles/qstyle.h
@@ -556,6 +556,7 @@ public:
PM_ScrollView_ScrollBarSpacing,
PM_ScrollView_ScrollBarOverlap,
PM_SubMenuOverlap,
+ PM_TreeViewIndentation,
// do not add any values below/greater than this
PM_CustomBase = 0xf0000000
diff --git a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
index 34cfbf8c99..653f158fc9 100644
--- a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
+++ b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
@@ -212,6 +212,7 @@ private slots:
void rowSizeHint();
void setSortingEnabled();
void headerHidden();
+ void indentation();
void selection();
void removeAndInsertExpandedCol0();
@@ -439,7 +440,8 @@ void tst_QTreeView::getSetCheck()
// int QTreeView::indentation()
// void QTreeView::setIndentation(int)
- QCOMPARE(obj1.indentation(), 20);
+ const int styledIndentation = obj1.style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, &obj1);
+ QCOMPARE(obj1.indentation(), styledIndentation);
obj1.setIndentation(0);
QCOMPARE(obj1.indentation(), 0);
obj1.setIndentation(INT_MIN);
@@ -554,7 +556,8 @@ void tst_QTreeView::construction()
QCOMPARE(view.columnWidth(0), 0);
QCOMPARE(view.columnWidth(1), 0);
QVERIFY(view.header());
- QCOMPARE(view.indentation(), 20);
+ const int styledIndentation = view.style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, &view);
+ QCOMPARE(view.indentation(), styledIndentation);
QCOMPARE(view.indexAbove(QModelIndex()), QModelIndex());
QCOMPARE(view.indexBelow(QModelIndex()), QModelIndex());
QVERIFY(!view.isAnimated());
@@ -2576,6 +2579,42 @@ void tst_QTreeView::headerHidden()
QCOMPARE(view.header()->isHidden(), true);
}
+class TestTreeViewStyle : public QProxyStyle
+{
+public:
+ TestTreeViewStyle() : indentation(20) {}
+ int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const Q_DECL_OVERRIDE
+ {
+ if (metric == QStyle::PM_TreeViewIndentation)
+ return indentation;
+ else
+ return QProxyStyle::pixelMetric(metric, option, widget);
+ }
+ int indentation;
+};
+
+void tst_QTreeView::indentation()
+{
+ TestTreeViewStyle style1;
+ TestTreeViewStyle style2;
+ style1.indentation = 20;
+ style2.indentation = 30;
+
+ QTreeView view;
+ view.setStyle(&style1);
+ QCOMPARE(view.indentation(), style1.indentation);
+ view.setStyle(&style2);
+ QCOMPARE(view.indentation(), style2.indentation);
+ view.setIndentation(70);
+ QCOMPARE(view.indentation(), 70);
+ view.setStyle(&style1);
+ QCOMPARE(view.indentation(), 70);
+ view.resetIndentation();
+ QCOMPARE(view.indentation(), style1.indentation);
+ view.setStyle(&style2);
+ QCOMPARE(view.indentation(), style2.indentation);
+}
+
// From Task 145199 (crash when column 0 having at least one expanded item is removed and then
// inserted). The test passes simply if it doesn't crash, hence there are no calls
// to QCOMPARE() or QVERIFY().