Is it ok to pass a QJsonArray to qml?
-
Hello,
I love qml and think it's great.
Recently I implemented a autocomplete suggestion-List and it worked like a charm.
The use case was the following:
A user can edit a text-field, embedded in the suggestionList-Component, and after some key-strokes suggestionList emits a signal, which should be handled by a parent. The handling parent calls the corresponding method of the api-Object(context-property). The api-object lives on c++ side and sends and receives the http-requests. After receiving the result-payload, the payload is deserialized and passed to qml via a signal emitted by the api-object. I passed the result in a QJsonArray and want to know, what you think about it? I'm asking, because I didn't find concrete documentation. Lots of people recommend a QAbstractListModel or a QQmlPropertyList, but I think that would be an overkill for my case.
Furthermore I don't know, how I should use a QAbstractListModel in that case.
Maybe you can help me.
Thanks
-
@wuarmin said in Is it ok to pass a QJsonArray to qml?:
Furthermore I don't know, how I should use a QAbstractListModel in that case.
- implement custom C++ model for QML
- request data
- on data receive call model reset
I passed the result in a QJsonArray and want to know, what you think about it?
you should use QVariantList for JavaScript lists, as described in this section.
-
Thank you for your response!
So one can say passing a QVariantList improves the performance, cause no transformation has to be done?Yesterday I tried a different thing. I passed a QList containing QObject-derived heap-object-pointer. It worked, but I could not figure out, how I have to delete them correctly on qml side. I don't want to manage such memory things at c++ side, cause I'm just passing the QList. Qml should manage it. Can you put me in the right direction?
Thanks and best regards. -
@wuarmin
try usingQQmlEngine::setObjectOwnership(object, QQmlEngine::JavaScriptOwnership)
to let the JavaScript garbage collector take care of it.
Basically objects created in QML have JavaScriptOwnership, objects created in C++ have CppOwnership.