Results 1 to 7 of 7

Thread: QHash Clear

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QHash Clear

    When I use QHash clear(), does this delete all children (and children of children in nested containers)?

    Also, I have some QHash's (and other QT containers) which point to my own typedef structs. Will clear() delete those for me too?

    I couldn't tell from reading the documentation.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash Clear

    clear() will not call operator delete on pointers stored in the QHash, if you ask about it. You need to release the memory allocated with operator new yourself.
    If the structs are kept by value, then memory will be freed.

  3. #3
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QHash Clear

    Also, you can make use of the convenient qDeleteAll(), like:

    Qt Code:
    1. qDeleteAll( m_hashMap ) ;
    To copy to clipboard, switch view to plain text mode 
    This will free memory for values stored in the container.
    Note that this will not call clear() on the container.

  4. #4
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash Clear

    ok, I see, if I have values stored by reference, it will just clear the pointers, but not free the memory of the objects they are pointing to.

    Does this also apply to Qt containers? if I have nested Qt Containers, will it at least call clear in the nested container?

    for example, if I have something like:
    QHash<int, QList<int,float> testhash

    will clear() call clear() for each QList "value"?
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash Clear

    Qt Code:
    1. QHash<int, QList<int,float>
    To copy to clipboard, switch view to plain text mode 
    I don't really know how to interpret that, did you mean:
    Qt Code:
    1. QHash<int, QList< QPair<int,float> > > hash;
    To copy to clipboard, switch view to plain text mode 
    ?
    Anyway, with stack-allocated objects, hash.clear(); is enough, it will remove all the associated data from the memory.

  6. #6
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash Clear

    oh sorry, I meant

    QHash< int, QList<float> > hash;

    but I think you answered my question, if I do hash.clear() all the QLists will be deleted.

    But If I have something like

    Qt Code:
    1. typedef struct customtype{
    2. QList<float> list;
    3. };
    4.  
    5. QHash< int, customtype > hash;
    To copy to clipboard, switch view to plain text mode 
    hash.clear() will not delete the structs
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash Clear

    hash.clear() will not delete the structs
    Of course it will, why do you think so ?
    Try this code and see the output:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3.  
    4. struct customtype{
    5. QList<float> list;
    6. ~customtype(){
    7. qDebug() << "deleting customtype..." << list.count();
    8. }
    9. };
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QHash< int, customtype > hash;
    14. hash[1].list.append(2);
    15. hash[1].list.append(3);
    16. hash[2].list.append(1);
    17. qDebug() << "before clear()";
    18. hash.clear();
    19. qDebug() << "after clear()";
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 12th October 2011 at 22:06. Reason: code update

Similar Threads

  1. QList < QHash < int, QString> > ... clear() is slow
    By JoZCaVaLLo in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2011, 11:07
  2. Replies: 1
    Last Post: 10th February 2009, 09:42
  3. QHash
    By coderbob in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2008, 14:29
  4. QHash with 2 keys?
    By MrGarbage in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2007, 01:09
  5. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.