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. otherway aroun: how to call qml function from javascript file?
Forum Updated to NodeBB v4.3 + New Features

otherway aroun: how to call qml function from javascript file?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 226 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Hello guys,

    im looking for a way to call QML function from inside of JS file, so lets say I have this:

    main.qml:

    ApplicationWindow {
        id: mainWindow
        width: 480
        height: 800
        title: qsTr("testapp")
    
        StackView {
            id: view
            anchors.fill: parent
            initialItem: view_ChoiseOffline
        }
        Component {
            id: view_ChoiseOffline
            ChoiseOffline {}
        }
    }
    

    ChoiseOffline.qml:

    import 'JavaScript.js' as JS
    Item {
            id: parentObject
    	Component.onCompleted: { JS.testFromJS(); }
    	
    	MouseArea {
    		id: mouseAreaID
    		anchors.fill: parent
    		function testFunction() { console.log("test passed"); }
    		onClicked: { testFunction(); }
    	}
    }
    

    JavaScript.js

    function testFromJS() {
        console.log("script inside JS works fine");    // this works fine
        testFunction();                                // this gives me error
    }
    

    obviously caling testFunction() and JS.testFromJS() from QML works fine...
    however when I call the testFunction() from JS file gives the error:

    ReferenceError: testFunction is not defined
    

    i tried several options of calling it, like parentObject.testFunction() or even view.currentItem.testFunction() and view.currentItem.parentObject.testFunction() or view.currentItem.mouseAreaID.testFunction() but nothing works...

    Can I ask for advice how to call QML function form JS file?
    I just need to keep the function inside the ChoiseOffline.qml file, so moving it out to main.qml is not an option to me.

    Any help appriciated

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      Its not in the same scope. How would it even know it exists?
      Either pass the function or the object to the JS function:

      Component.onCompleted: { JS.testFromJS(mouseAreaID); }
      ...
      function testFromJS(obj) {
        obj.testFunction()
      }
      

      C++ is a perfectly valid school of magic.

      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