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. How are y'all making global variables in QML
Forum Updated to NodeBB v4.3 + New Features

How are y'all making global variables in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 176 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.
  • M Offline
    M Offline
    MaximBozek
    wrote 22 days ago last edited by MaximBozek 5 Dec 2025, 14:48
    #1

    Hi

    I want to use certain QML (property) variables in multiple QML files, but don't really know how to integrate this into my project. I have a C++ and QML project in Qt 6.3

    Anyone have a good workflow for this?

    1 Reply Last reply
    1
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote 22 days ago last edited by
      #2

      Usually, I keep read-only globals around in JavaScript file.
      Something like

      // myGlobalVariables.js
      var defaultUrl = "https://www.qt.io"
      

      ...and import it and use it, where needed

      // import whatever
      import "myGlobalVariables.js" as RootEnv
      ApplicationWindow {
          visible: true
          title: "Enter Url"
      
          TextEdit {
              anchors.centerIn: parent
              text: RootEnv.defaultUrl
          }
      }
      
      

      If globals should be writable, I usually use a singleton.

      Software Engineer
      The Qt Company, Oslo

      J 1 Reply Last reply 21 days ago
      3
      • A Axel Spoerl
        22 days ago

        Usually, I keep read-only globals around in JavaScript file.
        Something like

        // myGlobalVariables.js
        var defaultUrl = "https://www.qt.io"
        

        ...and import it and use it, where needed

        // import whatever
        import "myGlobalVariables.js" as RootEnv
        ApplicationWindow {
            visible: true
            title: "Enter Url"
        
            TextEdit {
                anchors.centerIn: parent
                text: RootEnv.defaultUrl
            }
        }
        
        

        If globals should be writable, I usually use a singleton.

        J Online
        J Online
        J.Hilk
        Moderators
        wrote 21 days ago last edited by
        #3

        @Axel-Spoerl Both options let my skin crawl!

        But I recently learned that using context property is significantly worse than these. Due too lookup times.

        "You can and you can't,
        You shall and you shan't,
        You will and you won't,
        And you will be damned if you do,
        And you will be damned if you don't."


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote 21 days ago last edited by
          #4

          Singleton is the way.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote 21 days ago last edited by
            #5

            I agree that Singleton is the way.
            But it really depends on the complexity of your app.
            When I have 2-3 qml files and need globals, I really don't want to keep a singleton around.

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote 20 days ago last edited by
              #6

              A QML singleton file is hardly more complicated and has the benefit of being typed and using a proper module import, making the tooling easy.

              // Globals.qml
              pragma singleton
              import QtQml
              
              QtObject {
                  readonly property string defaultUrl: "https://www.qt.io"
              }
              
              1 Reply Last reply
              2

              1/6

              12 May 2025, 14:35

              • Login

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