aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlmodels/qqmlobjectmodel.cpp
diff options
context:
space:
mode:
authorUlf Hermann <[email protected]>2024-08-09 11:38:55 +0200
committerUlf Hermann <[email protected]>2024-08-12 12:34:31 +0200
commit7c404391c3971a7d20a6a64f25a7c89d65fab9db (patch)
treef05ab89258b3db96a44cfe3750e6fac3edae8b30 /src/qmlmodels/qqmlobjectmodel.cpp
parent04984f49cd2801a7817365e523f1f3996071d9ff (diff)
QQmlObjectModel: Do not store attached objects in global static
This is rather dangerous. The global static is not protected in any way against concurrent access. However, we already have a place to store attached objects. The base object's QQmlData. Just use that. Pick-to: 6.8 Task-number: QTBUG-124345 Change-Id: I5f254e640f836016564f297d2f39ea40e037acd8 Reviewed-by: Fabian Kosmale <[email protected]> Reviewed-by: Sami Shalayel <[email protected]> Reviewed-by: Olivier De Cannière <[email protected]>
Diffstat (limited to 'src/qmlmodels/qqmlobjectmodel.cpp')
-rw-r--r--src/qmlmodels/qqmlobjectmodel.cpp46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/qmlmodels/qqmlobjectmodel.cpp b/src/qmlmodels/qqmlobjectmodel.cpp
index 4921010911..fb98b330d9 100644
--- a/src/qmlmodels/qqmlobjectmodel.cpp
+++ b/src/qmlmodels/qqmlobjectmodel.cpp
@@ -18,9 +18,6 @@
QT_BEGIN_NAMESPACE
-QHash<QObject*, QQmlObjectModelAttached*> QQmlObjectModelAttached::attachedProperties;
-
-
class QQmlObjectModelPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QQmlObjectModel)
@@ -67,10 +64,8 @@ public:
void insert(int index, QObject *item) {
Q_Q(QQmlObjectModel);
children.insert(index, Item(item));
- for (int i = index; i < children.size(); ++i) {
- QQmlObjectModelAttached *attached = QQmlObjectModelAttached::properties(children.at(i).item);
- attached->setIndex(i);
- }
+ for (int i = index, end = children.size(); i < end; ++i)
+ setIndex(i);
QQmlChangeSet changeSet;
changeSet.insert(index, 1);
emit q->modelUpdated(changeSet, false);
@@ -80,10 +75,9 @@ public:
void replace(int index, QObject *item) {
Q_Q(QQmlObjectModel);
- auto *attached = QQmlObjectModelAttached::properties(children.at(index).item);
- attached->setIndex(-1);
+ clearIndex(index);
children.replace(index, Item(item));
- QQmlObjectModelAttached::properties(children.at(index).item)->setIndex(index);
+ setIndex(index);
QQmlChangeSet changeSet;
changeSet.change(index, 1);
emit q->modelUpdated(changeSet, false);
@@ -107,10 +101,9 @@ public:
for (int i = 0; i < n; ++i)
store.append(children[from + i]);
- for (int i = 0; i < store.count(); ++i) {
+ for (int i = 0, end = store.count(); i < end; ++i) {
children[from + i] = store[i];
- QQmlObjectModelAttached *attached = QQmlObjectModelAttached::properties(children.at(from + i).item);
- attached->setIndex(from + i);
+ setIndex(from + i);
}
QQmlChangeSet changeSet;
@@ -121,15 +114,11 @@ public:
void remove(int index, int n) {
Q_Q(QQmlObjectModel);
- for (int i = index; i < index + n; ++i) {
- QQmlObjectModelAttached *attached = QQmlObjectModelAttached::properties(children.at(i).item);
- attached->setIndex(-1);
- }
+ for (int i = index; i < index + n; ++i)
+ clearIndex(i);
children.erase(children.begin() + index, children.begin() + index + n);
- for (int i = index; i < children.size(); ++i) {
- QQmlObjectModelAttached *attached = QQmlObjectModelAttached::properties(children.at(i).item);
- attached->setIndex(i);
- }
+ for (int i = index, end = children.size(); i < end; ++i)
+ setIndex(i);
QQmlChangeSet changeSet;
changeSet.remove(index, n);
emit q->modelUpdated(changeSet, false);
@@ -152,6 +141,19 @@ public:
return -1;
}
+private:
+ void setIndex(int child, int index)
+ {
+ if (auto *attached = static_cast<QQmlObjectModelAttached *>(
+ qmlAttachedPropertiesObject<QQmlObjectModel>(children.at(child).item))) {
+ attached->setIndex(index);
+ }
+ }
+
+ void setIndex(int child) { setIndex(child, child); }
+ void clearIndex(int child) { setIndex(child, -1); }
+
+
uint moveId;
QList<Item> children;
};
@@ -283,7 +285,7 @@ int QQmlObjectModel::indexOf(QObject *item, QObject *) const
QQmlObjectModelAttached *QQmlObjectModel::qmlAttachedProperties(QObject *obj)
{
- return QQmlObjectModelAttached::properties(obj);
+ return new QQmlObjectModelAttached(obj);
}
/*!