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

Jérôme Godbout godboutj at amotus.ca
Fri Mar 1 22:29:35 CET 2019


Why is the struct deleted one the signal is finished? You create a structure only for the signal? it’s not an object or data that will outlive that call in the end? If so, you really need to make a copiable representation of the struct.

What kind of data live into your struct? are they all basic type or pointer? or do you have QObject into it?
QVariantMap seem like an easy way out.

JSON serialization or even better QDataStream (which is really fast compare to JSON) could help you do it. You could do an operator<<() and operator>>() with QDataStream on your struct to do it. QByteArray are copiable.

If you can provide more information about your struct data that could help.

From: Sylvain Pointeau <sylvain.pointeau at gmail.com>
Sent: March 1, 2019 3:27 PM
To: Jérôme Godbout <godboutj at amotus.ca>
Cc: Qt Project <interest at qt-project.org>
Subject: Re: [Interest] using a custom class as parameter of signal/slot, used in QML

Hello Jerome,

On Fri, Mar 1, 2019 at 5:25 PM Jérôme Godbout <godboutj at amotus.ca<mailto:godboutj at amotus.ca>> wrote:
Since signal are not sync all the time but can be async, the argument need to be copiable to allow this. So you need to be able to copy the arguments on the signals. Therefore the QObject is not copiable, they cannot be passed as arguments directly, you need a pointer. The ownership and lifetime of that object is up to you, but you need it to live until all handler have finish processing the signal (this can be longer then the signal emit for queued connection).

Not only it can be asynchrone but also multiple "listeners". One consumer might be QML, one other might be another Object.
The lifetime is simply impossible to predict, and the ownership is really an issue.

if the copy would have been possible, we could have used a reference counted object.

If you can easily copy the structure you probably can serialize/deserialize it or put it into a QVariant, QVariantList or QVariantMap. That would be copiable.

Yes, I agree, this is just another mechanism to implement. I just did it in Json for one object but this is not ideal neither, but workable. (I hesitate with QVariant)






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190301/4dc5fb86/attachment.html>


More information about the Interest mailing list