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. Context object being available to JS

Context object being available to JS

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.9k 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.
  • A Offline
    A Offline
    allbabel
    wrote on last edited by
    #1

    Hi,

    I have added an object to my context and this is available from the QML as such:

    @
    _engine->rootContext()->setContextProperty( "Proxy", _proxy );
    @

    and this works great in QML:

    @
    property variant size: Proxy.getFunction1()
    @

    If I try to use it in a JS file that is included in my QML as such:

    @
    function function2() {
    Proxy.function2();
    }
    @

    I get the following error:

    qrc:/qml/js/uni.js:17: ReferenceError: Can't find variable: Proxy

    Can't I add an object reference which is accessible from JS?

    Thanks in advance, Andy

    [EDIT: code formatting, please wrap in @-tags, Volker]

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cpscotti
      wrote on last edited by
      #2

      Hi Andy,

      One solution to the problem is to assign your context object to a javascript object.

      For example if you have, in a qml file, something like :
      @
      import "AppLogic.js" as AppLogic
      Page {
      id: mainPage
      }
      @

      together with a js like this:
      @
      var contextObj = ""
      ///the rest of your JS file
      @

      You can connect the context obj to "contextObj" at startup in the qml..

      @
      import "AppLogic.js" as AppLogic
      Page {
      id: mainPage
      Component.onCompleted: {
      AppLogic.contextObj = Proxy; //given that Proxy is the obj u added to the context
      }
      }
      @

      That way, after startup your JS file you'll have a "bridge" to the context object.
      Works for me.. :D

      cpscotti.com/blog/ - Used when I need to kill some time at work :D

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srikanth_trulyit
        wrote on last edited by
        #3

        Would that be easy to pass the context obj as a parameter

        @
        // inside javascript
        function doSomething(contextObj) {
        ...
        }
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cpscotti
          wrote on last edited by
          #4

          Yep.. would be easier but depending on what you're doing it would be a bit uglier.. or not.. :D

          cpscotti.com/blog/ - Used when I need to kill some time at work :D

          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