Please nominate your Qt Champions for 2021!

Possible to override toString in Javascript for a QObject-derived class?

  • I've exposed a property from C++ called "paths". Currently, this property returns a const QStringList, which is nicely mapped to a Javascript array of strings. I now need "paths" to support sequences of frames, so each "path" now has additional metadata (the frame list), which may also involve QObject-derived classes. I'd like this new object to still behave like a string, but expose additional properties.

    For example, a sequence of 4 frames:

    could be represented as an Object with a "toString()" method that returns

    and a "frameList" property that returns

    I've tried adding this to the class:

    @ Q_INVOKABLE const QString toString() const;@

    But it has no effect. :-(

    So, currently possible? Good Idea/Bad Idea?

    I should mention that I'm somewhat familiar with the qtdeclarative module, so I'd be willing to take a stab at adding this feature myself if given a bit of direction on how to proceed (and if it's not a Bad Idea). I'm using Qt5 from github.


  • Hi,

    Check out the qv8qobjectwrapper.cpp:
    static v8::Handlev8::Value ToString(QV8Engine *engine, QObject *object, int, v8::Handlev8::Object)

    You can modify that to call the toString() function of object if it exists (eg, via QMetaObject::invokeMethod() after first doing a QMetaObject::property() lookup and determining if it's a function).


Log in to reply