Cannot delete dynamically created widget using removeWidget method
-
I have a grid
gridLayout_2
and I am dynamically adding QLineEdit widgets to this grid. See the example below:In my header file, I create a list of QLineEdit as shown below:
QList<QLineEdit*> cursor_list;
Then in my .c file I am appending the list and adding widget:
cursor_list.append(new QLineEdit()); ui->gridLayout_2->addWidget(cursor_list[cursor_counter],10,1);
I can see that the Widget is added to the position I want (Row 10, Column 1)
Later in my code, I want to delete the widget that I have recently added, I use the function:
ui->gridLayout_2->removeWidget(ui->gridLayout_2->itemAtPosition(10, 1)->widget());
So I have added a QLineEdit widget to position 10,1 and then later I am trying to remove the same widget. The function executes but the widget is not removed from my application. Perhaps someone could clarify what could be the reasons? What is the best way to debug/troubleshoot this?
-
@lukutis222 If you read https://doc.qt.io/qt-6/qlayout.html#removeWidget
"After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout or to explicitly hide it if necessary.".
So, removeWidget just removes it from the layout, it does NOT hide it or move it to some other position. This is your job. Either delete it, or hide it (if you still need the widget).