How to save the state of qml file using c++
-
@arlyn123 hi! You can also see on QSettings(https://doc.qt.io/qt-6/qsettings.html) if you use C++.
-
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" } }
-
@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?
-
@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.
-
@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();
-
@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.
-
@arlyn123 did you find any solution?