Results 1 to 3 of 3

Thread: QHashIterator doesn't work as expected (while QHash::const_iterator does)

  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry QHashIterator doesn't work as expected (while QHash::const_iterator does)

    Hello everyone,

    First: great forum, a lot of useful information. Thanks for all experienced users for support.

    I have some issues while trying to iterate through QHash data structure designed as:
    Qt Code:
    1. QHash<QString, QHash<QString, int> >
    To copy to clipboard, switch view to plain text mode 

    A brief description of a class:
    Qt Code:
    1. class RCDelayedRpcCaller : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit RCDelayedRpcCaller(renderclient::RenderClient *rc, QObject *parent = 0);
    6. (...)
    7. private:
    8. // el.name, el.attr, value
    9. // | | |
    10. QHash<QString, QHash<QString, int> > scalarHashTable;
    11. (...)
    12. //object that gets data from above data structure
    13. renderclient::RenderClient *renderClient;
    14. (...)
    15. };
    To copy to clipboard, switch view to plain text mode 

    When I'm trying to implement that with QHashIterator, the program crashes:
    Qt Code:
    1. QHashIterator<QString, QHash<QString, int> > i(scalarHashTable);
    2. while (i.hasNext()){
    3. qDebug() << i.key();// crashes here;
    4. QHash<QString, int> attribute = i.value();
    5. QHashIterator<QString, int> j(attribute);
    6. while (j.hasNext()){
    7. renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    In debuger it shows hashTable structures and it seems to be just fine. I've found that changing from iterator to following code works and does what's expected:
    Qt Code:
    1. QHash<QString, QHash<QString, int> >::const_iterator i = scalarHashTable.constBegin();
    2. while (i != scalarHashTable.constEnd()){
    3. QHash<QString, int>::const_iterator j = i.value().constBegin();
    4. while (j != i.value().constEnd()){
    5. renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
    6. ++j;
    7. }
    8. ++i;
    9. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? I'd like to use QHashIterator, as it looks better for my eyes but can't get it working.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHashIterator doesn't work as expected (while QHash::const_iterator does)

    The value is not available until you call next() on the Java-style iterator:
    Qt Code:
    1. QHashIterator<QString, QHash<QString, int> > i(scalarHashTable);
    2. while (i.hasNext()){
    3. (void) i.next(); // <<<<<< you have to step the Java-style iterator to get the value
    4. qDebug() << i.key();
    5. QHash<QString, int> attribute = i.value();
    6. QHashIterator<QString, int> j(attribute);
    7. while (j.hasNext()){
    8. (void) j.next(); // <<<<<< you have to step the Java-style iterator to get the value
    9. renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    I am discarding the next() return value, you might want to capture it.

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

    melon (6th August 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHashIterator doesn't work as expected (while QHash::const_iterator does)

    Works as expected, thank you!

    I assumed that hasNext() do the job, but that is just another "read the docs, Luke" case

Similar Threads

  1. QDataStream doesn't work as expected
    By mastupristi in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 07:08
  2. Replies: 9
    Last Post: 2nd December 2009, 18:59
  3. How does QString::qHash() work?
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2009, 09:17
  4. macdeployqt doesn't work at all.
    By pherthyl in forum Installation and Deployment
    Replies: 1
    Last Post: 10th July 2009, 00:42
  5. QHTTP::GET doesn't work as expected !
    By tuthmosis in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2008, 15:45

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.