Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMap <int, Widget*> deletion

[SOLVED] QMap <int, Widget*> deletion

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JeroentjehomeJ Offline
    JeroentjehomeJ Offline
    Jeroentjehome
    wrote on last edited by
    #1

    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.

    Greetz, Jeroen

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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();
      

      @

      Greetz, Jeroen

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved