Results 1 to 5 of 5

Thread: QHash non-ANSI key problem

  1. #1
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.
    Qt Code:
    1. //qstring_load - just a utility function for text file loading into QString
    2. QString qstring_load (const QString &fileName)
    3. {
    4. QFile file (fileName);
    5.  
    6.  
    7. if (! file.open(QFile::ReadOnly | QFile::Text))
    8. return s;
    9.  
    10. QTextStream in(&file);
    11. in.setCodec ("UTF-8");
    12.  
    13. s = in.readAll();
    14. return s;
    15. }
    16.  
    17. //hash_load_keyval - loads a key/value pairs table into the hash:
    18. QHash<QString, QString> hash_load_keyval (const QString &fname)
    19. {
    20. QHash<QString, QString> result;
    21. if (! file_exists (fname))
    22. return result;
    23.  
    24. QStringList l = qstring_load (fname).split ("\n");
    25.  
    26. int c = l.size();
    27. for (int i = 0; i < c; i++)
    28. {
    29. QStringList sl = l[i].split("=");
    30. if (sl.size() == 2)
    31. result.insert (sl[0], sl[1]);
    32. }
    33.  
    34. return result;
    35. }
    36.  
    37. //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".
    38. Please note that all data from the file loads OK.
    39.  
    40. QString morse_from_lang (const QString &s, const QString &lang)
    41. {
    42.  
    43. QHash<QString, QString> h = hash_load_keyval (":/text-data/morse-" + lang);
    44.  
    45. QString result;
    46.  
    47. int c = s.size();
    48. for (int i = 0; i < c; i++)
    49. {
    50. QString t = h.value (QString (s[i])); //here can be an empty value if key is non-Latin, and I don't know why
    51. if (! t.isNull() || ! t.isEmpty())
    52. result.append (t + " ");
    53. }
    54.  
    55. return result;
    56. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by roxton; 27th May 2008 at 15:38.

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. QChar c = sl[0][0];
    2. qDebug() << "Adding " << c << " (= " << c.unicode() << ") to the hash";
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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).

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.