Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Dynamically add functions to C++ objects exposed to JS



  • Hello,

    I have an ID and a type representing some Object O of type T in my application. I do not have direct access to this object. I only have its ID. I however want to expose this object to JS. I cannot create "normal" QObject wrappers for each of the classes I need to exposed with Q_INVOKABLE methods because:

    available methods are numerous and may be changing a lot in the future; So, if I need to write QObject wrappers, I will spend all my time (and maybe more) maintaining them;

    Objects are potentially heavy and copying them here and there is costly.

    Instead, I can retrieve the list of prototypes attached to each classes I need to expose to JS. So, what I want to do:

    From ID, retrieve the list of functions prototypes attached to this object;

    Dynamically attach these functions to a QObject (or similar); each function should call some global function.

    Pass this object to the JS engine and use it.

    At that point, I would be ok.

    So:
    Is it at least a good idea? If yes, how can I dynamically attach methods to newly created objects and pass them to the JS engine?

    Thanks for any help.


Log in to reply