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

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



  • 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])))

  • Lifetime Qt Champion

    Hi,

    Looks like you should take a look at beginWriteArray.



  • This post is deleted!


  • @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.


  • Lifetime Qt Champion

    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.


  • Banned

    Okay this is not a MRE (Minimal Reproducible Example) so please provide one and I will take a look and see if I can get you headed in the right direction.



  • @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]))
    

  • Banned

    Okay that is a code snippet -- whether it is easier to read (which it might be) or not is immaterial because it is not an MRE

    To help you if you post a MRE all I will need to do to reproduce your issue is to copy/paste your code and run it -- that is how an MRE works -- and what it does is assures me that I am dealing with the fullness of your issue rather than trying to figure what you might have done wrong outside the code snippet you have given as sometimes the issue is not actually contained within the code snippet.

    Again look forward to seeing an MRE so I can see if I can help you or not with your issue


Log in to reply