[Qt-interest] Installing a hook in QtScript to get notified when a meta-property is accessed
Laurent Gomila
laurent.gom at gmail.com
Tue Jun 22 11:08:44 CEST 2010
Hi
I'm using QtScript heavily in my application, all the objects that I
use in my scripts are QObjects. I'd like to receive a C++ notification
whenever a property of an object is read or written from a script
(from C++ code would work too, but I don't think that QObject offers
this possibility). And of course I don't want to add specific code in
every getter/setter of my objects, it must be performed automatically.
Something like a global hook that I would be able to install/uninstall
whenever I need to observe what happens to my properties.
I first tried using QScriptEngineAgent. I can receive a lot of
notifications (contextPush, functionEntry, ...) but using
engine()->currentContext() to retrieve the property name and argument
never works, it seems that the context is invariably empty: no callee,
no argument, ... I tried this in every virtual function of
QScriptEngineAgent, just to make sure that I was not watching the
wrong event. I'm surprised because QScriptContext and
QScriptContextInfo contain exactly the information that I need. But
the only place where this information is properly defined seems to be
inside the getter/setter of the properties that I want to watch (if my
objects inherit from QScriptable), which is obviously rather useless.
After a little debugging into the QtScript sources, it appears that
the agent is never notified when a native function is called. For
example, QtPropertyFunction::call directly calls
QScriptEnginePrivate::pushContext, which doesn't notify the agent.
So when am I supposed to be able to get contextInfo.functionType() ==
QScriptContextInfo::QtPropertyFunction, for example, if the internal
QtPropertyFunction class never notifies the engine's agent?
I'm sure that I'm missing something, but I don't know what. QtScript
classes seem to provide everything that I need but I can't figure out
how to make them work.
I'm using Qt 4.6.3 on Windows XP with Visual C++ 2008.
More information about the Qt-interest-old
mailing list