diff options
author | Richard Moe Gustavsen <[email protected]> | 2021-04-30 12:16:38 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <[email protected]> | 2021-04-30 15:20:48 +0200 |
commit | 499fcb5d399321c3d887ead8f5a5b8696841a7f9 (patch) | |
tree | 5b4f84c34509d723b8b66bb090cbe012a1983f47 /src/qmlmodels/qqmltableinstancemodel.cpp | |
parent | 70e1d5234af1f454be60f0a18594ba82fea8a8f3 (diff) |
qqmltableinstancemodel: add setRequiredProperty()
Add a virtual function setRequiredProperty() to
QQmlInstanceModel that we override in
QQmlTableInstanceModel. This function can be called
from QQuickTableView, upon getting the initItem signal,
to assign initial values to any required properties
that the view makes use of.
This patch is added as a preparation for adding selection
support to QQuickTableView (which will make use of
"required property isSelected" on the delegate)
Change-Id: I55885bafa14da1d432c120bef807e73165f1466c
Reviewed-by: Fabian Kosmale <[email protected]>
Diffstat (limited to 'src/qmlmodels/qqmltableinstancemodel.cpp')
-rw-r--r-- | src/qmlmodels/qqmltableinstancemodel.cpp | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/qmlmodels/qqmltableinstancemodel.cpp b/src/qmlmodels/qqmltableinstancemodel.cpp index 5c30a8eb24..5b058481ab 100644 --- a/src/qmlmodels/qqmltableinstancemodel.cpp +++ b/src/qmlmodels/qqmltableinstancemodel.cpp @@ -404,6 +404,33 @@ QQmlIncubator::Status QQmlTableInstanceModel::incubationStatus(int index) { return QQmlIncubator::Ready; } +bool QQmlTableInstanceModel::setRequiredProperty(int index, const QString &name, const QVariant &value) +{ + // This function can be called from the view upon + // receiving the initItem signal. It can be used to + // give all required delegate properties used by the + // view an initial value. + const auto modelItem = m_modelItems.value(index, nullptr); + if (!modelItem) + return false; + if (!modelItem->object) + return false; + if (!modelItem->incubationTask) + return false; + + bool wasInRequired = false; + const auto task = QQmlIncubatorPrivate::get(modelItem->incubationTask); + RequiredProperties &props = task->requiredProperties(); + if (props.empty()) + return false; + + QQmlProperty componentProp = QQmlComponentPrivate::removePropertyFromRequired( + modelItem->object, name, props, &wasInRequired); + if (wasInRequired) + componentProp.write(value); + return wasInRequired; +} + void QQmlTableInstanceModel::deleteIncubationTaskLater(QQmlIncubator *incubationTask) { // We often need to post-delete incubation tasks, since we cannot @@ -484,10 +511,11 @@ const QAbstractItemModel *QQmlTableInstanceModel::abstractItemModel() const void QQmlTableInstanceModelIncubationTask::setInitialState(QObject *object) { initializeRequiredProperties(modelItemToIncubate, object); - if (QQmlIncubatorPrivate::get(this)->requiredProperties().empty()) { - modelItemToIncubate->object = object; - emit tableInstanceModel->initItem(modelItemToIncubate->index, object); - } else { + modelItemToIncubate->object = object; + emit tableInstanceModel->initItem(modelItemToIncubate->index, object); + + if (!QQmlIncubatorPrivate::get(this)->requiredProperties().empty()) { + modelItemToIncubate->object = nullptr; object->deleteLater(); } } |