From 4615ffda64f567774264b4b510d9125d2d711718 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Thu, 19 Apr 2018 13:44:55 +0200 Subject: TableView: implement getAttachedObject() for convenience This function will come in handy in later patches as well. Change-Id: Id4d6437dcd74f5e69e43d1e56c4c5aa4f2cee462 Reviewed-by: Mitch Curtis --- src/quick/items/qquicktableview.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index b3679af801..77d6592917 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -261,6 +261,7 @@ protected: protected: inline QQmlDelegateModel *wrapperModel() const { return qobject_cast(model); } + QQuickTableViewAttached *getAttachedObject(const QObject *object) const; int modelIndexAtCell(const QPoint &cell); QPoint cellAtModelIndex(int modelIndex); @@ -397,6 +398,12 @@ void QQuickTableViewPrivate::dumpTable() const qWarning() << "Window capture saved to:" << filename; } +QQuickTableViewAttached *QQuickTableViewPrivate::getAttachedObject(const QObject *object) const +{ + QObject *attachedObject = qmlAttachedPropertiesObject(object); + return static_cast(attachedObject); +} + int QQuickTableViewPrivate::modelIndexAtCell(const QPoint &cell) { int availableRows = tableSize.height(); @@ -1357,23 +1364,20 @@ void QQuickTableViewPrivate::itemCreatedCallback(int modelIndex, QObject*) void QQuickTableViewPrivate::initItemCallback(int modelIndex, QObject *object) { Q_UNUSED(modelIndex); - QQuickItem *item = qmlobject_cast(object); - if (!item) + auto attached = getAttachedObject(object); + if (!attached) return; - QObject *attachedObject = qmlAttachedPropertiesObject(item); - if (QQuickTableViewAttached *attached = static_cast(attachedObject)) { - attached->setTableView(q_func()); - // Even though row and column is injected directly into the context of a delegate item - // from QQmlDelegateModel and its model classes, they will only return which row and - // column an item represents in the model. This might be different from which - // cell an item ends up in in the Table, if a different rows/columns has been set - // on it (which is typically the case for list models). For those cases, Table.row - // and Table.column can be helpful. - QPoint cell = cellAtModelIndex(modelIndex); - attached->setColumn(cell.x()); - attached->setRow(cell.y()); - } + // Even though row and column is injected directly into the context of a delegate item + // from QQmlDelegateModel and its model classes, they will only return which row and + // column an item represents in the model. This might be different from which + // cell an item ends up in in the Table, if a different rows/columns has been set + // on it (which is typically the case for list models). For those cases, Table.row + // and Table.column can be helpful. + QPoint cell = cellAtModelIndex(modelIndex); + attached->setTableView(q_func()); + attached->setColumn(cell.x()); + attached->setRow(cell.y()); } void QQuickTableViewPrivate::modelUpdated(const QQmlChangeSet &changeSet, bool reset) -- cgit v1.2.3