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:
QHash<QString, QHash<QString, int> >
QHash<QString, QHash<QString, int> >
To copy to clipboard, switch view to plain text mode
A brief description of a class:
class RCDelayedRpcCaller
: public QObject{
Q_OBJECT
public:
explicit RCDelayedRpcCaller
(renderclient
::RenderClient *rc,
QObject *parent
= 0);
(...)
private:
// el.name, el.attr, value
// | | |
QHash<QString, QHash<QString, int> > scalarHashTable;
(...)
//object that gets data from above data structure
renderclient::RenderClient *renderClient;
(...)
};
class RCDelayedRpcCaller : public QObject
{
Q_OBJECT
public:
explicit RCDelayedRpcCaller(renderclient::RenderClient *rc, QObject *parent = 0);
(...)
private:
// el.name, el.attr, value
// | | |
QHash<QString, QHash<QString, int> > scalarHashTable;
(...)
//object that gets data from above data structure
renderclient::RenderClient *renderClient;
(...)
};
To copy to clipboard, switch view to plain text mode
When I'm trying to implement that with QHashIterator, the program crashes:
QHashIterator<QString, QHash<QString, int> > i(scalarHashTable);
while (i.hasNext()){
qDebug() << i.key();// crashes here;
QHash<QString, int> attribute = i.value();
QHashIterator<QString, int> j(attribute);
while (j.hasNext()){
renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
}
}
QHashIterator<QString, QHash<QString, int> > i(scalarHashTable);
while (i.hasNext()){
qDebug() << i.key();// crashes here;
QHash<QString, int> attribute = i.value();
QHashIterator<QString, int> j(attribute);
while (j.hasNext()){
renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
}
}
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:
QHash<QString, QHash<QString, int> >::const_iterator i = scalarHashTable.constBegin();
while (i != scalarHashTable.constEnd()){
QHash<QString, int>::const_iterator j = i.value().constBegin();
while (j != i.value().constEnd()){
renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
++j;
}
++i;
}
QHash<QString, QHash<QString, int> >::const_iterator i = scalarHashTable.constBegin();
while (i != scalarHashTable.constEnd()){
QHash<QString, int>::const_iterator j = i.value().constBegin();
while (j != i.value().constEnd()){
renderClient->set_scalar_type_attribute(i.key(), j.key(), j.value());
++j;
}
++i;
}
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.
Bookmarks