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

  • 0 Stimmen
    1 Beiträge
    74 Aufrufe
    Niemand hat geantwortet
  • NiceGUI

    Linux
    2
    0 Stimmen
    2 Beiträge
    231 Aufrufe
    FrankMF

    Ich habe mir das jetzt einige Tage gegeben und habe mich dagegen entschieden. Werde weiterhin PyWebIO benutzen. Hauptgrund ist die wesentlich bessere Dokumentation -> https://pywebio.readthedocs.io/en/latest/index.html

    Da kann ich mir auch mit meinem Python Kenntnisstand die Informationen holen, die ich brauche um es einzusetzen.

  • Python - Formatumwandlung str -> float

    Python3
    1
    0 Stimmen
    1 Beiträge
    160 Aufrufe
    Niemand hat geantwortet
  • Python - Frameworks

    Python3
    2
    0 Stimmen
    2 Beiträge
    76 Aufrufe
    FrankMF

    Und mal hier parken

  • 0 Stimmen
    7 Beiträge
    148 Aufrufe
    FrankMF

    Link Preview Image Merge branch 'master' into 'main' (45c83cd8) · Commits · Frank / restic_ui_pywebio · GitLab

    Fix some bugs See merge request Bullet64/restic_ui_pywebio!71

    favicon

    GitLab (gitlab.com)

  • PyWebIO - put_buttons

    PyWebIO
    2
    0 Stimmen
    2 Beiträge
    146 Aufrufe
    FrankMF

    Und noch eine kleine Übung, wie man den Buttton abhängig von einem Value enabled/disabled

    # we build header and tdata for table tab_mount = [] for count, value in enumerate(backups): if count == 0: tab_mount.append(['No.', 'Backup name of the restic data backup', 'Actions']) if backups[value].init == "0": tab_mount.append([count + 1, backups[count].name, put_buttons([ dict(label='Mount', value='Mount', color='primary', disabled=True), dict(label='UMount', value='UMount', color='primary', disabled=True), dict(label='Restore', value='Restore', color='primary', disabled=True), ] , onclick=partial(actions, count + 1)) ]) else: tab_mount.append([count + 1, backups[count].name, put_buttons([ dict(label='Mount', value='Mount', color='primary'), dict(label='UMount', value='UMount', color='primary'), dict(label='Restore', value='Restore', color='primary'), ], onclick=partial(actions, count + 1)) ])
  • ResticUI with PyWebIO - Repo

    PyWebIO
    1
    0 Stimmen
    1 Beiträge
    83 Aufrufe
    Niemand hat geantwortet
  • Python3 - QSettings

    Python3
    1
    0 Stimmen
    1 Beiträge
    141 Aufrufe
    Niemand hat geantwortet