aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-05-31 21:45:01 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-06-03 08:46:10 +0200
commit7dba66ab707b74458fe8c9f3422c42d2ff733040 (patch)
treef721319e98dd2bb47fad8d03fc2f4bf23484b2b9 /src
parent8ea96460198bea744a3583a714d2cc042d8403ab (diff)
QQuickTableView: add new property 'selectionBehavior'
This patch will add a 'selectionBehavior' property to TableView. It will let the developer specify if the user should be able to select individual cells, rows, columns, or if selections should be disabled. This is equal to the selectionBehavior enum in QAbstractItemView. [ChangeLog][Quick][TableView] A new property 'selectionBehavior' has been added that specifies if the user should be able to select rows, columns, or cells. Change-Id: Ia8855ae032bb02d278b284ed35049d9237523139 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quick/items/qquicktableview.cpp86
-rw-r--r--src/quick/items/qquicktableview_p.h13
-rw-r--r--src/quick/items/qquicktableview_p_p.h5
3 files changed, 88 insertions, 16 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 2e8d794385..9026cb13d2 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -191,10 +191,13 @@
\section1 Selecting items
- You can add selection support to TableView by assigning an ItemSelectionModel to
+ You can add selection support to TableView by assigning an \l ItemSelectionModel to
the \l selectionModel property. It will then use this model to control which
delegate items should be shown as selected, and which item should be shown as
- current. To find out whether a delegate is selected or current, declare the
+ current. You can set \l selectionBehavior to control if the user should
+ be allowed to select individual cells, rows, or columns.
+
+ To find out whether a delegate is selected or current, declare the
following properties:
\code
@@ -537,6 +540,22 @@
*/
/*!
+ \qmlproperty enumeration QtQuick::TableView::selectionBehavior
+ \since 6.4
+
+ This property holds whether the user can select single cells, rows or columns.
+
+ \list
+ \li TableView.SelectionDisabled - the user cannot perform selections.
+ \li TableView.SelectCells (default) - the user can select individual cells.
+ \li TableView.SelectRows - the user can only select rows.
+ \li TableView.SelectColumns - the user can only select columns.
+ \endlist
+
+ \sa {Selecting items}, selectionModel, keyNavigationEnabled
+*/
+
+/*!
\qmlmethod QtQuick::TableView::positionViewAtCell(point cell, PositionMode mode, point offset, rect subRect)
Positions \l {Flickable::}{contentX} and \l {Flickable::}{contentY} such
@@ -1041,14 +1060,26 @@ void QQuickTableViewPrivate::setSelectionStartPos(const QPointF &pos)
return;
const QRect prevSelection = selection();
- selectionStartCell = clampedCellAtPos(pos);
-
- if (!cellIsValid(selectionStartCell))
+ const QPoint clampedCell = clampedCellAtPos(pos);
+ if (!cellIsValid(clampedCell))
return;
- // Update selection rectangle
- selectionStartCellRect = loadedTableItem(selectionStartCell)->geometry();
- setCurrentIndex(selectionStartCell);
+ setCurrentIndex(clampedCell);
+ selectionStartCellRect = loadedTableItem(clampedCell)->geometry();
+
+ switch (selectionBehavior) {
+ case QQuickTableView::SelectCells:
+ selectionStartCell = clampedCell;
+ break;
+ case QQuickTableView::SelectRows:
+ selectionStartCell = QPoint(0, clampedCell.y());
+ break;
+ case QQuickTableView::SelectColumns:
+ selectionStartCell = QPoint(clampedCell.x(), 0);
+ break;
+ case QQuickTableView::SelectionDisabled:
+ return;
+ }
if (!cellIsValid(selectionEndCell))
return;
@@ -1072,14 +1103,26 @@ void QQuickTableViewPrivate::setSelectionEndPos(const QPointF &pos)
return;
const QRect prevSelection = selection();
- selectionEndCell = clampedCellAtPos(pos);
-
- if (!cellIsValid(selectionEndCell))
+ const QPoint clampedCell = clampedCellAtPos(pos);
+ if (!cellIsValid(clampedCell))
return;
- // Update selection rectangle
- selectionEndCellRect = loadedTableItem(selectionEndCell)->geometry();
- setCurrentIndex(selectionEndCell);
+ setCurrentIndex(clampedCell);
+ selectionEndCellRect = loadedTableItem(clampedCell)->geometry();;
+
+ switch (selectionBehavior) {
+ case QQuickTableView::SelectCells:
+ selectionEndCell = clampedCell;
+ break;
+ case QQuickTableView::SelectRows:
+ selectionEndCell = QPoint(tableSize.width() - 1, clampedCell.y());
+ break;
+ case QQuickTableView::SelectColumns:
+ selectionEndCell = QPoint(clampedCell.x(), tableSize.height() - 1);
+ break;
+ case QQuickTableView::SelectionDisabled:
+ return;
+ }
if (!cellIsValid(selectionStartCell))
return;
@@ -5029,6 +5072,21 @@ void QQuickTableView::setAlternatingRows(bool alternatingRows)
emit alternatingRowsChanged();
}
+QQuickTableView::SelectionBehavior QQuickTableView::selectionBehavior() const
+{
+ return d_func()->selectionBehavior;
+}
+
+void QQuickTableView::setSelectionBehavior(SelectionBehavior selectionBehavior)
+{
+ Q_D(QQuickTableView);
+ if (d->selectionBehavior == selectionBehavior)
+ return;
+
+ d->selectionBehavior = selectionBehavior;
+ emit selectionBehaviorChanged();
+}
+
class QObjectPrivate;
class QQuickTableSectionSizeProviderPrivate : public QObjectPrivate {
public:
diff --git a/src/quick/items/qquicktableview_p.h b/src/quick/items/qquicktableview_p.h
index a740cc1900..bf32e9f3a7 100644
--- a/src/quick/items/qquicktableview_p.h
+++ b/src/quick/items/qquicktableview_p.h
@@ -95,6 +95,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickTableView : public QQuickFlickable, public QQ
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)
+ Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior NOTIFY selectionBehaviorChanged REVISION(6, 4) FINAL)
QML_NAMED_ELEMENT(TableView)
QML_ADDED_IN_VERSION(2, 12)
@@ -115,6 +116,14 @@ public:
Q_FLAG(PositionModeFlag)
Q_DECLARE_FLAGS(PositionMode, PositionModeFlag)
+ enum SelectionBehavior {
+ SelectionDisabled,
+ SelectCells,
+ SelectRows,
+ SelectColumns
+ };
+ Q_ENUM(SelectionBehavior)
+
QQuickTableView(QQuickItem *parent = nullptr);
~QQuickTableView() override;
int rows() const;
@@ -172,6 +181,9 @@ public:
bool alternatingRows() const;
void setAlternatingRows(bool alternatingRows);
+ SelectionBehavior selectionBehavior() const;
+ void setSelectionBehavior(SelectionBehavior selectionBehavior);
+
Q_INVOKABLE void forceLayout();
Q_INVOKABLE void positionViewAtCell(const QPoint &cell, PositionMode mode, const QPointF &offset = QPointF(), const QRectF &subRect = QRectF());
Q_INVOKABLE void positionViewAtCell(int column, int row, PositionMode mode, const QPointF &offset = QPointF(), const QRectF &subRect = QRectF());
@@ -227,6 +239,7 @@ Q_SIGNALS:
Q_REVISION(6, 4) void currentRowChanged();
Q_REVISION(6, 4) void currentColumnChanged();
Q_REVISION(6, 4) void alternatingRowsChanged();
+ Q_REVISION(6, 4) void selectionBehaviorChanged();
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 b30effc42c..fd2016571c 100644
--- a/src/quick/items/qquicktableview_p_p.h
+++ b/src/quick/items/qquicktableview_p_p.h
@@ -327,6 +327,7 @@ public:
Qt::Orientations assignedSyncDirection = Qt::Horizontal | Qt::Vertical;
QPointer<QItemSelectionModel> selectionModel;
+ QQuickTableView::SelectionBehavior selectionBehavior = QQuickTableView::SelectCells;
int assignedPositionViewAtRowAfterRebuild = 0;
int assignedPositionViewAtColumnAfterRebuild = 0;
@@ -342,8 +343,8 @@ public:
QQuickPropertyAnimation positionXAnimation;
QQuickPropertyAnimation positionYAnimation;
- QPoint selectionStartCell;
- QPoint selectionEndCell;
+ QPoint selectionStartCell = {-1, -1};
+ QPoint selectionEndCell = {-1, -1};
QRectF selectionStartCellRect;
QRectF selectionEndCellRect;