aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2021-04-30 12:16:38 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2021-04-30 15:20:48 +0200
commit499fcb5d399321c3d887ead8f5a5b8696841a7f9 (patch)
tree5b4f84c34509d723b8b66bb090cbe012a1983f47
parent70e1d5234af1f454be60f0a18594ba82fea8a8f3 (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 <fabian.kosmale@qt.io>
-rw-r--r--src/qmlmodels/qqmlobjectmodel.cpp11
-rw-r--r--src/qmlmodels/qqmlobjectmodel_p.h2
-rw-r--r--src/qmlmodels/qqmltableinstancemodel.cpp36
-rw-r--r--src/qmlmodels/qqmltableinstancemodel_p.h2
4 files changed, 47 insertions, 4 deletions
diff --git a/src/qmlmodels/qqmlobjectmodel.cpp b/src/qmlmodels/qqmlobjectmodel.cpp
index 19aaaae88e..49588b306c 100644
--- a/src/qmlmodels/qqmlobjectmodel.cpp
+++ b/src/qmlmodels/qqmlobjectmodel.cpp
@@ -451,6 +451,17 @@ void QQmlObjectModel::clear()
d->clear();
}
+bool QQmlInstanceModel::setRequiredProperty(int index, const QString &name, const QVariant &value)
+{
+ Q_UNUSED(index);
+ Q_UNUSED(name);
+ Q_UNUSED(value);
+ // The view should not call this function, unless
+ // it's actually handled in a subclass.
+ Q_UNREACHABLE();
+ return false;
+}
+
QT_END_NAMESPACE
#include "moc_qqmlobjectmodel_p.cpp"
diff --git a/src/qmlmodels/qqmlobjectmodel_p.h b/src/qmlmodels/qqmlobjectmodel_p.h
index 5f91b5e8a0..c89084e207 100644
--- a/src/qmlmodels/qqmlobjectmodel_p.h
+++ b/src/qmlmodels/qqmlobjectmodel_p.h
@@ -99,6 +99,8 @@ public:
virtual int indexOf(QObject *object, QObject *objectContext) const = 0;
virtual const QAbstractItemModel *abstractItemModel() const { return nullptr; }
+ virtual bool setRequiredProperty(int index, const QString &name, const QVariant &value);
+
Q_SIGNALS:
void countChanged();
void modelUpdated(const QQmlChangeSet &changeSet, bool reset);
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();
}
}
diff --git a/src/qmlmodels/qqmltableinstancemodel_p.h b/src/qmlmodels/qqmltableinstancemodel_p.h
index defe513ef9..18421bae38 100644
--- a/src/qmlmodels/qqmltableinstancemodel_p.h
+++ b/src/qmlmodels/qqmltableinstancemodel_p.h
@@ -119,6 +119,8 @@ public:
QQmlIncubator::Status incubationStatus(int index) override;
+ bool setRequiredProperty(int index, const QString &name, const QVariant &value) final;
+
QVariant variantValue(int, const QString &) override { Q_UNREACHABLE(); return QVariant(); }
void setWatchedRoles(const QList<QByteArray> &) override { Q_UNREACHABLE(); }
int indexOf(QObject *, QObject *) const override { Q_UNREACHABLE(); return 0; }