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 to read variable values between pages on different QML files?
QtWS25 Last Chance

How to read variable values between pages on different QML files?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 886 Views
  • 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
    arjun30
    wrote on last edited by
    #1

    In a QML project, I am using a StackView to switch between different pages. However, something inside one of the files, called Game.qml needs to know the value and when a variable is changed in the main.qml file. So far, I have been using a external JS file to store the variables "globally". However, the problem is I cannot use an automatic signal handler to find out when the value is changed in the main.qml file.
    Does anyone know any alternate solutions so I can change variables from two seperate QML files with ease? I am very new to Qt (sorry if this is a obvious question). Right now, my application is purely in qml with very little c++, but any solution (even a c++) would help.

    ODБOïO 1 Reply Last reply
    0
    • A arjun30

      In a QML project, I am using a StackView to switch between different pages. However, something inside one of the files, called Game.qml needs to know the value and when a variable is changed in the main.qml file. So far, I have been using a external JS file to store the variables "globally". However, the problem is I cannot use an automatic signal handler to find out when the value is changed in the main.qml file.
      Does anyone know any alternate solutions so I can change variables from two seperate QML files with ease? I am very new to Qt (sorry if this is a obvious question). Right now, my application is purely in qml with very little c++, but any solution (even a c++) would help.

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

      @arjun30 said in How to read variable values between pages on different QML files?:

      Game.qml needs to know the value and when a variable is changed in the main.qml

      Globally defined properties in main.qml are accessibles for all the qml components

      //main.qml
      ApplicationWindow {
          id:root
          property var test : 0
      
      //Game.qml
      Item {
          property var val: root.test
          onValChanged: console.log(val)
      
      

      but i believe this is not recommended approach.

      You can use QML Singleton

      You also can have a c++ class with a Q_PROPERTY, you set its value from main.qml, and access it where you need
      https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties

      A 1 Reply Last reply
      2
      • ODБOïO ODБOï

        @arjun30 said in How to read variable values between pages on different QML files?:

        Game.qml needs to know the value and when a variable is changed in the main.qml

        Globally defined properties in main.qml are accessibles for all the qml components

        //main.qml
        ApplicationWindow {
            id:root
            property var test : 0
        
        //Game.qml
        Item {
            property var val: root.test
            onValChanged: console.log(val)
        
        

        but i believe this is not recommended approach.

        You can use QML Singleton

        You also can have a c++ class with a Q_PROPERTY, you set its value from main.qml, and access it where you need
        https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties

        A Offline
        A Offline
        arjun30
        wrote on last edited by
        #3

        I used the Q_PROPERTY approach and it worked great! Thanks for all your help.

        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