Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use def load_settings() , def save_settings() and def closeEvent() with QTableView?



  • I asked here a problem that my QSettings doesn't work, means with next start of the programm doesn't load info. From SO found out that I did completely wrong and didn't call my settings anywhere. But after two days I still have no idea how to write it in my code even with documentation and other's code. Help please.

    
            self.qlineedit_name = QtWidgets.QLineEdit()
            self.qlineedit_name.resize(24, 80)
            self.qlineedit_points = QtWidgets.QLineEdit()
            self.qlineedit_points.resize(24, 80)
    
            horisontal_layout = QtWidgets.QHBoxLayout()
            horisontal_layout.addWidget(self.qlineedit_name, stretch=1)
            horisontal_layout.addWidget(self.qlineedit_points, stretch=1)
            horisontal_layout.addStretch(1)
            horisontal_layout.addWidget(update_button)          
            horisontal_layout.setAlignment(QtCore.Qt.AlignRight)
    
            grid_layout.addLayout(horisontal_layout, 0, 0)
            grid_layout.addWidget(self.table, 1, 0)
    
        def save_settings(self):
            settings = QtCore.QSettings("settings.ini", QtCore.QSettings.IniFormat)
            name = self.qlineedit_name.text().strip()
            points = self.qlineedit_points.text().strip()
    
            settings.setValue("name", name)
            settings.setValue("points", points) 
            settings.beginWriteArray("Params")
            index = 0
            
            for row in range(self.param_table.rowCount()):
                param = self.params_table.item(row, 0)
                value = self.params_table.item(row, 1)
                if param is not None and len(param.text()):
                    if value is not None and len(value.text()):
                        settings.setArrayIndex(index)
                        settings.setValue("param", self.params_table.item(row, 0).text())
                        settings.setValue("value", self.params_table.item(row, 1).text())
                        index += 1
            settings.endArray()
     
        def load_settings(self):
            settings = QtCore.QSettings("settings.ini", QtCore)
    
            self.qlineedit_name.setText(settings.value("name", ""))
            self.qlineedit_points.setText(settings.value("points", ""))
    
            for index in range(settings.beginReadArray("Params")):
                settings.setArrayIndex(index)
                param = settings.value("param")
                value = settings.value("value")
                self.params_table.insertRow(index)
                self.params_table.setItem(index, 0, QtWidgets.QTableWidgetItem(param))
                self.params_table.setItem(index, 1, QtWidgets.QTableWidgetItem(value))
            settings.endArray()
    
        def closeEvent(self, event: QtGui.QCloseEvent):
            self.save_settings()
            super().closeEvent(event)
    
        def on_update_button(self):
            name = self.qlineedit_name.text().strip()
            point = self.qlineedit_points.text().strip() if self.qlineedit_points.text().strip() else '0'
            if not point.isdigit():
                msg = QtWidgets.QMessageBox.information(self, 'ВНИМАНИЕ', 'Заполните правильно поле ввода Points!')
                return msg
    
            if not name:
                msg = QtWidgets.QMessageBox.information(self, 'ВНИМАНИЕ', 'Заполните поле ввода Name!')
                return msg
            rows  = self.table.model().rowCount()
            add_record = True
            for row in range(rows):
                if name == self.proxy.data(self.proxy.index(row, 0)):
                    #print(name)
                    add_record = False
                    row_edit = row
                    break
    
            if add_record:       
                if self.table.selectedIndexes():
                    row = self.table.selectedIndexes()[-1].row()
                    self.model.insertRow(row+1, [QtGui.QStandardItem(name), 
                                          QtGui.QStandardItem(point)])                  
    
                else:
                    self.model.appendRow([QtGui.QStandardItem(name), 
                                          QtGui.QStandardItem(point)])            
            else:                 
                self.model.setData(self.model.index(row_edit, 1), point, QtCore.Qt.EditRole)
            
            self.qlineedit_name.clear()
            self.qlineedit_points.clear()


  • @Lcashe
    There is an lot of code to read through here, most of which has nothing to do with saving/loading settings. It helps if a question is cut down to the essentials of whatever your question is.

    What exactly is your problem, the behaviour you see which is not correct? Glancing through your code, you set up in save_settings with

    settings = QtCore.QSettings("settings.ini", QtCore.QSettings.IniFormat)
    

    which looks right. Do the settings get correctly saved into the file if you look at it after exiting your program?

    But when you set up to load back in load_settings, instead of the same line you have

    settings = QtCore.QSettings("settings.ini", QtCore)
    

    Why would you write a different line? What do you expect that to do with the QtCore argument at the end? Does that work without erroring? Print out the resulting settings object, what do you get?


  • Lifetime Qt Champion

    Hi,

    Beside @JonB good point, you do not call load_settings anywhere.



  • @SGaist said in How to use def load_settings() , def save_settings() and def closeEvent() with QTableView?:

    Beside @JonB good point, you do not call load_settings anywhere.

    ...which might explain why the settings = QtCore.QSettings("settings.ini", QtCore) does not error, which it would if called...! :)



  • @JonB corrected. Tell me what to do, have no idea


  • Lifetime Qt Champion

    Well, fix the code in load_settings to match the one from write_settings and call that method.


Log in to reply