Search code examples
qtqmlpyside6qt6

Settings file not created with Settings QML type from QtCore module in Qt 6.5.0


I am trying to use the Settings QML type from QtCore in Qt 6.5.0 (not the one from Qt.labs.settings). The settings seem to be persistent as the size of the window is remembered, however, the "settings.ini" file is never created. According to the documentation (accessible via the link above), the location property does this:

This property holds the path to the settings file. If the file doesn't already exist, it will be created.

If this property is empty (the default), then QSettings::defaultFormat() will be used. Otherwise, QSettings::IniFormat will be used.

I have tried both relative and absolute path for the location property.

Here is a minimal reproducible example:

main.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtCore

ApplicationWindow {
    id: window
    visible: true

    width: 800
    height: 600

    Settings {
        location: "settings.ini"
        property alias x: window.x
        property alias y: window.y
        property alias width: window.width
        property alias height: window.height
    }
}

main.py

import sys
from pathlib import Path

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    qml_file = Path(__file__).resolve().parent / "main.qml"
    engine.load(qml_file)
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

Update: If I add the following to the main.py file, the settings.ini file is created just fine, so it shouldn't be an issue with write access.

from Pyside6.QtCore import QSettings
settings = QSettings("settings.ini", QSettings.IniFormat)
settings.setValue("test", 1)

Update 2: Both main.py and main.qml are in the same directory. I think there is some issue with the way the path is defined because as mentioned in the comments, it should be an url type. This is because the settings are remembered, just not in the format and location I want.


Solution

  • I have fixed this by using the following paths:

    relative: "file:settings.ini"

    absolute: "file:///absolute path here/settings.ini"