[Qt-interest] Calling a QML-method within C++-code

Hendrik vP variadic.template at googlemail.com
Tue Apr 5 00:02:34 CEST 2011


After some investigations on the sourcecode of the qml-internals,
which said that it should be possible what I am trying to do
(
http://qt.gitorious.org/qt/qt/blobs/v4.7.0/src/declarative/qml/qdeclarativeengine.cpp#line1878

and
http://qt.gitorious.org/qt/qt/blobs/v4.7.0/src/declarative/qml/qdeclarativemetatype.cpp#line631
),
i tried to change the declaration of my qvariant-variable. It turned out,
that QVariant(QMetaType::QObjectStar, object) is insufficient and
unnecessary - the way to go is
QVariant wrapped;
wrapped.setValue<QObject*>(object);
I don't understand how and why this works and the other declaration
not - maybe someone can brighten this thing up. But at least, it works
now.

- Hendrik

2011/4/4 Hendrik vP <variadic.template at googlemail.com>

> Hi,
>
> I defined a method with one argument within QML that i want
> to invoke in C++. In C++, I have the object that got the function
> added as slot, I also can see that the slot is added by iterating
> the methods within the metaobject. And, invoking a QML-method
> that has no arguments, already worked fine.
> The type of the argument is a QObject * and I expect to be able
> to access its properties within QML simply by
> function foo(item) {
>   console.log("start")
>   console.log(item.my_property)
>   console.log("end")
> }
> As the type of the arguments are QVariants within C++, i tried to
> call it by
> metamethod.invoke(target, Q_ARG(QVariant(QMetaType::QObjectStar,
> my_qobject)))
> I'm receiving no errors on the terminal, but also I'm only receiving
> the "start"-method, it seems to break when i try to access the property.
> Trying to print the item itself by console.log(item) gives me a
> "undefined".
> So, is it even possible to use a QObject* as QML-method-argument and
> when yes, how?
>
> - Hendrik
>
> References:
> http://doc.qt.nokia.com/4.7.1/qml-extending-types.html#adding-new-methods
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110405/c400671c/attachment.html 


More information about the Qt-interest-old mailing list