QObject::property(const char* name) in QML
-
Hi,
is it possible (in pure QML or with the help of C++) to define a "fallback getter function" for not-yet-existing-properties of objects?
Actually, this would correspond to the functionality of QObject::property(...), but that is not called for a QObject accessed from QML, even if it is Q_INVOKABLE.Cheers!
Manuel
-
Ok, sure:
@
myObj.a = 1
console.log(myObj.a) # this should work fine, because the property "a" was defined before
console.log(myObj.b) # the property "b" does not exist yet. here a fallback function should be called#something like
myObj.propertyDoesNotExist = function(propertyName) {
console.log("Property '"+propertyName+"'does not exist, yet")
return 999;
}and if you access a non-existing property (e.g. "c"), this function should be called
console.log("c=")
console.log(myObj.c)@
So the output of the last two lines should be
@
Property 'c' does not exist,yet
c=
999
@ -
I could be wrong but it seems impossible without modification of the Qt core components such as QObject or QtScript module. It seems like you want to have functionality which is not appropriate to JavaScript at all. You can use some convention, like using some unified function for setting and accessing object properties. In such a way you will have full control over the properties.
-
Well, actually it would correspond to "QScriptClass::queryProperty":http://doc.qt.nokia.com/latest/qscriptclass.html#queryProperty, but afaik this is not usable in QML, right?
-
Frankly speaking, I don't know what the QScriptClass is and how it is related to the QML. But QML objects are descendants of the QDeclarativeItem class and it is not related to the QScriptClass . So if my logic is not broken the only way to accomplish your task is to modify QObject to make the setProperty method virtual. But it is a wrong way I believe.