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. [Solved]Call function in script

[Solved]Call function in script

Scheduled Pinned Locked Moved General and Desktop
scriptqt5.5qjsengineqjsvaluesignal & slotjavascriptfunction
2 Posts 1 Posters 2.3k 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.
  • T Offline
    T Offline
    TheHawk
    wrote on 18 Sept 2015, 11:54 last edited by TheHawk
    #1

    I am trying to make an application which shows and let you edit some data. Currently, I am trying to integrate custom logic into the application where the user can execute custom code during runtime. I am using QJSEngine for this since QScriptEngine is deprecated.

    I evaluate and run the script in my class CustomCode like so:

    void CustomCode::on_button_clicked()
    {
        QLabel *tb = new QLabel;
        ui->verticalLayout->addWidget(tb);
        QJSEngine myEngine;
        QJSValue scripttb = myEngine.newQObject(tb);
    
        QJSValue jsfunctionObj = myEngine.newQObject(&jsfunction);
        myEngine.globalObject().setProperty("jsfunction",jsfunctionObj);
        myEngine.globalObject().setProperty("tb",scripttb);
    
    
        QString fileName = "customlogic.qs";
        QFile scriptFile(fileName);
        if (!scriptFile.open(QIODevice::ReadOnly))
        {
        }// handle error
    
        QTextStream stream(&scriptFile);
        QString contents = stream.readAll();
        scriptFile.close();
        QJSValue result = myEngine.evaluate(contents, fileName);
        //QJSValue result = myEngine.evaluate("jsfunction.call_test();");
        if (result.isError())
        {
            qDebug() << "result: " << result.property("lineNumber").toInt() << ":" << result.toString();
        }
    }
    

    this was tested and works. Now, what I want to do is access functions in my mainwindow via the script and being able to call functions inside the script from my mainwindow (when a parameter changes the script has to process new data). Would it be possible to connect a signal/slot from mainwindow to a function inside the script and vice versa? (I think I have the calling of a function of mainwindow from inside the script working correctly but I would like to see my other options as well)

    thanks!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TheHawk
      wrote on 21 Sept 2015, 09:20 last edited by TheHawk
      #2

      I managed using the following additional lines in CustomCode

      QString function = "test(1)";
      QJSValue testfunction = myEngine.evaluate(function);
      qDebug() << testfunction.toString();
      

      With my script CustomLogic.qs looking like this:

      function test(x)
      {
          var a = "tested!";
          a = a + x.toString();
          return a;
      }
      

      Returns "tested!1" in the application output

      1 Reply Last reply
      1

      2/2

      21 Sept 2015, 09:20

      • Login

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