0

I have found a very nice solution here on this site in order to store gui settings in pyqt5: Python PyQt4 functions to save and restore UI widget values?

The solution is saved in the function guisave.

Now I'm trying to implement this to my code. The idea is to close my gui with the exitAction button. This fires the closeApp function which fires the guisave function. The guisave function should save now all my pyqt objects.
The problem is that this does not happen. I'm not sure how I need to assign the ui variable in the guisave function. As you can see I tried to assign the mainwindow class. But this does not work. I'm also not sure if this works at all or if I need to scan all functions separately since the QEditLine are in the tab2UI function.

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import inspect

class mainwindow(QMainWindow):
    def __init__(self, parent = None):
        super(mainwindow, self).__init__(parent)

        self.initUI()


    def initUI(self):
        exitAction = QAction(QIcon('icon\\exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)     
        exitAction.triggered.connect(self.closeApp)     

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.setMovable(False)
        self.toolbar.addAction(exitAction)

        self.tab_widget = QTabWidget(self)     # add tab

        self.tab2 = QWidget()
        self.tab_widget.addTab(self.tab2, "Tab_2") 
        self.tab2UI()

        self.setCentralWidget(self.tab_widget)


    def tab2UI(self):
        self.layout = QFormLayout()
        self.layout.addRow("Name",QLineEdit())
        self.layout.addRow("Address",QLineEdit())
        self.tab2.setLayout(self.layout)


    def closeApp(self):
        guisave()


def guisave():
    ui = mainwindow
    settings = QSettings('gui.ini', QSettings.IniFormat)

    for name, obj in inspect.getmembers(ui): 
        if isinstance(obj, QComboBox):
            name = obj.objectName()  # get combobox name
            index = obj.currentIndex()  # get current index from combobox
            text = obj.itemText(index)  # get the text for current index
            settings.setValue(name, text)  # save combobox selection to registry

        if isinstance(obj, QLineEdit):
            print obj.objectName()
            name = obj.objectName()
            value = obj.text()
            settings.setValue(name, value)  # save ui values, so they can be restored next time
            print name, value

        if isinstance(obj, QCheckBox):
            name = obj.objectName()
            state = obj.isChecked()
            settings.setValue(name, state)

        if isinstance(obj, QRadioButton):
            name = obj.objectName()
            value = obj.isChecked()  # get stored value from registry
            settings.setValue(name, value)


def main():
    app = QApplication(sys.argv)
    ex = mainwindow()
    ex.setGeometry(100,100,1000,600)
    ex.show()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Stephan
  • 242
  • 4
  • 16

1 Answers1

-1

The solution that I propose is responsible for saving the states of the QWidgets, but for this you must put a name through the property objectName:

objectName : QString

This property holds the name of this object. You can find an object by name (and type) using findChild(). You can find a set of objects with findChildren().

Access functions:

QString objectName() const

void setObjectName(const QString &name)

So you should put a name, for example:

self.tab_widget.setObjectName("tabWidget")

We can use the QApplication :: allWidgets () function to get all the widgets of the application, and then we get the properties and save them, the process of restoring is the reverse of the previous one.

def restore(settings):
    finfo = QFileInfo(settings.fileName())

    if finfo.exists() and finfo.isFile():
        for w in qApp.allWidgets():
            mo = w.metaObject()
            if w.objectName() != "":
                for i in range(mo.propertyCount()):
                    name = mo.property(i).name()
                    val = settings.value("{}/{}".format(w.objectName(), name), w.property(name))
                    w.setProperty(name, val)


def save(settings):
    for w in qApp.allWidgets():
        mo = w.metaObject()
        if w.objectName() != "":
            for i in range(mo.propertyCount()):
                name = mo.property(i).name()
                settings.setValue("{}/{}".format(w.objectName(), name), w.property(name))

The complete example is found here

Community
  • 1
  • 1
eyllanesc
  • 235,170
  • 19
  • 170
  • 241