Important: Please read the 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)
        loader = QUiLoader()
        self.window = loader.load(ui_file)
        self.window.findChild(QTableWidget, 'beginningdiameterinput').setColumnCount(self.numVes)
        saveBtn = self.window.findChild(QPushButton, '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):

    def savebutton(self):
    for i in range(self.numVes):
    if self.window.findChild(QTableWidget, 'beginningdiameterinput').itemAt(0, i) is None:
    self.settings.setValue(self.namesettings[i], 400)#self.namesettings[i], random.randint(48, 53))
    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


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