Hello; I wonder if anyone can help with this...
In my GUI I have a fairly complicated matrix of widgets in a QGridLayout. From time to time, I need to add or remove rows of widgets from the GUI.
I store pointers to each widget in a set of QLists, one for each type of widget. Adding widgets is no problem. I simply use:
// First, remove from the layout all the widgets in rows below the one to be inserted
...
// Then, add a new widget in row 'i'
MyWidgetList.insert(i, new MyCustomWidget(i));
// Then, repopulate the layout from row 'i' downwards
...
// First, remove from the layout all the widgets in rows below the one to be inserted
...
// Then, add a new widget in row 'i'
MyWidgetList.insert(i, new MyCustomWidget(i));
// Then, repopulate the layout from row 'i' downwards
...
To copy to clipboard, switch view to plain text mode
where MyWidgetList is a QList<MyCustomWidget *>. In effect, there are three distinct operations here: firstly, create a new widget; secondly, insert its pointer into the QList; thirdly, add the new widget to the layout.
However, I get into trouble when trying to remove a widget. In principle, I'd like to be able to do the above in reverse. Taking the widget out of the layout is easy, but I'm getting tied in knots over how to both remove the pointer from the QList AND destroy the widget itself. Currently, I use:
// First, remove the widget from the layout:
MyLayout -> removeWidget(MyWidgetList.at(i));
// Now call the destructor:
MyWidgetList.at(i) -> ~MyCustomWidget();
// Now delete the pointer:
MyWidgetList.removeAt(i);
// Finally, refresh the layout once again...
// First, remove the widget from the layout:
MyLayout -> removeWidget(MyWidgetList.at(i));
// Now call the destructor:
MyWidgetList.at(i) -> ~MyCustomWidget();
// Now delete the pointer:
MyWidgetList.removeAt(i);
// Finally, refresh the layout once again...
To copy to clipboard, switch view to plain text mode
When compiled with gcc, this seems to work, but then I seem to get random crashes after I've done this a couple of times, implying a memory leak. When compiled with MSVC, the program stops with an exception from deep within the heap management routines.
There must be a 'proper' way to do this. The QList documenation provides the following example for clearing out all the widgets from a QList, namely:
qDeleteAll(list.begin(), list.end());
list.clear();
qDeleteAll(list.begin(), list.end());
list.clear();
To copy to clipboard, switch view to plain text mode
...but there are no clues how one might go about clearing out a single entry.
Can anyone advise?
Bookmarks