Domanda

I want to get a random pair with number n from my QHash.

Now I do it this way:

QHash<QString,QString>::iterator iterator = dictionary.begin();
iterator+= n;
question->setText(iterator.key());

But this seems just ridiculous... There must be a normal way. Can you help me please? I've read the whole entire man-page for QHash already

È stato utile?

Soluzione

QHash doesn't offer random selection. If you have to perform this operation often, then copy (pointers to) the keys() of the hash table into a vector or QVector, get a random index into that and use the key to look up the value in the QHash.

Depending on what else you use the QHash for, you might want to convert it to a vector of pairs at some point and just use that for random selection.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top