Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Using QSettings() to save values of QTableWidgetItem and display values even after reopening a window
Forum Updated to NodeBB v4.3 + New Features

Using QSettings() to save values of QTableWidgetItem and display values even after reopening a window

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rahulr22
    wrote on last edited by
    #1

    Hi,
    Previously, I have used QSettings to save values in a QLineEdit into an integer variable and display the same values even after closing and then opening a window. However, when I try to save QTableWidgetItems in the same way, I always get empty QTableWidgetItems in my window and none of the values seem to save. As a side note, the QTableWidget columns are variable thus why I have for loops iterating through them.

    Here is my code regarding the saving process of the QTableWidget:

    def init(self, ui_file, parent=None):

        self.numVes, self.numBif = FormVesselMain('vesselwindowmain.ui').savebutton()
    
        super(FormBeginningDiameter, self).__init__(parent)
        ui_file = QFile(ui_file)
        ui_file.open(QFile.ReadOnly)
    
        loader = QUiLoader()
        self.window = loader.load(ui_file)
        ui_file.close()
    
        self.window.findChild(QTableWidget, 'beginningdiameterinput').setColumnCount(self.numVes)
    
        saveBtn = self.window.findChild(QPushButton, 'savebutton')
        saveBtn.clicked.connect(self.savebutton)
    
        for i in range(self.numVes):
            self.window.findChild(QTableWidget, 'beginningdiameterinput').setItem(0, i, QTableWidgetItem(self.settings.value('beginningdiameter')[i]))
    
        for i in range(self.numVes):
            self.namesettings.append("dia"+str(i))
    
        self.savebutton
    
        return
    

    def savebutton(self):
    for i in range(self.numVes):
    if self.window.findChild(QTableWidget, 'beginningdiameterinput').itemAt(0, i) is None:
    print(self.namesettings[i])
    self.settings.setValue(self.namesettings[i], 400)#self.namesettings[i], random.randint(48, 53))
    self.settings.value(self.namesettings[i])
    else:
    self.settings.setValue(self.namesettings[i], self.window.findChild(QTableWidget, 'beginningdiameterinput').itemAt(0, i).text())

        for i in range(self.numVes):
            self.window.findChild(QTableWidget, 'beginningdiameterinput').setItem(0, i, QTableWidgetItem(self.settings.value(self.namesettings[i])))
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Looks like you should take a look at beginWriteArray.

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

      R 1 Reply Last reply
      2
      • R Offline
        R Offline
        rahulr22
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Looks like you should take a look at beginWriteArray.

          R Offline
          R Offline
          rahulr22
          wrote on last edited by
          #4

          @SGaist Hi,
          I looked into beginWriteArray and the difference is I don't have multiple keys to input into an array. Even if I write in an array for an object for a QSettings value, I still am unable to access it later on when I close the window and reopen it. I am having more of an issue with QTableWidgetItem tracking QSettings, I believe.

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

            You don't need multiple keys to build an array.

            Note that your code is pretty convoluted to read.

            If you want to store the content of that table widget, you should refactor that code to clearly access the data of that QTableWidget rather than calling findChild all the time.

            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
            2
            • R Offline
              R Offline
              rahulr22
              wrote on last edited by
              #6

              @Denni-0 Sure. Please let me know if this is easier to read

                      self.window.findChild(QTableWidget, 'beginningdiameterinput').setColumnCount(self.numVes)
                      begDia = []
                      for i in range(self.numVes):
                          if self.window.findChild(QTableWidget, 'beginningdiameterinput').item(0, i) is None:
                              begDia.append(random.uniform(48, 53))
                          else:
                              begDia.append(int(self.window.findChild(QTableWidget, 'beginningdiameterinput').itemAt(0, i).text()))
                          self.settings.setValue("beginningdiameter", begDia)
                      return self.settings.value("beginningdiameter")
              
                      for i in range(self.numVes):
                          self.window.findChild(QTableWidget, 'beginningdiameterinput').setItem(0, i, QTableWidgetItem(self.settings.value('beginningdiameter')[i]))
              
              1 Reply Last reply
              0

              • Login

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