Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString loss data with non-ascii characters
QtWS25 Last Chance

QString loss data with non-ascii characters

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kokeroulis
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kokeroulis
        wrote on last edited by
        #3

        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 :(

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

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

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              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.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kokeroulis
                      wrote on last edited by
                      #10

                      Hello

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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SherifOmran
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tobias.hunger
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved