12-04-2012 10:52 PM
I have a c++ class exposed to QML using
qml->setContextProperty("MyObject", MyClass);
All functions returning basic types such as "int", "bool", "string"
Q_INVOKABLE int myFunction(int myID) {
return myID;
}
have no problem returning those values back to QML when called from QML:
var myNumber = MyObject.myFunction(44);
// myNumber now is set to 44
However when I have a function (in the same C++ class as above) which returns something like QDeclarativePropertyMap
Q_INVOKABLE QDeclarativePropertyMap* myMapFunction() {
QDeclarativePropertyMap* myMap = new QDeclarativePropertyMap;
myMap->insert("ID", QVariant(55));
return myMap;
}
when I call it I am getting [undefined] in QML
var myMap = MyObject.myMapFunction(); // myMap now is [undefined]any idea why?
12-05-2012 01:33 AM
i think you need to return a QVariant...if you cannot wrap the QDeclarativePropertyMap within a QVariant i.e QVariant(QDeclarativePropertyMap*), then you would need to:
1) create a custom wrapper class (with QObject defined within that class for use in QML)
2) register that custom wrapper class so that it is useable in QML: qmlRegisterType
12-05-2012 02:57 AM
12-05-2012 04:19 AM - edited 12-05-2012 04:20 AM
use QVariantMap :
Q_INVOKABLE QVariantMap myFunction(QString ID){}
Also, you can use Q_PROPERTY to get dynamic property :
class MyClass : public QObject{
Q_PROPERTY (QVariantMap data READ data NOTIFY dataChanged)
Q_OBJECT
public:
QVariantMap data(){return mData;}
void insertData(const QString& key)
{mData.insert(key, 42); emit dataChanged()}
signals:
void dataChanged();
private:
QVariantMap mData;
}
//From QML
//Use the property.. will automatically changed when dataChanged has emitted
Label {
text: myobject.data["key"]
}