2

I'm making an application in which I'd like to call a function from QML in C++ source, and that c++ function to return me and object which I can use it with the same properties in the javascript part of QML. I've made the connection and everything. I've tried to send a QVariantMap and tried to use that object in javascript, but I don't get the properties of that object

3
  • So at this stage your QML recognizes the c++ objects, correct? Did you create read and write functions for the elements you want to access? Commented Jul 21, 2011 at 17:17
  • I have something like this, this c++ method to return a qvariant map QVariantMap Mediator::initialize() { vector<Node*> table; Mediator m; Node TestNode("1", table); QVariant test = QVariant::fromValue(TestNode); QVariantMap map; map.insert("1",proba); return map; } and later in the javascript source I have: function foo(anObject) { console.log(anObject.getId()); } So I want to call a methods from that object. So far if I try to access the map it only prints "QVariant(Node)" Sorry for the unformatted text Commented Jul 22, 2011 at 9:24
  • Are you sure the object is returned correctly? I personally haven't seen any samples with functions returning object instances (I could be wrong). Also I believe any objects exposed to qml need to inherit from QDeclarativeItem and registered with qml... Finally, if it all works ok and you get the correct instance back I would expect to get the object's id via the id property : anObject.id (not the underlying getter function) Commented Aug 10, 2011 at 7:35

3 Answers 3

3

There are two ways for exporting QObject based types from C++ to QML:

  1. Return standalone QObject directly from property READer or Q_INVOKABLE function. Note, object returned as a property has C++ ownership, Q_INVOKABLE-object has JS ownership. You can change this default behaviour via http://doc.qt.nokia.com/4.7/qdeclarativeengine.html#setObjectOwnership.
  2. Return array of QObjects. In that case you should use QObjectList, QDeclarativePropertyMap (not QVariantMap) or QAbstractListModel.
Sign up to request clarification or add additional context in comments.

Comments

1

Your classes to be exposed have to inherit from QObject (or QDeclarativeItem if they are UI components) and you will have to register their types in your main() or in a Qt plugin before loading the QML code.

Have a look at http://developer.qt.nokia.com/doc/qt-4.7/declarative-tutorials-extending-chapter1-basics.html

Comments

0

To pass an object from C++ to QML as a function return value, the return value type needs to be QVariant, not QVariantMap, even though that is the type in the C++ code. So just change your initialize function signature to

QVariant initialize();

without changing anything else, and then you can access the properties.

Regarding your later comment on wanting to call methods on that returned object, that is not possible; the returned object is just a set of name-value pairs. If you want the object to have, say, an id property, you need to insert a value with that key to the QVariantMap in C++ before returning it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.