aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-04-06 19:18:06 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-04-08 15:57:16 +0200
commitf296c300614cbe2c9de66fe05ea7201d79249062 (patch)
tree4fde16688d250e380ad0f046a815ac70233d37e2
parentbe12b71faeba23a8ef0cf9695e11aa847b68ae7e (diff)
QQuickTableView: add function: itemAtCell(const QPoint)
Add a function to the API to let the application get the item loaded for a specific cell. [ChangeLog][TableView] A function 'itemAtCell()' has now been added to let the application get the delegate item loaded for a specific cell. Change-Id: Ie84ef44ea2a0a901487812c4d611b98d4c86ee22 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp31
-rw-r--r--src/quick/items/qquicktableview_p.h2
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp38
3 files changed, 71 insertions, 0 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 28a5dee681..3548d20706 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -425,6 +425,23 @@
*/
/*!
+ \qmlmethod Item QtQuick::TableView::itemAtCell(point cell)
+
+ Returns the delegate item at \a cell if loaded, otherwise \c null.
+
+ \note only the items that are visible in the view are normally loaded.
+ As soon as a cell is flicked out of the view, the item inside will
+ either be unloaded or placed in the recycle pool. As such, the return
+ value should never be stored.
+*/
+
+/*!
+ \qmlmethod Item QtQuick::TableView::itemAtCell(int column, int row)
+
+ Convenience for calling \code itemAtCell(Qt.point(column, row)) \endcode
+*/
+
+/*!
\qmlattachedproperty TableView QtQuick::TableView::view
This attached property holds the view that manages the delegate instance.
@@ -3122,6 +3139,20 @@ void QQuickTableView::positionViewAtColumn(int column, Qt::Alignment alignment,
QQuickTableViewPrivate::RebuildOption::PositionViewAtColumn);
}
+QQuickItem *QQuickTableView::itemAtCell(const QPoint &cell) const
+{
+ Q_D(const QQuickTableView);
+ const int modelIndex = d->modelIndexAtCell(cell);
+ if (!d->loadedItems.contains(modelIndex))
+ return nullptr;
+ return d->loadedItems.value(modelIndex)->item;
+}
+
+QQuickItem *QQuickTableView::itemAtCell(int column, int row) const
+{
+ return itemAtCell(QPoint(column, row));
+}
+
void QQuickTableView::forceLayout()
{
d_func()->forceLayout();
diff --git a/src/quick/items/qquicktableview_p.h b/src/quick/items/qquicktableview_p.h
index 387cc5d51d..e377f7da65 100644
--- a/src/quick/items/qquicktableview_p.h
+++ b/src/quick/items/qquicktableview_p.h
@@ -127,6 +127,8 @@ public:
Q_INVOKABLE void positionViewAtCell(int column, int row, Qt::Alignment alignment, const QPointF &offset = QPointF());
Q_INVOKABLE void positionViewAtRow(int row, Qt::Alignment alignment, qreal offset = 0);
Q_INVOKABLE void positionViewAtColumn(int column, Qt::Alignment alignment, qreal offset = 0);
+ Q_INVOKABLE QQuickItem *itemAtCell(const QPoint &cell) const;
+ Q_INVOKABLE QQuickItem *itemAtCell(int column, int row) const;
static QQuickTableViewAttached *qmlAttachedProperties(QObject *);
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index e0917cbea9..679ced17d7 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -182,6 +182,8 @@ private slots:
void positionViewAtRow();
void positionViewAtColumn_data();
void positionViewAtColumn();
+ void itemAtCell_data();
+ void itemAtCell();
};
tst_QQuickTableView::tst_QQuickTableView()
@@ -2954,6 +2956,42 @@ void tst_QQuickTableView::positionViewAtColumn()
}
}
+void tst_QQuickTableView::itemAtCell_data()
+{
+ QTest::addColumn<QPoint>("cell");
+ QTest::addColumn<bool>("shouldExist");
+
+ QTest::newRow("0, 0") << QPoint(0, 0) << true;
+ QTest::newRow("0, 4") << QPoint(0, 4) << true;
+ QTest::newRow("4, 0") << QPoint(4, 0) << true;
+ QTest::newRow("4, 4") << QPoint(4, 4) << true;
+ QTest::newRow("30, 30") << QPoint(30, 30) << false;
+ QTest::newRow("-1, -1") << QPoint(-1, -1) << false;
+}
+
+void tst_QQuickTableView::itemAtCell()
+{
+ QFETCH(QPoint, cell);
+ QFETCH(bool, shouldExist);
+
+ LOAD_TABLEVIEW("plaintableview.qml");
+ auto model = TestModelAsVariant(100, 100);
+ tableView->setModel(model);
+
+ WAIT_UNTIL_POLISHED;
+
+ const auto item = tableView->itemAtCell(cell);
+ if (shouldExist) {
+ const auto context = qmlContext(item);
+ const int contextRow = context->contextProperty("row").toInt();
+ const int contextColumn = context->contextProperty("column").toInt();
+ QCOMPARE(contextColumn, cell.x());
+ QCOMPARE(contextRow, cell.y());
+ } else {
+ QVERIFY(!item);
+ }
+}
+
QTEST_MAIN(tst_QQuickTableView)
#include "tst_qquicktableview.moc"