Save files in settings folder
-
Settings { category: "General" property alias x: root.x property alias y: root.y }
Settings saves
~/.config/<OrganizationName>/<ApplicationName>.conf
.How can I specify that path when using a function?
Not manually but using something that works for all apps. -
@realroot you could do something like this:
import Qt.labs.platform import Qt.labs.settings
Settings { id: settings category: "General" // put it outside of settings to not store the configLocation as setting entry property string configLocation: StandardPaths.writableLocation( StandardPaths.ConfigLocation) fileName: configLocation.replace("file://", "") + "/myOrganisation/myApp.conf" }
Note:
You can also pass the fileName as context property from main.cpp, or get it from any other C++ class.Settings { id: settings category: "General" fileName: myContextPropertySettingsLocation // fileName: backendClass.getSettingsLocation() }
-
@lemons thanks.
Instead of specyifing manually theOrganizationName
andApplicationName
:fileName: configLocation.replace("file://", "") + "/myOrganization/myApp.cfg"
If I want to save a file in the same folder can I specify it with some built-in var?
For example:fileName: configLocation.replace("file://", "") + <OrganizationName> + "/" + <ApplicationName> + ".customFile"
I can make some
property string
I ask in case there is something. -
As mentioned in my previous answer, you can pass the information from anywhere to QML. You have to find the best solution for your application.
e.g. Define them in your .pro file, and pass them all the way to QML:
// .pro DEFINES += \ ORGANIZATION_NAME="MyOrganizationName" \ APP_NAME="MyAppName"
// main.cpp QApplication app(argc, argv); QString orgName = QT_STRINGIFY(ORGANIZATION_NAME); QString appName = QT_STRINGIFY(APP_NAME); QString confLoc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); QQmlApplicationEngine engine; // I prefer uppercase context properties, but this is not required engine.rootContext()->setContextProperty("ORGNAME", orgName); engine.rootContext()->setContextProperty("APPNAME", appName); engine.rootContext()->setContextProperty("CONFLOC", confLoc); // ...
// .qml Settings { id: settings category: "General" fileName: CONFLOC + "/" + ORGNAME + "/" + APPNAME + ".conf" Component.onCompleted: { console.debug("Config base path:", CONFLOC) console.debug("Config file path:", settings.fileName) } } // you can use the context properties anywhere Text { anchors.centerIn: parent text: ORGNAME + ":" + APPNAME }