[Interest] using a custom class as parameter of signal/slot, used in QML

Fabrice Mousset | GEOCEPT GmbH fabrice.mousset at geocept.com
Fri Mar 1 17:14:29 CET 2019

Hi Sylvain,

It is not so complicated, when passing an object instance to QML (it can only by pointers of QObject based classes), if the instance doesn’t have a parent, the QML Engine take ownership of the object and delete if when it is no more needed.
If the instance has a parent, then QML Engine don’t try to delete it. It is the parent which will delete the instance, at least when parent is deleted.
So, there is no memory leak issue.

Best regards


Von: Interest <interest-bounces at qt-project.org> Im Auftrag von Sylvain Pointeau
Gesendet: Freitag, 1. März 2019 17:08
An: Jason H <jhihn at gmx.com>
Cc: Qt Project <interest at qt-project.org>
Betreff: Re: [Interest] using a custom class as parameter of signal/slot, used in QML

Hi Jason,

thank you for the link, I did have a look but they are doing the same, with a comment "// parent-less QObject -> ownership transferred to the JS engine"

but what if there was no connection, or consumed by another object, then I guess there will be a memory leak?

I don't feel comfortable sending pointer to objects through the signals, the ownership for me is not clear.
I guess there is no real solution, except to avoid using objects in signals if QML is a consumer?

it is really puzzling me...

Best regards,

On Fri, Mar 1, 2019 at 4:12 PM Jason H <jhihn at gmx.com<mailto:jhihn at gmx.com>> wrote:
Check out qtmultimedia/examples/multimedia/video/qmlvideofilter_opencl fo the filter result types. I think that's what you want?

I find it easier to code and work with to just rely on QVariant conversion. QVariantList and QVariantMap are transparently converted to JS Objects.

Sent: Friday, March 01, 2019 at 6:43 AM
From: "Sylvain Pointeau" <sylvain.pointeau at gmail.com<mailto:sylvain.pointeau at gmail.com>>
To: "Qt Project" <interest at qt-project.org<mailto:interest at qt-project.org>>
Subject: [Interest] using a custom class as parameter of signal/slot, used in QML
Dear all,

I understood how to use a struct / simple class as a parameter of a signal / slot, and it works well by value.

However as soon as we have to declare the type for QML, it is not good anymore.
It seems that it must be derived from QObject and passed by pointer.

Do I understand well that the QML world does only accept pointers on QObject?

However I don't understand, who owns the pointer of the custom class (derived from QObject) and who actually deletes it?
(I tried a sharepointer but QML does not know how to handle it)

Do you have any idea?

Best regards,

_______________________________________________ Interest mailing list Interest at qt-project.org<mailto:Interest at qt-project.org> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190301/392cff82/attachment.html>

More information about the Interest mailing list