Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ callback in JS using QScriptValue
Forum Updated to NodeBB v4.3 + New Features

C++ callback in JS using QScriptValue

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 487 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.
  • M Offline
    M Offline
    Montjet
    wrote on last edited by Montjet
    #1

    Hi,
    I can call JS function with arguments and get the result using QScriptValue but question is: how to send an argument as a C++ callback?

    My JS script:

    function test(testCallback)
    {
       testCallback(5);
    }
    

    And my callback (C++):

    void Test::testCallback(const qint32& value)
    {
    // Signal and other actions
    emit testSignal(value);
    // ...
    }
    

    Application won't use qml, but inherits some js functions from another project.

    1 Reply Last reply
    0
    • CKurduC Offline
      CKurduC Offline
      CKurdu
      wrote on last edited by
      #2

      Hi,
      You can send C++ callback function as an argument. Firstly, use QScriptEngine globalObject method to get QObject callback (your testCallback) and then use your test function with testCallback parameter.

          QScriptValue global = engine.globalObject();
          QScriptValue testCallback = global.property("Test").property("testCallback");
          QScriptValue test = global.property("test");
          QScriptValueList vals;
          vals<<testCallback<<23;
          QScriptValue result = test.call(global,vals);
          qDebug()<<"result : "<<result.toInt32()<<endl;
         
      
      
          
      

      You reap what you sow it

      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