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 Votes
    3 Posts
    142 Views
    FrankMF

    Mein vorhandenes Projekt war doch etwas größer als ich gedacht hatte. Also musste ich mehr Zeit aufwenden um es nach Flask zu transferieren. Nach einiger Zeit hatte sich eine ganz ansehnliche Zahl von Dateien angesammelt und es kam wie es kommen musste, ich wusste manchmal nicht mehr, welches File ich anfassen musste. Chaos kam auf 🙂

    So fing ich an ein wenig zu recherchieren und kam auf die Funktion Blueprint von Flask. Mich ein wenig eingelesen, ChatGPT mal eben um ein Beispiel gebeten und dann angefangen die Applikation entsprechend umzubauen.

    Auch das hat Zeit gekostet. Aber, jetzt habe ich ein aufgeräumtes Projekt, was es mir wesentlich einfacher macht, daran zu arbeiten.

    5226e90d-aa20-4b20-93af-e50f8f841880-grafik.png

    Wenn ihr also mal vorhabt, so was zu coden, halte ich es für sinnvoll das Projekt von Anfang an zu strukturieren. Und zum Schluss noch ein Screenshot zum Stand des Projektes.

    a7981ffc-0f93-41f4-93e2-c914fdba5e43-grafik.png

    Und weiter geht es, ist nämlich noch nicht fertig 😀

  • 0 Votes
    1 Posts
    68 Views
    No one has replied
  • Python - Frameworks

    Python3
    2
    0 Votes
    2 Posts
    76 Views
    FrankMF

    Und mal hier parken

  • 0 Votes
    7 Posts
    148 Views
    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)

  • Example Class

    Pinned Python3
    3
    0 Votes
    3 Posts
    103 Views
    FrankMF

    In meinem PywebIO Projekt tauchte heute ein alter Bekannter auf. Wenn ich einen Eintrag (innerhalb der Liste) löschte, war das etwas durcheinander 🤔

    Ja, den Fehler kenne ich schon was länger und stolper immer mal wieder drüber. Heute z.B.

    Also mal grübeln 🤓

    Das mache ich, wenn ich einen Eintrag lösche

    case 'Yes': # delete entrie BackupList.delete((line - 1)) # save json BackupList.save_json() # Load data from filesystem BackupList.load_json() # Reload Tab Backup backup()

    Ich mache folgendes

    lösche den Eintrag speicher die Liste lade die Liste baue den Tab wieder auf

    Das führt aber dazu, das meine Liste die als Objekt im Speicher steht nicht aktuell ist. Im Gegenteil, da ist dann etwas Unordnung. Ich brauchte also die Möglichkeit mein Objekt backups irgendwie zurückzusetzen.

    Mal gegoogelt und die Dinge sind manchmal wirklich total easy 🙂

    # clear dict backups.clear()

    Das leert das Objekt und im nächsten Schritt kann ich es wieder befüllen.

    Lösung case 'Yes': # delete entrie BackupList.delete((line - 1)) # save json BackupList.save_json() # clear dict backups.clear() # Load data from filesystem BackupList.load_json() # Reload Tab Backup backup()

    Problem erledigt. Damit ich das noch finde, wenn mein Kopf das nicht mehr hergibt, notiere ich das hier.

  • 0 Votes
    1 Posts
    187 Views
    No one has replied
  • PyWebIO - put_buttons

    PyWebIO
    2
    0 Votes
    2 Posts
    146 Views
    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)) ])
  • 0 Votes
    1 Posts
    75 Views
    No one has replied