QString loss data with non-ascii characters



  • Hello

    I am trying to store something inside a QString but the non-ascii characters are lost. I have use the QString::fromUtf8() but it didn't work... Here is an example

    @#include <QtCore>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    qDebug() << QString::fromUtf8("LØNN JOBB"); // it should return "LØNN JOBB" but it returns "LNN JOBB"
    return a.exec();
    }@

    Thanks in advance



  • And what is the encoding of your input file?

    Personally, I tend to avoid anything non-ascii in source files. If I need localized text, I use translations instead.



  • Hello

    Thank you for your reply. My application is requesting from the user to add a username, which username might be anything (letters,numbers and non-ascii characters), and i use a QString to store this name, so the solution with the translations doesn't work :(



  • It does. QString has no problem at all with UTF, and all Qt widgets return QString and accept UTF input just fine. The issue is with the code you posted in line 5, not with QStrings abilities. If you would use a QInputDialog to ask for a name, you would see that the string is correct.



  • If translation is not usable for you, ensure that the cpp file is store in utf-8, then it will work.



  • Use

    @#include <QtCore>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    qDebug() << QString::fromUtf8("L\u00d8NN JOBB");
    return a.exec();
    }@

    Avoiding non-ascii is best practice. A lot of compilers assume ascii, but try to work with other encodings, which can result in strange behavior.



  • We have year 2011 - switching all source code to UTF-8 is possible and desirable.

    Computers and programming languages shall ease our lives. Writing cryptic codes instead of Ø or ü or å is anarchistic. We have landed on the moon over 40 years ago but still shall not use native characters in IT - this outrages me.

    As Gerolf already stated, you must be sure that your source file is actually safed as UTF-8 file. From your result, it's very likely that it is some kind of ISO-8859-xx (aka Latin-xx) encoding.



  • Nice in theory, but hardly workable in practice, espcially if you have multiple people working on the project. You will have to make sure each and every one uses the right encoding when loading and saving the files, otherwise you'll be in big trouble. I agree that writing cryptic codes is probably not the way to go (unless you only need one or two across the project). That is why I suggest using Qt's excellent translation facilities for this stuff. Also helps if you ever decide you want to translate your application to another language, or you get a none-native developer on board of your team who can not read the text messages you put all over your code at that point in time.

    I think it is easier to go the translation route than using UTF directly in code. No QString::fromUtf8 needed, no setting codecs for source files, no hassles. The only thing you need to do is install the translation file (which you can compile in as a resource) and make sure you update the translation file before a release.



  • I strongly disagree here.

    Computers in particular, and machines in general, have to adapt to humans, not the other way round. Computer should make our lives (even as programmers :-) ) easier, not more complicated or add more work.

    I'm just plain tired to write "Goetz" instead of "Götz", or even worse using TeX umlauts like 'G"otz' or "construct" a name using octal encoding. This is silly. This is stupid. This is not intuitive. This is just annoying. We have landed on the moon and sent rovers to mars, but we still shall not use in 2011 what human culture provides us for some thousand years? This can't be meant serious!

    Ok, to become on-topic a bit:

    When talking aboute file encoding, one has to agree on one particular one in the team anyways (at least if different platforms like Windows/Mac OS X are in the game). It does not matter which one you choose from that point of view :-)

    And then there are teams where the translation solution is just not an option. A small team, for example, might have the ressources to add an additional translation step in the development process. Yes, there are applications for domestic markets out there :-)

    Additionally, the software developers might not speak (write) English fluently, the base language version would be crappy at best. .

    Oh, and once you've changed everything to UTF-8, it works like a charm. We've done this in our team some years ago, we never ran into any problems.



  • Hello

    Sorry for my late reply, thank you all of you for your help.



  • The answer is not clear to me, I would appreciate if somebody explain more to me. i have a similar issue, when I input the unicode manually in a string, it works fine, but when input automatically through the output from a previous function, which gives the same QString, it does not work.

    My Code is published in this thread
    "http://qt-project.org/forums/viewthread/20617/":http://qt-project.org/forums/viewthread/20617/

    Edit: I am not hijacking the thread, full respect to the owner, but i am linking similar issues together.


  • Moderators

    SherifOmran: Please do not try to hijack other peoples problems with yours. That is considered impolite.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.