Results 1 to 3 of 3

Thread: QHash <int, class*>

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default QHash <int, class*>

    Hi,

    I started playing with QHash, but I cant make hashPersons function to enumerate all keys and value since its crashing and I don't know why, please help

    Qt Code:
    1. class Person
    2. {
    3.  
    4. public:
    5. Person( QString name, QString number );
    6.  
    7. QString m_name, m_number;
    8. };
    9.  
    10. Person::Person( QString name, QString number ) : m_name( name ), m_number( number ) {}
    11.  
    12. void hashPersons()
    13. {
    14.  
    15. QHash<int, Person*> hash;
    16. hash[12] = new Person( "Anders", "8447070" );
    17. hash[20] = new Person( "Micke", "7728433" );
    18.  
    19. QHashIterator<int, Person*> i(hash);
    20.  
    21. while (i.hasNext()) {
    22.  
    23. qDebug()<<i.key();
    24. Person *per = i.value();
    25.  
    26. qDebug()<< "Name: " << per->m_name << "Number: "<<per->m_number;
    27. i.next();
    28. }
    29. hash.clear();
    30. }
    To copy to clipboard, switch view to plain text mode 
    also if I call hash.clear() at the end do I need to delete the value first or clear() will take care of it ?

  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 <int, class*>

    You need to call i.next() before taking the value with i.value() (btw. its in the first example in QHashIterator documentation):
    Qt Code:
    1. while (i.hasNext()) {
    2. i.next();
    3. qDebug()<<i.key();
    4. Person *per = i.value();
    5. if( per ){ // better check before dereferencing a pointer
    6. qDebug()<< "Name: " << per->m_name << "Number: "<<per->m_number;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    also if I call hash.clear() at the end do I need to delete the value first or clear() will take care of it ?
    Yes you need to delete allocated values yourself, QHash does not know what kind of data you keep inside it.

  3. The following user says thank you to stampede for this useful post:

    ArkKup (19th December 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QHash <int, class*>

    hash.clear(): See qDeleteAll() for convenience.

  5. The following user says thank you to Lykurg for this useful post:

    ArkKup (19th December 2011)

Similar Threads

  1. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 06:30
  2. Replies: 1
    Last Post: 10th February 2009, 09:42
  3. How to store custom class in QHash?
    By Misenko in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 08:57
  4. QHash
    By coderbob in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2008, 14:29
  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
  •  
Qt is a trademark of The Qt Company.