Hbi Giu Hbi Giu - 4 months ago 32
C++ Question

Qml and C++ interaction: return by pointer or by copy

So I have a Q_PROPERTY exposed to qml like below

Q_PROPERTY(QVariantMap info READ getInfo NOTIFY infoChanged)

QVariantMap ProfileInfo::getInfo()
{
return info;
}


Now, my question is would the approach below would be more efficient and less costly?

Q_PROPERTY(QVariantMap* info READ getInfo NOTIFY infoChanged)

QVariantMap* ProfileInfo::getInfo()
{
return &info;
}

Answer

Just slightly more efficient (QVariantMap uses implicit sharing, so a copy is not expensive), but your biggest problem is that QML doesn't know how to deal with a QVariantMap *, only with a QVariantMap (see here).