Search code examples
pythonpyqtpyqt5qcomboboxqsettings

Writing Qsettings of Qcombobox to a file without closing Widget


Having a Qtablewidget runs up as start widget and having Con widget imported to QTableWidget. From QCombobox wishs to save and restore to a file with path to a directory. Each widgets and layouts work, the issue is with QCombobox Saving function. Thinks to start and run app, when value of Qcombobox is changed then self.writeSettings fuction should run and write? but it does not work. Wonder what is it wrong here? Any help appreciate. I reproduce to minimal and productive code below.

class CON(QtWidgets.QWidget):
    def __init__(self, rowTable, parent=None):
        super(CON, self).__init__(parent)  
        self.rowtable = rowTable
        self.combo = QtWidgets.QComboBox()
        self.combo.addItems(["15","20","25","30","37","45","50","55",
                       "60","67","75","85","95","105"])

        ------------------------------------

       self.combo.activated.connect(self.setdata)
       self.readSettings()

    @QtCore.pyqtSlot(int)
    def setdata(self, index):
        self.writeSettings()


    def readSettings(self):
        settings = QtCore.QSettings('files/con{}.ini'.format(self.rowtable) ,QtCore.QSettings.IniFormat)
        settings.beginGroup("Con")
        self.combo.setCurrentIndex(settings.value("Conoption", 4))
        settings.endGroup()

    def writeSettings(self):
        settings = QtCore.QSettings('files/con{}.ini'.format(self.rowtable) ,QtCore.QSettings.IniFormat)
        settings.beginGroup("Con")
        settings.setValue("Conoption",self.combo.currentIndex())
        settings.endGroup()

Solution

  • The only error I get is the value read is interpreted as str, so to avoid this you must indicate the type of reading:

    settings.beginGroup("Con")
    self.combo.setCurrentIndex(settings.value("Conoption", 4, int))
    settings.endGroup()