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:
    @["/foo/bar.0001.exr",
    "/foo/bar.0002.exr",
    "/foo/bar.0003.exr",
    "/foo/bar.0004.exr"]@

    could be represented as an Object with a "toString()" method that returns
    @"/foo/bar.d.exr"@

    and a "frameList" property that returns
    @[1,2,3,4]@

    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.

    cheers,
    -Mark



  • 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).

    Cheers,
    Chris.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.