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

Return value from JS function to CPP. How?



  • Hello all!

    I know how to run JS function in QML object from CPP. Something like this in *.qml:

    ACustomType1 {
    
    	id: oCustomType1_1;
    	objectName: "oCustomType1_1";
    
    	function mTestJSFunction() {
    		console.log("mTestJSFunction");
    	}
    
    	function mReturnValue() { // How to run this?
    		return 1234;
    	}
    }
    

    in *.cpp

    QObject* oCustomType1 = oEngine.rootObjects().first()->findChild<QObject*>("oCustomType1_1");
    QMetaObject::invokeMethod(oCustomType1,"mTestJSFunction");
    

    How to run JS function in CPP and return value? Not just getting default boolean value.
    This code do not working:

    quint32 oReturnValue;
    qRegisterMetaType<quint32>("quint32");
    QMetaObject::invokeMethod(oCustomType2,"mReturnValue",Q_RETURN_ARG(quint32, oReturnValue));
    

    It's return false in QMetaObject::invokeMethod, but when no use of Q_RETURN_ARG(quint32, oReturnValue) method called. What am I missing?



  • Solution found. issue closed.
    It's working with QVariant data type, that is default for QML/JS -> CPP exchange.
    This code working perfectly:

    QVariant oReturnValue("no_value");
    QMetaObject::invokeMethod(oCustomType2,"mReturnValue",Q_RETURN_ARG(QVariant, oReturnValue));
    

Log in to reply