QHash non-ANSI key problem
Hello! I've found a strange behavior of a hash lookup with a non-ANSI key. When I use Latin keys, hash works fine - all values are returned by the QHash.value function. But when I'm trying to use a Russian keys (in the QString unicode, of course), sometimes hash does not return a value. And vice versa - sometime I can't get a key but the value - even if key or value if a one-character strings.
Also I have to notice that all keys and values are presented in the hash table, as I can check it with keys() and values() functions.
If QHash table have such problems, what you can suggest me to use for key/value handling with a full Unicode support in keys and values?
Re: QHash non-ANSI key problem
I don't think it's a qhash - problem but something on your side. Plz post some code to show us the problem.
Re: QHash non-ANSI key problem
OK, here is all code that related to my problem. It's a part of my Morse code converter.
Code:
//qstring_load - just a utility function for text file loading into QString
{
return s;
in.setCodec ("UTF-8");
s = in.readAll();
return s;
}
//hash_load_keyval - loads a key/value pairs table into the hash:
QHash<QString, QString> hash_load_keyval
(const QString &fname
){
QHash<QString, QString> result;
if (! file_exists (fname))
return result;
int c = l.size();
for (int i = 0; i < c; i++)
{
if (sl.size() == 2)
result.insert (sl[0], sl[1]);
}
return result;
}
//morse_from_lang - converts QString s into the Morse code using the key/value file that embeded into the resource. The QString lang is a locale id, for example "en" or "ru".
Please note that all data from the file loads OK.
{
QHash<QString, QString> h = hash_load_keyval (":/text-data/morse-" + lang);
int c = s.size();
for (int i = 0; i < c; i++)
{
QString t
= h.
value (QString (s
[i
]));
//here can be an empty value if key is non-Latin, and I don't know why if (! t.isNull() || ! t.isEmpty())
result.append (t + " ");
}
return result;
}
Re: QHash non-ANSI key problem
First: Please use the '[[code]]' - tags to make your code more readable
I would suggest adding some debug output to see what's going wrong. Maybe the utf-8 codec is not correct or something like this.
Add a debug output to see which keys you really put into the hash. Also use QHash<QChar,QString> because you only test for a char, not a string.
And then put a debug output into your other loop to see what s[i] actually is.
The debug-output should output the unicode numbers:
Code:
qDebug() << "Adding " << c << " (= " << c.unicode() << ") to the hash";
Re: QHash non-ANSI key problem
Thanks, all works fine now. It was my fault - I've copied a Morse table data from Wikipedia, and there are was some weird stuff with Russian letters (Latin "A" instead on a Russian "A", etc).