@@ -533,6 +533,42 @@ void QQmlDelegateModel::setRootIndex(const QVariant &root)
533
533
}
534
534
}
535
535
536
+ /* !
537
+ \qmlproperty enumeration QtQml.Models::DelegateModel::delegateModelAccess
538
+
539
+ \include delegatemodelaccess.qdocinc
540
+ */
541
+ QQmlDelegateModel::DelegateModelAccess QQmlDelegateModel::delegateModelAccess () const
542
+ {
543
+ Q_D (const QQmlDelegateModel);
544
+ return d->m_adaptorModel .delegateModelAccess ;
545
+ }
546
+
547
+ void QQmlDelegateModel::setDelegateModelAccess (
548
+ QQmlDelegateModel::DelegateModelAccess delegateModelAccess)
549
+ {
550
+ Q_D (QQmlDelegateModel);
551
+ if (d->m_adaptorModel .delegateModelAccess == delegateModelAccess)
552
+ return ;
553
+
554
+ if (d->m_transaction ) {
555
+ qmlWarning (this ) << tr (" The delegateModelAccess of a DelegateModel "
556
+ " cannot be changed within onUpdated." );
557
+ return ;
558
+ }
559
+
560
+ if (d->m_complete ) {
561
+ _q_itemsRemoved (0 , d->m_count );
562
+ d->m_adaptorModel .delegateModelAccess = delegateModelAccess;
563
+ _q_itemsInserted (0 , d->adaptorModelCount ());
564
+ d->requestMoreIfNecessary ();
565
+ } else {
566
+ d->m_adaptorModel .delegateModelAccess = delegateModelAccess;
567
+ }
568
+
569
+ emit delegateModelAccessChanged ();
570
+ }
571
+
536
572
/* !
537
573
\qmlmethod QModelIndex QtQml.Models::DelegateModel::modelIndex(int index)
538
574
@@ -904,7 +940,9 @@ static bool isDoneIncubating(QQmlIncubator::Status status)
904
940
return status == QQmlIncubator::Ready || status == QQmlIncubator::Error;
905
941
}
906
942
907
- void QQDMIncubationTask::initializeRequiredProperties (QQmlDelegateModelItem *modelItemToIncubate, QObject *object)
943
+ void QQDMIncubationTask::initializeRequiredProperties (
944
+ QQmlDelegateModelItem *modelItemToIncubate, QObject *object,
945
+ QQmlDelegateModel::DelegateModelAccess access)
908
946
{
909
947
// QQmlObjectCreator produces a private internal context.
910
948
// We can always attach the extra object there.
@@ -976,9 +1014,18 @@ void QQDMIncubationTask::initializeRequiredProperties(QQmlDelegateModelItem *mod
976
1014
object, propName, requiredProperties,
977
1015
engine, &wasInRequired);
978
1016
if (wasInRequired) {
979
- QQmlAnyBinding binding = QQmlPropertyToPropertyBinding::create (
980
- engine, QQmlProperty (itemOrProxy, propName), targetProp);
981
- binding.installOn (targetProp);
1017
+ QQmlProperty sourceProp (itemOrProxy, propName);
1018
+ QQmlAnyBinding forward = QQmlPropertyToPropertyBinding::create (
1019
+ engine, sourceProp, targetProp);
1020
+ if (access != QQmlDelegateModel::Qt5ReadWrite)
1021
+ forward.setSticky ();
1022
+ forward.installOn (targetProp);
1023
+ if (access == QQmlDelegateModel::ReadWrite && sourceProp.isWritable ()) {
1024
+ QQmlAnyBinding reverse = QQmlPropertyToPropertyBinding::create (
1025
+ engine, targetProp, sourceProp);
1026
+ reverse.setSticky ();
1027
+ reverse.installOn (sourceProp);
1028
+ }
982
1029
}
983
1030
}
984
1031
}
@@ -1163,7 +1210,8 @@ void QQmlDelegateModelGroupEmitter::destroyingPackage(QQuickPackage *) {}
1163
1210
void QQmlDelegateModelPrivate::setInitialState (QQDMIncubationTask *incubationTask, QObject *o)
1164
1211
{
1165
1212
QQmlDelegateModelItem *cacheItem = incubationTask->incubating ;
1166
- incubationTask->initializeRequiredProperties (incubationTask->incubating , o);
1213
+ incubationTask->initializeRequiredProperties (
1214
+ incubationTask->incubating , o, m_adaptorModel.delegateModelAccess );
1167
1215
cacheItem->object = o;
1168
1216
1169
1217
if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(cacheItem->object ))
@@ -1266,8 +1314,14 @@ QObject *QQmlDelegateModelPrivate::object(Compositor::Group group, int index, QQ
1266
1314
ctxt->setContextObject (cacheItem);
1267
1315
cacheItem->contextData = ctxt;
1268
1316
1269
- if (m_adaptorModel.hasProxyObject ())
1270
- ctxt = cacheItem->initProxy ();
1317
+ // If the model is read-only we cannot just expose the object as context
1318
+ // We actually need a separate model object to moderate access.
1319
+ if (m_adaptorModel.hasProxyObject ()) {
1320
+ if (m_adaptorModel.delegateModelAccess == QQmlDelegateModel::ReadOnly)
1321
+ cacheItem->initProxy ();
1322
+ else
1323
+ ctxt = cacheItem->initProxy ();
1324
+ }
1271
1325
1272
1326
cp->incubateObject (
1273
1327
cacheItem->incubationTask ,
0 commit comments