I have a QGridLayout that I populate with some QLabels and QLineEdits at startup. The first row of my QGridlayout is the title of the columns. I use this function.
def add_row(self, idx, name, units, is_read_only=False):
row = idx + 1
# Add name
text = QLabel(name + ':', self)
text.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
col = 0
self.gridLayout.addWidget(text, row, col)
# Add read values
col = 1
self.read_values[name] = QLineEdit("", self)
self.read_values[name].setMinimumHeight(20)
#self.read_values[name].setMaximumWidth(260)
self.read_values[name].setMinimumWidth(150)
self.read_values[name].setReadOnly(True)
self.gridLayout.addWidget(self.read_values[name], row, col)
and it works well.
At some point, I have to delete the content of the QGridLayout.
I then do the following to delete the widgets and remove then from layout while keeping the first row where my headers (col titles).
def clear_grid_layout(self):
for i in reversed(range(self.gridLayout.count())):
row, col, d,e = self.gridLayout.getItemPosition(i)
if row == 0:
continue
widgetToRemove = self.gridLayout.itemAt(i).widget()
self.gridLayout.removeWidget(widgetToRemove)
widgetToRemove.deleteLater()
Then I need to add some widgets back. They should all align just as in the first try (before delete). However, when trying to add widgets back I reuse the same function above with the correct arguments and it fails.
At this point I expect the widgets to appear in the gridlayout, but they are created OUTSIDE the actual software.
Other usefull info: When I check, the QLabel object text.parent() returns None. Even when I try to set text.setParent(self), it keeps returning None.
I am answering my own question. I forgot the "Do not try to change the GUI in nothing but the MainThread". Just before going to lunch I realized that I had forgotten about this (again!). I added a simple signal/slot connection to jump threads and..... it worked.
self.s_set_names.connect(self.set_names)