Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use def load_settings() , def save_settings() and def closeEvent() with QTableView?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 197 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lcashe
    wrote on last edited by Lcashe
    #1

    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()
    JonBJ 1 Reply Last reply
    0
    • L Lcashe

      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()
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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?

      L 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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...! :)

          1 Reply Last reply
          0
          • JonBJ JonB

            @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?

            L Offline
            L Offline
            Lcashe
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved