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. Source code encoding under Qt5

Source code encoding under Qt5

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 17.0k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    matthewpl
    wrote on last edited by
    #1

    Hi,

    I am trying write (or more correctly: learn new Qt) a application with some Polish words (ąśćółęńżź, etc). In Qt4 I was using tr() and setCodecForTr() to make Polish words correctly visible, but now I cannot use setCodecForTr() anymore and setCodecForLocale() cannot fix my problem. What to do?

    My whole system is (I believe) in UTF-8.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Nothing special need to do, it will works by default. If the exec-charset of your your compiler is UTF-8.

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

        On Qt 5, for the libraries itself, UTF-8 is the default encoding as of a couple of days[1]. It's highly recommendable to switch to UTF-8 for your own sources too.

        fn1. http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          matthewpl
          wrote on last edited by
          #4

          [quote author="1+1=2" date="1338322461"]Nothing special need to do, it will works by default. If the exec-charset of your your compiler is UTF-8.[/quote]

          For some reasons it doesn't work. Polish letters are invisible for me. If I compile the source code under Qt 4.8 I will see "not-encoded" UTF-8 letters (2 weird chars instead of one Polish letter).

          [quote author="Volker" date="1338323417"]On Qt 5, for the libraries itself, UTF-8 is the default encoding as of a couple of days[1]. It's highly recommendable to switch to UTF-8 for your own sources too.

          fn1. http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/[/quote]

          My source code is in UTF-8 for many, many years so until Qt Creator 2.5 doesn't make any mistake here (but after compiling project under Qt 4.8 I believe Qt Creator works just fine) this source code is also in UTF-8.

          EDIT:
          What is weird, if I create label in Designer I see Polish words correctly but after changing text of label in source code Polish letters are invisible.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            IMO, you need to provide more information.

            1. the SHA of Qt5's source code
            2. The compiler you used. Don't told us you used something like
              @
              setCodecForTr("Something other that utf8")
              @

            in Qt4.

            If so, obvious your exec-charset is not UTF-8.

            1. Make sure abc is utf-8 encoded bytes.
              @
              char abc[]="ąśćółęńżź";
              @

            BTY, Note that, you should make sure that your exec-charset is utf-8. exec-charset may be different from input-charset.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              matthewpl
              wrote on last edited by
              #6

              [quote author="1+1=2" date="1338327856"]IMO, you need to provide more information.

              1. the SHA of Qt5's source code
                [/quote]

              Qt git directory: 9985003ac4a42adfa35db286eda1b2ae9656d85b
              qtbase: ac16d722140661cd21949ca321b659ba2c359388

              [quote author="1+1=2" date="1338327856"]2. The compiler you used.[/quote]

              @$ g++ -v
              Using built-in specs.
              COLLECT_GCC=g++-4.7
              COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
              Target: x86_64-linux-gnu
              Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.0-9' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
              Thread model: posix
              gcc version 4.7.0 (Debian 4.7.0-9)@

              [quote author="1+1=2" date="1338327856"]Don’t told us you used something like

              @setCodecForTr("Something other that utf8")@

              in Qt4.

              If so, obvious your exec-charset is not UTF-8.
              [/quote]

              In Qt4 in every single project I always add this:

              @QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));@

              But problem isn't with Qt4 (where I always saw all chars, but without this line just encoded incorrect) but with Qt5 where I don't see Polish letters when I wrote them in source code.

              But how check this exec-charset (as far as I know this property have UTF-8 as default value)?

              [quote author="1+1=2" date="1338327856"]
              3. Make sure abc is utf-8 encoded bytes.
              @
              char abc[]="ąśćółęńżź";
              @
              [/quote]

              @matthew@pingwinek:~/tmp$ cat main.cpp
              #include <iostream>

              using namespace std;

              int main()
              {
              char abc[]="ąśćółęńżź";

              cout << sizeof(abc) << endl;

              return 0;
              }

              matthew@pingwinek:~/tmp$ g++ main.cpp -o cpp
              matthew@pingwinek:~/tmp$ ./cpp
              19@

              So yeaaaa... my source code IS in UTF-8, I just doesn't see Polish letters in labels in Qt projects when I set label text in source code (in Designer everything is fine).

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #7

                You can write a simple example like this

                @
                #include <QApplication>
                #include <QLabel>

                #if _MSC_VER >= 1600
                #pragma execution_character_set("utf-8")
                #endif

                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);
                QLabel label("ąśćółęńżź");
                label.show();

                return a.exec&#40;&#41;;
                

                }
                @

                If other people can reproduce your problem, you can file a bug.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  matthewpl
                  wrote on last edited by
                  #8

                  I get something like this (window style: oxygen, app style: plastique because of @Old plugin format found in lib /usr/lib/kde4/plugins/styles/oxygen.so@):
                  http://dl.dropbox.com/u/680693/qt5label-screenshot.png

                  For me it looks like Latin1 or Windows CP-1251.

                  But maybe problem isn't in Qt itself but in Qt configuration? Maybe font is wrong (serifs? Why there are serifs when my whole system use verdana?)? I didn't set up anything, just use default.

                  EDIT: Interesting... even if I manually change font of label (in designer or in source code) it doesn't change anything in binary.

                  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