I have my code:
for(QHash<long,float>::iterator i=list.begin();i!=list.end();++i)
{
long id = QVariant((*i).key()).toLongLong();
float ile = QVariant((*i).value()).toFloat();
}
and i become this error:
request for member 'key' in 'i.QHash<K,V>::iterator::operator*<long int, float>()', which is of non-class type 'float'
long id = QVariant((*i).key()).toLongLong();
How to convert key() and value() to my float and int?
The member methods key and value are part of the iterator's interface.
Because of that, it should be enough to use i.key() instead of (*i).key().
The same applies for values, for which you should use i.value() instead of (*i).value().
The operator* of the iterator returns a reference to the i-th value, in your case to a float that has not the member methods key and value indeed.
The assignments should resemble the following ones:
long id = i.key();
float ile = i.value();
See here for further details.
key() and value() are methods of the iterator, so you access them like this:
long id = QVariant(i.key()).toLongLong();
float ile = QVariant(i.value()).toFloat();
*i is same as i.value(), so you could also write QVariant(*i) for the value.
You get the error you get, because the line of the error is same as this: long id = QVariant(i.value().key()).toLongLong();, and obviously the float value is a non-class as said in the error, so it can't have key() method (or any other members).
Finally, going through the QVariant is totally unnecessary anyway (at least in this case), so you can just do this:
long id = i.key();
float ile = *i;
Doc reference: http://doc.qt.io/qt-5/qhash-iterator.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With