[Qt-qml] Passing QVariant from QML to CPP

Gregory Schlomoff gregory.schlomoff at gmail.com
Thu Jan 13 02:47:39 CET 2011


This may not be what you want, but have you loooked at QVariantMap ?
There is an automatic conversion between QVariantMap on the C++ side a
JS objects on the QML side, and this is quite aweosme.

You can write code like this:

Q_PROPERTY(QVariantMap customProp READ [...] )

And then in QML :

Text {
  text: myObj.customProp.nameOfAProperty
}

You can also use the JS object notation to set the properties :
myObj.customProp = {someProperty: "hello", otherStuff: 33}

Everything is explained here:
http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html



On Thu, Jan 13, 2011 at 8:13 AM, James Ausmus <james.ausmus at intel.com> wrote:
> Hi All-
>
> I'm trying to make Q_INVOKABLE methods on my custom Qt class that allow
> get/set of random custom properties by name (stored in the class as a
> QMap<QString, QVariant>). However, when I try and call the getter from a QML
> context, the QVariant default value arg always comes up as "invalid" (from a
> QtCreator debugger viewpoint) - I would have thought that automatic conversion
> *to* a QVariant in a QML->CPP transition would be available - am I just doing
> it wrong?
>
> Here's some (condensed) sample code:
>
> MyObj::getCustomProp(const QString &propName, const QVariant &defaultValue =
> QVariant())
> {
>  if (!mCustomProps.contains(propName))
>    setCustomProp(propName, defaultValue);
>  return mCustomProps.value(propName);
> }
>
> and the QML that invokes it:
>
> myObj.getCustomProp("MyPropName", true);
>
>
> This also doesn't work with any well-known type - ie I can't pass a number, a
> string, anything, and have it show up as a valid QVariant in getCustomProp.
>
>
> Any thoughts?
>
> Thanks!
>
> -James
> _______________________________________________
> 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