-2

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.

PBareil
  • 157
  • 6
  • It is getting so hard to ask a question without being down voted... not all problems are simple and easy to describe. – PBareil Jan 03 '20 at 16:00

1 Answers1

-1

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)
PBareil
  • 157
  • 6
  • 2
    Can you better clarify what you did in the first place that misled you? You never mentioned you were using different threads in the question. Also, the code provided in this answer is completely without context, and it doesn't help to understand how you solved your issue in the first place. What signal is that, and how/why/when/by whom is it emitted? What that slot does? – musicamante Aug 07 '19 at 16:54