Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to save the state of qml file using c++
QtWS25 Last Chance

How to save the state of qml file using c++

Scheduled Pinned Locked Moved Unsolved Game Development
10 Posts 4 Posters 1.5k 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
    arlyn123
    wrote on 26 Jan 2024, 22:08 last edited by
    #1

    I am working on game and I want to save the game window using c++. I want make a method ofr that and in qml I will call that method. How can I save the state and geometry of that window?

    B 1 Reply Last reply 27 Jan 2024, 20:44
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 27 Jan 2024, 00:40 last edited by
      #2

      Is using C++ an important part of the goal?

      If not, Settings provides a way to read and write data backed by an OS dependent mechanism.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      A 1 Reply Last reply 27 Jan 2024, 13:49
      0
      • J jeremy_k
        27 Jan 2024, 00:40

        Is using C++ an important part of the goal?

        If not, Settings provides a way to read and write data backed by an OS dependent mechanism.

        A Offline
        A Offline
        arlyn123
        wrote on 27 Jan 2024, 13:49 last edited by
        #3

        @jeremy_k not really. I want to do it in a simple way

        1 Reply Last reply
        0
        • A arlyn123
          26 Jan 2024, 22:08

          I am working on game and I want to save the game window using c++. I want make a method ofr that and in qml I will call that method. How can I save the state and geometry of that window?

          B Offline
          B Offline
          Bondrusiek
          wrote on 27 Jan 2024, 20:44 last edited by
          #4

          @arlyn123 hi! You can also see on QSettings(https://doc.qt.io/qt-6/qsettings.html) if you use C++.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jeremy_k
            wrote on 27 Jan 2024, 21:58 last edited by jeremy_k
            #5
            import QtQuick.Window
            import QtQuick.Controls
            import QtCore
            
            Window {
                id: win
                visible: true
            
                Component.onCompleted: {
                    var geometry = settings.value("windowPosition");
                    if (geometry) {
                        var saved = JSON.parse(geometry);
                        win.x = saved.x;
                        win.y = saved.y;
                        win.width = saved.width;
                        win.height = saved.height;
                    }
                }
            
                Button {
                    anchors.centerIn: win.contentItem
                    text: "push to save"
                    onClicked: settings.setValue("windowPosition",
                        JSON.stringify({"x": win.x, "y": win.y, "width": win.width, "height": win.height }));
                }
            
                Settings {
                    id: settings
                    location: "file:///tmp/test"
                }
            }
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            A 1 Reply Last reply 28 Jan 2024, 19:57
            0
            • J jeremy_k
              27 Jan 2024, 21:58
              import QtQuick.Window
              import QtQuick.Controls
              import QtCore
              
              Window {
                  id: win
                  visible: true
              
                  Component.onCompleted: {
                      var geometry = settings.value("windowPosition");
                      if (geometry) {
                          var saved = JSON.parse(geometry);
                          win.x = saved.x;
                          win.y = saved.y;
                          win.width = saved.width;
                          win.height = saved.height;
                      }
                  }
              
                  Button {
                      anchors.centerIn: win.contentItem
                      text: "push to save"
                      onClicked: settings.setValue("windowPosition",
                          JSON.stringify({"x": win.x, "y": win.y, "width": win.width, "height": win.height }));
                  }
              
                  Settings {
                      id: settings
                      location: "file:///tmp/test"
                  }
              }
              
              A Offline
              A Offline
              arlyn123
              wrote on 28 Jan 2024, 19:57 last edited by arlyn123
              #6

              @jeremy_k I have two qml files. I want to save on them using a button onClick. How can I do that? The one in that I want to save the other window is connected my a main.qml that has a window and a loader.

              import QtQuick
              import QtQuick.Window
              import QtQuick.Controls 6.0
              
              Window {
                  width: 1920
                  height: 1080
                  visible: true
                  title: qsTr("Main Menu")
              
                  Loader {
                      id: mainLoader
                      anchors.fill: parent
                      source: "qrc:/UI/screens/HomeScreen.qml"
                  }
              }
              
              

              HomeScreen has an item and other elements inside it. As well as the button that I want to click. Is possible to have the second window using that button that it is in HomeScreen?

              J 1 Reply Last reply 29 Jan 2024, 01:27
              0
              • A arlyn123
                28 Jan 2024, 19:57

                @jeremy_k I have two qml files. I want to save on them using a button onClick. How can I do that? The one in that I want to save the other window is connected my a main.qml that has a window and a loader.

                import QtQuick
                import QtQuick.Window
                import QtQuick.Controls 6.0
                
                Window {
                    width: 1920
                    height: 1080
                    visible: true
                    title: qsTr("Main Menu")
                
                    Loader {
                        id: mainLoader
                        anchors.fill: parent
                        source: "qrc:/UI/screens/HomeScreen.qml"
                    }
                }
                
                

                HomeScreen has an item and other elements inside it. As well as the button that I want to click. Is possible to have the second window using that button that it is in HomeScreen?

                J Offline
                J Offline
                jeremy_k
                wrote on 29 Jan 2024, 01:27 last edited by
                #7

                @arlyn123 said in How to save the state of qml file using c++:

                HomeScreen has an item and other elements inside it. As well as the button that I want to click. Is possible to have the second window using that button that it is in HomeScreen?

                The clean way is to have HomeScreen emit a signal with the properties to be saved. If the properties are generic and externally visible, these can be skipped.

                In main.qml, use a Connections to connect to the signal and save the data. The Loader documentation gives an example of printing a message when signal is emitted from a loaded item.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                A 1 Reply Last reply 29 Jan 2024, 19:29
                0
                • J jeremy_k
                  29 Jan 2024, 01:27

                  @arlyn123 said in How to save the state of qml file using c++:

                  HomeScreen has an item and other elements inside it. As well as the button that I want to click. Is possible to have the second window using that button that it is in HomeScreen?

                  The clean way is to have HomeScreen emit a signal with the properties to be saved. If the properties are generic and externally visible, these can be skipped.

                  In main.qml, use a Connections to connect to the signal and save the data. The Loader documentation gives an example of printing a message when signal is emitted from a loaded item.

                  A Offline
                  A Offline
                  arlyn123
                  wrote on 29 Jan 2024, 19:29 last edited by
                  #8

                  @jeremy_k how can I use a connection to actually restore the window that I want to save and to make the loader to still open HomeScreen.qml? that's my biggest problem right now. I have created a signal and emited in the button. And now?

                  Button {
                          id: loadGame
                          x: 81
                          y: 284
                  
                          width: 154
                          height: 34
                          onClicked: {
                              loadSignal.emit()
                          }
                          
                          text: "Load Game"
                          background: Rectangle {
                              color: "transparent"
                          }
                  
                          contentItem: Text {
                              color: loadGame.hovered ? "#a9caf9" : "#ffffff"
                              text: "Load Game"
                              font.family: "Inria Serif"
                              font.pixelSize: 26
                              horizontalAlignment: Text.AlignLeft
                              verticalAlignment: Text.AlignTop
                              wrapMode: Text.Wrap
                              font.weight: Font.Normal
                          }
                      }
                      
                      signal loadSignal();
                  
                  
                  J 1 Reply Last reply 30 Jan 2024, 07:13
                  0
                  • A arlyn123
                    29 Jan 2024, 19:29

                    @jeremy_k how can I use a connection to actually restore the window that I want to save and to make the loader to still open HomeScreen.qml? that's my biggest problem right now. I have created a signal and emited in the button. And now?

                    Button {
                            id: loadGame
                            x: 81
                            y: 284
                    
                            width: 154
                            height: 34
                            onClicked: {
                                loadSignal.emit()
                            }
                            
                            text: "Load Game"
                            background: Rectangle {
                                color: "transparent"
                            }
                    
                            contentItem: Text {
                                color: loadGame.hovered ? "#a9caf9" : "#ffffff"
                                text: "Load Game"
                                font.family: "Inria Serif"
                                font.pixelSize: 26
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignTop
                                wrapMode: Text.Wrap
                                font.weight: Font.Normal
                            }
                        }
                        
                        signal loadSignal();
                    
                    
                    J Offline
                    J Offline
                    jeremy_k
                    wrote on 30 Jan 2024, 07:13 last edited by
                    #9

                    @arlyn123 said in How to save the state of qml file using c++:

                    @jeremy_k how can I use a connection to actually restore the window that I want to save and to make the loader to still open HomeScreen.qml?

                    You might find Loader.setSource(url source, object properties) useful to specify the component to load as well as its properties to restore.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BogDan Vatra
                      wrote on 12 May 2024, 06:49 last edited by
                      #10

                      @arlyn123 did you find any solution?

                      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