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. Qml doesn't allow javascript singletons?
Forum Updated to NodeBB v4.3 + New Features

Qml doesn't allow javascript singletons?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 1.1k 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?

    Thanks!

    https://alaskafamilysystems.com/

    KroMignonK ODБOïO 2 Replies Last reply
    0
    • P patrickkidd

      I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?

      Thanks!

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @patrickkidd said in Qml doesn't allow javascript singletons?:

      Is there any way to accomplish this?

      I think what you are looking for is pragma Singleton

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      P 1 Reply Last reply
      0
      • P patrickkidd

        I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?

        Thanks!

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @patrickkidd hi
        https://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries

        1 Reply Last reply
        2
        • KroMignonK KroMignon

          @patrickkidd said in Qml doesn't allow javascript singletons?:

          Is there any way to accomplish this?

          I think what you are looking for is pragma Singleton

          P Offline
          P Offline
          patrickkidd
          wrote on last edited by
          #4

          @KroMignon said in Qml doesn't allow javascript singletons?:

          I think what you are looking for is pragma Singleton

          Thanks, but that is for singleton Qml components not Javascript files.

          @LeLev said in Qml doesn't allow javascript singletons?:

          @patrickkidd hi
          https://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries

          That was what I was looking for. However, of my 7 or so qml files that import this (all loaded in a different QQuickWidget), the top-level code is still being executed twice. That's better than the 7 times it was executed before, but it should only be executed once. All QQuickWidget instances are using the same QQmlEngine so I'm not sure what's going on here.

          https://alaskafamilysystems.com/

          ODБOïO 1 Reply Last reply
          0
          • P patrickkidd

            @KroMignon said in Qml doesn't allow javascript singletons?:

            I think what you are looking for is pragma Singleton

            Thanks, but that is for singleton Qml components not Javascript files.

            @LeLev said in Qml doesn't allow javascript singletons?:

            @patrickkidd hi
            https://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries

            That was what I was looking for. However, of my 7 or so qml files that import this (all loaded in a different QQuickWidget), the top-level code is still being executed twice. That's better than the 7 times it was executed before, but it should only be executed once. All QQuickWidget instances are using the same QQmlEngine so I'm not sure what's going on here.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            sorry i forgot to answer
            @patrickkidd said in Qml doesn't allow javascript singletons?:

            executed twice

            I did the test couple of days ago and the code in my js file is executed only once.
            How do you import the file ?

            P 1 Reply Last reply
            0
            • ODБOïO ODБOï

              sorry i forgot to answer
              @patrickkidd said in Qml doesn't allow javascript singletons?:

              executed twice

              I did the test couple of days ago and the code in my js file is executed only once.
              How do you import the file ?

              P Offline
              P Offline
              patrickkidd
              wrote on last edited by
              #6

              @LeLev

              @LeLev said in Qml doesn't allow javascript singletons?:

              sorry i forgot to answer
              @patrickkidd said in Qml doesn't allow javascript singletons?:

              executed twice

              I did the test couple of days ago and the code in my js file is executed only once.
              How do you import the file ?

              I import the file from four different qml files, all do this:

              import 'js/Global.js' as Global

              https://alaskafamilysystems.com/

              ODБOïO 1 Reply Last reply
              0
              • P patrickkidd

                @LeLev

                @LeLev said in Qml doesn't allow javascript singletons?:

                sorry i forgot to answer
                @patrickkidd said in Qml doesn't allow javascript singletons?:

                executed twice

                I did the test couple of days ago and the code in my js file is executed only once.
                How do you import the file ?

                I import the file from four different qml files, all do this:

                import 'js/Global.js' as Global

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #7

                @patrickkidd can you make a simple project that replicates this behavior ?

                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