QObject::property(const char* name) in QML
-
wrote on 29 Mar 2011, 22:10 last edited by
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
-
wrote on 30 Mar 2011, 16:03 last edited by
Anyone knows at least if this is possible at all?
-
wrote on 30 Mar 2011, 19:12 last edited by
Could you please provide some pseudo-code to make your intention more clear?
-
wrote on 30 Mar 2011, 19:58 last edited by
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
@ -
wrote on 30 Mar 2011, 20:19 last edited by
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.
-
wrote on 30 Mar 2011, 20:32 last edited by
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?
-
wrote on 31 Mar 2011, 09:02 last edited by
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.
-
wrote on 1 Apr 2011, 11:54 last edited by
I'm talking about the ECMA-Script engine in QML, so it is kind of related to QScriptClass.
But as long as we don't have access to the QML script engine, it probably is not possible to implement my idea.
1/8