Reviving this very old thread as I have a similar problem...
I have a QWidget that includes a QGridLayout Layout. This QGridLayout has on each row two QLineEdit's and one QPushButton, which are part of an Object of MyClass. The Layout has one header row at the top (row 0) and all Objects are stored in the ObjectList (a QList).
I have written the following code to remove a row regarding a specific Object.
// Deleting an Object
Layout->removeWidget(Object->LineEdit1());
Layout->removeWidget(Object->LineEdit2());
Layout->removeWidget(Object->PushButton());
ObjectList.removeOne(Object);
delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
// Deleting an Object
Layout->removeWidget(Object->LineEdit1());
Layout->removeWidget(Object->LineEdit2());
Layout->removeWidget(Object->PushButton());
ObjectList.removeOne(Object);
delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
To copy to clipboard, switch view to plain text mode
To add a row for a new Object, I have the following code.
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
To copy to clipboard, switch view to plain text mode
For some reason this code does not always work according to my expectation. In some situations the new Object does become visible and in other situations not, or at least no new row becomes visible (while the new Object is actually created). It seems that an existing row is basically overwritten with a row for the new Object.
Anyone a clue of what I am doing wrongly?
Edit: I may have clarified the symptom a bit: It seems that if not the last row in the QGridLayout deleted but any other row, a subsequent addition of a new row overwrites a row that remained to exist. How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?
Reviving this very old thread as I have a similar problem...
I have a QWidget that includes a QGridLayout Layout. This QGridLayout has on each row two QLineEdit's and one QPushButton, which are part of an Object of MyClass. The Layout has one header row at the top (row 0) and all Objects are stored in the ObjectList (a QList).
I have written the following code to remove a row regarding a specific Object.
// Deleting an Object
Layout->removeWidget(Object->LineEdit1());
Layout->removeWidget(Object->LineEdit2());
Layout->removeWidget(Object->PushButton());
ObjectList.removeOne(Object);
delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
// Deleting an Object
Layout->removeWidget(Object->LineEdit1());
Layout->removeWidget(Object->LineEdit2());
Layout->removeWidget(Object->PushButton());
ObjectList.removeOne(Object);
delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
To copy to clipboard, switch view to plain text mode
To add a row for a new Object, I have the following code.
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
To copy to clipboard, switch view to plain text mode
For some reason this code does not always work according to my expectation. In some situations the new Object does become visible and in other situations not, or at least no new row becomes visible (while the new Object is actually created). It seems that an existing row is basically overwritten with a row for the new Object.
Anyone a clue of what I am doing wrongly?
Edit: I may have clarified the symptom a bit: It seems that if not the last row in the QGridLayout deleted but any other row, a subsequent addition of a new row overwrites a row that remained to exist. How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?
Added after 1 45 minutes:
Ok, I found a solution, but I am not sure whether I like it... I changed the code to add an Object to:
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
int Count = Layout->rowCount();
Layout->addWidget(Object->LineEdit1(), Count, 0);
Layout->addWidget(Object->LineEdit2(), Count, 1);
Layout->addWidget(Object->PushButton(), Count, 2);
// Adding a new Object
MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
ObjectList.append(Object);
int Count = Layout->rowCount();
Layout->addWidget(Object->LineEdit1(), Count, 0);
Layout->addWidget(Object->LineEdit2(), Count, 1);
Layout->addWidget(Object->PushButton(), Count, 2);
To copy to clipboard, switch view to plain text mode
Is this really the way it should be done? I would expect that this creates a lot of empty rows / cells in the QGridLayout that only consume memory (although probably not much).
Bookmarks