1

Does QtScript support introspection/reflection (i.e. like python's dir() for example) that would enble me to 'map out' an api from the inside by exploring the object model at runtime?

PixelRouter
  • 103
  • 6

2 Answers2

1

You really should look more at QObject and QMetaObject.

QScriptValue supports the types defined in the ECMA-262 standard: The primitive types, which are Undefined, Null, Boolean, Number, and String; and the Object type. Additionally, Qt Script has built-in support for QVariant, QObject and QMetaObject.

From Qt's QMetaObject page:

Qt's meta-object system provides the signals and slots mechanism for inter-object communication, run-time type information, and the dynamic property system.

QObject contains the method children which returns a list of children for the object and a parent method.

On top of this, from pyqt's perspective these are python objects, the inspect module works well on them from my simple tests.

You should be able to build one with anyone of these.

FTR, afaik PyQt and Qt are the same thing, provide the same information. Ultimately if it works in C++ Qt, it should work in PyQt.

Mike Ramirez
  • 10,750
  • 3
  • 26
  • 20
1

Yes. You didn't specify if you want to do this from within QtScript or within C++.

Within the script engine you can use standard ECMAscript techniques to iterate over all properties on an object:

for (var property_name in some_object) {
  // do something with each property
}

This should include e.g. any slots, signals and Q_PROPERTYs on a QObject imported into the script engine. It will not include any C++ methods which weren't marked as a signal, slot, or Q_INVOKABLE.

Within C++ you can use QScriptValueIterator to iterate over all properties on any object in the script engine.

rohanpm
  • 4,264
  • 17
  • 16