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. [SOLVED] include stateless js files
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] include stateless js files

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 5.5k 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.
  • G Offline
    G Offline
    giovannie
    wrote on 27 Jun 2012, 18:59 last edited by
    #1

    Hello.

    What do you think this program shows?
    Is it good that file with .pragma library becomes statefull?

    @//main.qml
    import QtQuick 1.1
    import "stateless.js" as A
    import "statefull.js" as B

    Text {
    id: text
    Component.onCompleted: {
    A.x = 1;
    B.x = 2;
    text.text = "A.x = " + A.x + "; B.x = " + B.x
    }
    }@

    @//statefull.js
    Qt.include("stateless.js")@

    @//stateless.js
    .pragma library

    var x@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chriadam
      wrote on 28 Jun 2012, 00:24 last edited by
      #2

      Hi.

      The documentation about .pragma library scripts is plain wrong.
      .pragma library scripts are not stateless - they just have a single state which is shared across all imports.

      Note that doing Qt.include() does NOT import a script - instead, it copy-and-pastes the script contents into the current script's evaluation context and evaluates it. Only an 'import "someScript.js" as SomeQualifier' statement in a QML file, or a '.import "someScript.js" as SomeQualifier' statement in a JS file (qtquick2 only), are true "import statements" -- ie, which observe imports / pragmas defined.

      ALL javascript imports are stateful. It's just that some are shared state (pragma library scripts) and some are per-import state (non-pragma library scripts).

      Here's an example:

      @
      // test.qml
      import QtQuick 2.0

      Rectangle {
      id: root
      width: 400
      height: 400

      Comp1 {
          id: c1
          anchors.top: root.top
      }
      
      Comp2 {
          id: c2
          anchors.bottom: root.bottom
      }
      

      }

      // Comp1.qml
      import QtQuick 2.0
      import "js1.js" as A
      import "js2.js" as B

      Text {
      id: text1
      Component.onCompleted: {
      A.x = A.x + 1;
      B.x = B.x + 2;
      text1.text = "A.x = " + A.x + "; B.x = " + B.x
      }
      }

      // Comp2.qml
      import QtQuick 2.0
      import "js1.js" as A
      import "js2.js" as B

      Text {
      id: text2
      Component.onCompleted: {
      A.x = A.x + 1;
      B.x = B.x + 2;
      text2.text = "A.x = " + A.x + "; B.x = " + B.x
      }
      }

      // js1.js
      Qt.include("js2.js")

      // js2.js
      .pragma library
      var x = 1
      @

      We expect the output to display either:

      A.x = 2; B.x = 3
      A.x = 2; B.x = 5

      OR

      A.x = 2; B.x = 5
      A.x = 2; B.x = 3

      depending on the order with which the component onCompleted handlers are invoked.
      In fact, with QtQuick2, currently the second one is what we see (but that's basically arbitrary).
      The important point is that A.x in both will equal 2 (the state of the "js1.js" import is NOT shared because it's not a pragma library, while the state of the "js2.js" import IS shared because it is.)

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giovannie
        wrote on 28 Jun 2012, 06:28 last edited by
        #3

        Thank you very much.

        So is it impossible (in QtQuick 1.1) to have global variables for all of my project? Is library with shared state accessible only from qml files but not from js files?

        I already did a workaround for this but it is not very beautiful.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chriadam
          wrote on 28 Jun 2012, 23:25 last edited by
          #4

          It's difficult in QtQuick1.x but not impossible. You can, for example, use a single .pragma library script as the single repository of shared global data, and then import it from all QML files where you need it.
          For those non-pragma-library JavaScript files which need to access that data, you'll have to pass the data to them as function arguments, rather than attempting to access it directly (since in QtQuick 1.x there is no way to import one JavaScript file from another; that is a QtQuick2 feature, as are module apis).

          Cheers,
          Chris.

          1 Reply Last reply
          0

          1/4

          27 Jun 2012, 18:59

          • Login

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