Skip to content

PyQt6 - QSettings

Python3
  • In meinem Restic UI nutze ich PyQt6 und das wollte ich jetzt mal mit PyQt6 ausprobieren. Hatte gedacht, das wäre schnell erledigt -Nein ist es nicht 😉

    PyQt5

    # set path for user directory
    PATH = (USERHOME + '/' '.restic_ui' + '/' + FILE)
    
    settings = QSettings(PATH, QSettings.IniFormat)
    

    PyQt6

    Das wirft aber in PyQt6 nur einen Traceback

    Traceback (most recent call last):
      File "/home/frank/qsettings/test.py", line 5, in <module>
        settings = QSettings(PATH, QSettings.IniFormat)
    AttributeError: type object 'QSettings' has no attribute 'IniFormat'
    

    Jo, was machen? Viel findet man nicht im Netz, also ausprobieren.

    from PyQt6.QtCore import QSettings
    
    # Objekt erzeugen
    settings = QSettings("Frank Mankel", "Restic UI")
    
    # Pfad ausgeben
    print("Settings file located at:", settings.fileName())
    
    # Test Variablen
    name = 'TEST'
    value = 100
    
    # Zwei Testwerte setzen
    settings.setValue(name, value)
    settings.setValue("editor/wrapMargin", 68)
    
    # Zwei Testwerte ausgeben
    print(settings.value('TEST'))
    print(settings.value('editor/wrapMargin'))
    

    Ausgabe

    (qsettings-kBmIZW-V) frank@frank-MS-7C37:~/qsettings$ /home/frank/.local/share/virtualenvs/qsettings-kBmIZW-V/bin/python /home/frank/qsettings/test.py
    Settings file located at: /home/frank/.config/Frank Mankel/Restic UI.conf
    100
    68
    

    Inhalt der Datei

    [General]
    TEST=100
    
    [editor]
    wrapMargin=68
    

    Fazit

    Problem gelöst, ich weiß jetzt wie es geht und kann mich jetzt dran setzen, das in meinem PyQt6 Projekt umzubauen, wenn ich mal Lust habe.... Das wird bestimmt nicht die letzte Hürde sein...

    Quellen

  • Falls das mal jemand braucht

    settings = QSettings(QSettings.Format(0),QSettings.Scope(0),"Frank_Mankel", "Restic_UI")
    
  • Möglichkeit 1

    Das hier

    settings = QSettings("Frank_Mankel", "Restic_UI")
    

    erzeugt unter

    /home/frank/.config/Frank_Mankel/Restic_UI.conf
    

    die Konfigurationsdatei.

    Möglichkeit 2

    Das hier

    settings = QSettings("Restic_UI")
    

    erzeugt unter

    /home/frank/.config/Restic_UI.conf
    

    die Konfigurationsdatei.

    Damit habe ich die grundlegenden Dinge, hoffentlich 😉 , verstanden.

    Für den Rest mal in die Dokumentation schauen.

    Sehr hilfreich war auch mal ein Blick auf die Definition der Klasse. Rechtsklick auf QSettings und dann Go to Definition

    class QSettings(QObject):
    
        class Scope(enum.Enum):
            UserScope = ... # type: QSettings.Scope
            SystemScope = ... # type: QSettings.Scope
    
        class Format(enum.Enum):
            NativeFormat = ... # type: QSettings.Format
            IniFormat = ... # type: QSettings.Format
            InvalidFormat = ... # type: QSettings.Format
    
        class Status(enum.Enum):
            NoError = ... # type: QSettings.Status
            AccessError = ... # type: QSettings.Status
            FormatError = ... # type: QSettings.Status
    [..gekürzt..]
    
  • FrankMF FrankM hat am auf dieses Thema verwiesen