aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-05-23 14:52:29 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-05-27 15:43:50 +0200
commit8ef0407ddd4c8b7f3e685d8a5316dbcb323badd2 (patch)
treef309c8377088505ba400a5b61450d730facce51a
parent2a58e518113f5c3b05449d478f203925d4755f52 (diff)
QQuickTableView: add a new 'alternatingRows' property
Add a new property 'alternatingRows' that informs the delegate that it should alternate between rows. This is merely a hint, the delegate is free to ignore it. But our own internal delegates, like TreeViewDelegate, should not. [ChangeLog][Quick][TableView] Added a new property 'alternatingRows', which is a hint to the delegate to alternate between rows. Change-Id: I3f10e7280332b652d393348ff0d71cd73ce6fa4d Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp38
-rw-r--r--src/quick/items/qquicktableview_p.h5
-rw-r--r--src/quick/items/qquicktableview_p_p.h1
-rw-r--r--src/quickcontrols2/basic/TreeViewDelegate.qml4
-rw-r--r--src/quickcontrols2/macos/TreeViewDelegate.qml4
-rw-r--r--src/quicknativestyle/controls/DefaultTreeViewDelegate.qml4
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp13
7 files changed, 66 insertions, 3 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index ecd302a761..895667b17a 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -406,6 +406,29 @@
*/
/*!
+ \qmlproperty bool QtQuick::TableView::alternatingRows
+
+ This property controls whether the background color of the rows should alternate.
+ The default value is style dependent.
+
+ \note This property is only a hint, and might therefore not be
+ respected by custom delegates. It's recommended that a delegate alternates
+ between \c palette.base and \c palette.alternateBase when this hint is
+ \c true, so that the colors can be set from outside of the delegate.
+ For example:
+
+ \code
+ background: Rectangle {
+ color: control.row === control.tableView.currentRow
+ ? control.palette.highlight
+ : (control.tableView.alternatingRows && control.row % 2 !== 0
+ ? control.palette.alternateBase
+ : control.palette.base)
+ }
+ \endcode
+*/
+
+/*!
\qmlproperty int QtQuick::TableView::leftColumn
This property holds the leftmost column that is currently visible inside the view.
@@ -4896,6 +4919,21 @@ void QQuickTableView::keyPressEvent(QKeyEvent *e)
}
}
+bool QQuickTableView::alternatingRows() const
+{
+ return d_func()->alternatingRows;
+}
+
+void QQuickTableView::setAlternatingRows(bool alternatingRows)
+{
+ Q_D(QQuickTableView);
+ if (d->alternatingRows == alternatingRows)
+ return;
+
+ d->alternatingRows = alternatingRows;
+ emit alternatingRowsChanged();
+}
+
class QObjectPrivate;
class QQuickTableSectionSizeProviderPrivate : public QObjectPrivate {
public:
diff --git a/src/quick/items/qquicktableview_p.h b/src/quick/items/qquicktableview_p.h
index 0e43020e4b..1299f4b58f 100644
--- a/src/quick/items/qquicktableview_p.h
+++ b/src/quick/items/qquicktableview_p.h
@@ -94,6 +94,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickTableView : public QQuickFlickable, public QQ
Q_PROPERTY(bool pointerNavigationEnabled READ pointerNavigationEnabled WRITE setPointerNavigationEnabled NOTIFY pointerNavigationEnabledChanged REVISION(6, 4))
Q_PROPERTY(int currentRow READ currentRow NOTIFY currentRowChanged REVISION(6, 4) FINAL)
Q_PROPERTY(int currentColumn READ currentColumn NOTIFY currentColumnChanged REVISION(6, 4) FINAL)
+ Q_PROPERTY(bool alternatingRows READ alternatingRows WRITE setAlternatingRows NOTIFY alternatingRowsChanged REVISION(6, 4) FINAL)
QML_NAMED_ELEMENT(TableView)
QML_ADDED_IN_VERSION(2, 12)
@@ -168,6 +169,9 @@ public:
int currentRow() const;
int currentColumn() const;
+ bool alternatingRows() const;
+ void setAlternatingRows(bool alternatingRows);
+
Q_INVOKABLE void forceLayout();
Q_INVOKABLE void positionViewAtCell(const QPoint &cell, PositionMode mode, const QPointF &offset = QPointF());
Q_INVOKABLE void positionViewAtCell(int column, int row, PositionMode mode, const QPointF &offset = QPointF());
@@ -222,6 +226,7 @@ Q_SIGNALS:
Q_REVISION(6, 4) void pointerNavigationEnabledChanged();
Q_REVISION(6, 4) void currentRowChanged();
Q_REVISION(6, 4) void currentColumnChanged();
+ Q_REVISION(6, 4) void alternatingRowsChanged();
protected:
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
diff --git a/src/quick/items/qquicktableview_p_p.h b/src/quick/items/qquicktableview_p_p.h
index 0563a96bf3..0ef72d8be4 100644
--- a/src/quick/items/qquicktableview_p_p.h
+++ b/src/quick/items/qquicktableview_p_p.h
@@ -290,6 +290,7 @@ public:
bool animate = true;
bool keyNavigationEnabled = true;
bool pointerNavigationEnabled = true;
+ bool alternatingRows = true;
// isTransposed is currently only used by HeaderView.
// Consider making it public.
diff --git a/src/quickcontrols2/basic/TreeViewDelegate.qml b/src/quickcontrols2/basic/TreeViewDelegate.qml
index 6bd2c5e5c7..af73d96db4 100644
--- a/src/quickcontrols2/basic/TreeViewDelegate.qml
+++ b/src/quickcontrols2/basic/TreeViewDelegate.qml
@@ -81,7 +81,9 @@ T.TreeViewDelegate {
background: Rectangle {
color: control.row === control.treeView.currentRow
? control.palette.highlight
- : (control.row % 2 === 0 ? control.palette.base : control.palette.alternateBase)
+ : (control.treeView.alternatingRows && control.row % 2 !== 0
+ ? control.palette.alternateBase
+ : control.palette.base)
}
contentItem: Label {
diff --git a/src/quickcontrols2/macos/TreeViewDelegate.qml b/src/quickcontrols2/macos/TreeViewDelegate.qml
index 862ff6c440..adc1ee8528 100644
--- a/src/quickcontrols2/macos/TreeViewDelegate.qml
+++ b/src/quickcontrols2/macos/TreeViewDelegate.qml
@@ -49,7 +49,9 @@ NativeStyle.DefaultTreeViewDelegate {
background: Rectangle {
color: control.row === control.treeView.currentRow
? control.palette.highlight
- : (control.row % 2 === 0 ? control.palette.base : control.palette.alternateBase)
+ : (control.treeView.alternatingRows && control.row % 2 !== 0
+ ? control.palette.alternateBase
+ : control.palette.base)
// Ideally we want a rounded background for the whole row, also when
// there are more than one column. But until Rectangle gains support
// for corners with individual radii, we simplify it (QTBUG-48774)
diff --git a/src/quicknativestyle/controls/DefaultTreeViewDelegate.qml b/src/quicknativestyle/controls/DefaultTreeViewDelegate.qml
index 86e658824b..765f6d49d3 100644
--- a/src/quicknativestyle/controls/DefaultTreeViewDelegate.qml
+++ b/src/quicknativestyle/controls/DefaultTreeViewDelegate.qml
@@ -79,7 +79,9 @@ T.TreeViewDelegate {
background: Rectangle {
color: control.row === control.treeView.currentRow
? control.palette.highlight
- : (control.row % 2 === 0 ? control.palette.base : control.palette.alternateBase)
+ : (control.treeView.alternatingRows && control.row % 2 !== 0
+ ? control.palette.alternateBase
+ : control.palette.base)
}
contentItem: Label {
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index 2e02e5873d..59a257a7f5 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -230,6 +230,7 @@ private slots:
void selectUsingHomeAndEndKeys();
void selectUsingPageUpDownKeys();
void testDeprecatedApi();
+ void alternatingRows();
};
tst_QQuickTableView::tst_QQuickTableView()
@@ -4948,6 +4949,18 @@ void tst_QQuickTableView::testDeprecatedApi()
QCOMPARE(tableView->bottomRow(), model.rowCount() - 1);
}
+void tst_QQuickTableView::alternatingRows()
+{
+ // Check that you can set 'alternate'
+ LOAD_TABLEVIEW("plaintableview.qml");
+
+ QVERIFY(tableView->alternatingRows());
+ tableView->setAlternatingRows(false);
+ QVERIFY(!tableView->alternatingRows());
+ tableView->setAlternatingRows(true);
+ QVERIFY(tableView->alternatingRows());
+}
+
QTEST_MAIN(tst_QQuickTableView)
#include "tst_qquicktableview.moc"