[Qt-qml] Behaviour when sending QList<QObject*> as QVariant to QML?

Todd Rose teeceeare97 at gmail.com
Mon Oct 10 03:32:24 CEST 2011


You're already on the right track here.  One thing that might be
helpful is to do this in your QObject's constructor:

QDeclarativeEngine::setObjectOwnership (this, QDeclarativeEngine::CppOwnership);

We also had to implement reference counting because we shared our list
objects throughout the program.


On Sun, Oct 9, 2011 at 8:05 PM, Pris Matic <prismatic.project at gmail.com> wrote:
> I'll try to answer my own question, in the hopes that it might benefit
> someone else.
>
> You definitely need to maintain those QObjects in memory on the C++ side.
> When I pushed a QList<QObject*> to the model of a ListView, and then later
> deleted the QList (using qDeleteAll), I got an instant seg fault. There's
> also some unclear behaviour as to what happens when you clear the ListView
> of all elements, and then try to delete the QList. I ran into the same
> problem (seg fault) even though the model was now pointing to something
> else. So right now I keep track of everything I send to the declarative
> engine in this manner and then delete it when I exit the program. It's not
> ideal, but it's the only solution that works for me.
>
>
> Regards,
>
> -Pris
>
> On Sat, Oct 8, 2011 at 7:21 PM, Pris Matic <prismatic.project at gmail.com>
> wrote:
>>
>> Hi all,
>>
>> I have a question about what exactly happens when you have a
>> QList<QObject*> in your C++ code, and then you send it to QML with something
>> like this:
>>
>> QVariant Abc::GetObjectList()
>> {  return QVariant::fromValue(m_object_list);  }
>>
>> Where m_object_list is a QList<QObject*> where the QObjects have slots
>> that can be directly invoked from QML (imagine using the list for something
>> like a ListView).
>>
>> Does the QML side of things create a copy of the list, or do I have to
>> maintain it in C++? Ie if m_object_list was cleared after 'GetObjectList'
>> was called from QML, then would the list still be valid? If I was using a
>> ListView, would the ListView need to access that QList if it needed to draw
>> offscreen delegates as they became visible, etc.
>>
>>
>> Regards,
>>
>> -Pris
>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>
>


More information about the Qt-qml mailing list