Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Return value from JS function to CPP. How?

Return value from JS function to CPP. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 223 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bogong
      wrote on last edited by
      #2

      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));
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved