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

Using context property in Javascript. How?



  • Hello all!
    I got stacked in very simple issue. Totally forgotten how to call from JavaScript.
    Here the example *.cpp

    ObjectType* oObject = new ObjectType();
    oRootContext->setContextProperty("ObjectType",oObject);
    

    in *.qml:

    Component.onCompleted: {
    	ObjectType.someMethod();
    }
    

    in *.js:

    ObjectType.someMethod(); // this not working in JavaScript directly
    

    What am I missing? How to make this method be working in JS directly?



  • @bogong Hi,

    someMethod must be a slot, or be annotated with Q_INVOKABLE macro. is it the case ?

    What do you mean by "this not working". Do you see any error ? Nothing happens?
    edit: add code

    class ObjectType : public QObject {
    // Like this
    public slots:
        void someMethod();
    // Or
    public:
        Q_INVOKABLE void someInvokableMethod();
    }
    

Log in to reply