PDA

View Full Version : Initializing a QList of QHashes



Cruz
24th November 2009, 13:32
Hello!

I want to use a data structure made from a QList that has QHashes as elements. Here is the declaration:

QList< QHash<QString, float> > data;

With this I get an empty list that I want to fill while reading a file line by line. Now I don't know how to get the QHashes into the QList. I don't want to preallocate them on the heap, because I don't know how many of them I need and because it would be more convenient, if I don't have to destroy them. The whole data structure should be automatically destroyed, when the QList goes out of scope. I'm probably just missing the right syntax here. Can anyone help?

Thanks
Cruz

spirit
24th November 2009, 16:15
did you try something like this?


void FileProcessor::processFile()
{
QList< QHash<QString, float> > data;
...
while (!file.atEnd()) {
...
data << processLine(file.readLine());
...
}
...
}

QHash<QString, float> FileProcessor::processLine(const QString &line)
{
QHash<QString, float> res;
//parse the line and fill the hash
return res;
}

Cruz
24th November 2009, 17:24
Yes it works, thanks!