Skip to content

Commit

Permalink
Fix ListModel.get(idx) == ListModel.get(idx)
Browse files Browse the repository at this point in the history
This is a regression introduced with commit
4876ea6. Where we previously always
returned the same JS object, we would afterwards return a new JS object
for every invocation, which breaks reference comparison. As we store the
JS wrapper for the list element in the QQmlData->jsWrapper we can avoid
repeated allocations. In order for that wrapper to keep working after
modifications (insertion, etc.) to the list model, we have to replace
the static element index with a reference to the node model meta-object,
which also has an element index that however is kept up-to-date by the
list model itself.

Change-Id: I4368de6b6d86687fe96fbf73bd60b80b69d7b058
Task-number: QTBUG-52017
Reviewed-by: Michael Brasser <michael.brasser@live.com>
(cherry picked from commit 44a89492b49f23a975377795dbb7a48916cb5081)
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
  • Loading branch information
tronical committed Feb 23, 2018
1 parent b6d1e97 commit ec4c897
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/qml/types/qqmllistmodel.cpp
Expand Up @@ -1342,7 +1342,7 @@ void ModelObject::put(Managed *m, String *name, const Value &value)
ModelObject *that = static_cast<ModelObject*>(m);

ExecutionEngine *eng = that->engine();
const int elementIndex = that->d()->m_elementIndex;
const int elementIndex = that->d()->elementIndex();
const QString propName = name->toQString();
int roleIndex = that->d()->m_model->m_listModel->setExistingProperty(elementIndex, propName, value, eng);
if (roleIndex != -1)
Expand All @@ -1369,7 +1369,7 @@ ReturnedValue ModelObject::get(const Managed *m, String *name, bool *hasProperty
QQmlPropertyCapture::OnlyOnce, false);
}

const int elementIndex = that->d()->m_elementIndex;
const int elementIndex = that->d()->elementIndex();
QVariant value = that->d()->m_model->data(elementIndex, role->index);
return that->engine()->fromVariant(value);
}
Expand All @@ -1387,7 +1387,7 @@ void ModelObject::advanceIterator(Managed *m, ObjectIterator *it, Value *name, u
ScopedString roleName(scope, v4->newString(role.name));
name->setM(roleName->d());
*attributes = QV4::Attr_Data;
QVariant value = that->d()->m_model->data(that->d()->m_elementIndex, role.index);
QVariant value = that->d()->m_model->data(that->d()->elementIndex(), role.index);
p->value = v4->fromVariant(value);
return;
}
Expand Down Expand Up @@ -2295,9 +2295,14 @@ QQmlV4Handle QQmlListModel::get(int index) const
result = QV4::QObjectWrapper::wrap(scope.engine, object);
} else {
QObject *object = m_listModel->getOrCreateModelObject(const_cast<QQmlListModel *>(this), index);
result = scope.engine->memoryManager->allocObject<QV4::ModelObject>(object, const_cast<QQmlListModel *>(this), index);
// Keep track of the QObjectWrapper in persistent value storage
QQmlData::get(object)->jsWrapper.set(scope.engine, result);
QQmlData *ddata = QQmlData::get(object);
if (ddata->jsWrapper.isNullOrUndefined()) {
result = scope.engine->memoryManager->allocObject<QV4::ModelObject>(object, const_cast<QQmlListModel *>(this));
// Keep track of the QObjectWrapper in persistent value storage
ddata->jsWrapper.set(scope.engine, result);
} else {
result = ddata->jsWrapper.value();
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/qml/types/qqmllistmodel_p_p.h
Expand Up @@ -164,15 +164,17 @@ namespace QV4 {
namespace Heap {

struct ModelObject : public QObjectWrapper {
void init(QObject *object, QQmlListModel *model, int elementIndex)
void init(QObject *object, QQmlListModel *model)
{
QObjectWrapper::init(object);
m_model = model;
m_elementIndex = elementIndex;
QObjectPrivate *op = QObjectPrivate::get(object);
m_nodeModelMetaObject = static_cast<ModelNodeMetaObject *>(op->metaObject);
}
void destroy() { QObjectWrapper::destroy(); }
int elementIndex() const { return m_nodeModelMetaObject->m_elementIndex; }
QQmlListModel *m_model;
int m_elementIndex;
ModelNodeMetaObject *m_nodeModelMetaObject;
};

}
Expand Down
1 change: 1 addition & 0 deletions tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp
Expand Up @@ -449,6 +449,7 @@ void tst_qqmllistmodel::dynamic_data()
QTest::newRow("get2") << "{get(-1) === undefined}" << 1 << "" << dr;
QTest::newRow("get3") << "{append({'foo':123});get(0) != undefined}" << 1 << "" << dr;
QTest::newRow("get4") << "{append({'foo':123});get(0).foo}" << 123 << "" << dr;
QTest::newRow("get5") << "{append({'foo':123});get(0) == get(0)}" << 1 << "" << dr;
QTest::newRow("get-modify1") << "{append({'foo':123,'bar':456});get(0).foo = 333;get(0).foo}" << 333 << "" << dr;
QTest::newRow("get-modify2") << "{append({'z':1});append({'foo':123,'bar':456});get(1).bar = 999;get(1).bar}" << 999 << "" << dr;

Expand Down

0 comments on commit ec4c897

Please sign in to comment.