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. Calling a function inside of a *.qs file

Calling a function inside of a *.qs file

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 87 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.
  • D Offline
    D Offline
    Doublelune27
    wrote on 6 Aug 2024, 19:58 last edited by
    #1

    Hi,
    I wanted to customize dynamically my small app (Qt 6.5.0, windows & linux), and therefore make use of script module.
    I want to call a javascript function from an external *.qs file.
    In my C++ app, I have tried this to call the script like this:

    void MainWindow::onRunScript()    {
            QJSEngine myEngine;
        QString fileName = "run.qs";
        QFile scriptFile(fileName);
        if (!scriptFile.open(QIODevice::ReadOnly)) {
            // handle error
            qDebug() << "MainWindow::onRunScript : unable to open " << fileName;
        }
        QTextStream stream(&scriptFile);
        QString contents = stream.readAll();
        scriptFile.close();
    
        QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
        QJSValue LogicValue = myEngine.newQObject(this);
        myEngine.globalObject().setProperty("Main", LogicValue);
        QJSValue val = myEngine.evaluate(contents);
        if(val.isError())
        {
            qDebug() << val.errorType();
        }
    }
    

    No error until this, my file run.qs exists, and QJSValue resulted from QJSEngine is no error.
    Here is my run.qs file:

    (function() {
    	Main.write(57,64);
    })
    

    Unfortunately, MainWindow::write(int,int) is never called.
    I guess, the issue is in the *.qs file. I have added the outer parenthesis because of a warning from compiler
    I didn't find any example to make this work. I barely remember of a *.mjs file that enabled several functions stand in the same file, but I did not find it either.
    Can anyone give me a link to an explanation about the external javascript files format please?
    thank you,
    Claude

    1 Reply Last reply
    0

    1/1

    6 Aug 2024, 19:58

    • Login

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