[SOLVED] QMap <int, Widget*> deletion
-
Hi there,
My question is very simple, but still I'm stuck in the working.
I created a custom QWidget derived class and add it with a identifier to a QMap to be displayed in a horizontal layout. This all works fine and when a new item is detected, I allocate a new widget and add this to the QMap. Now what do I have to do to remove the widget from the QMap again and release memory? I'm reading into QMap::iterator or QMap::const_iteraror, but what happens if I use this iterator and in the for loop from begin to end I remove the map items with take()? Will this corrupt the iterator? If so, how to handle this?
My code so far:
@
QMap <GU32, QWidget*>::iterator Iter;for (Iter = m_GuiElements_wMap.begin(); Iter != m_GuiElements_wMap.end(); ++Iter) { // Clear all Widgets delete m_GuiElements_wMap.take(Iter.key()); } m_GuiElements_wMap.clear();
@
Any help will be appreciated. -
Solved it, I think, If anyone has any doubt if I do it correct, please let me know!!
@
QMap <GU32, QWidget*>::iterator Iter;for (Iter = m_GuiElements_wMap.begin(); Iter != m_GuiElements_wMap.end(); ++Iter) { // Clear all Widgets delete m_GuiElements_wMap.value(Iter.key()); } m_GuiElements_wMap.clear();
@
-
You could use a simple foreach as well.
@
// simple foreach
foreach (GU32 object, map.keys())
map.value(object).deleteLater(); // delete will do the same thing...// clear the map
map.clear();
@Its really a matter of preference though.