Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Source code encoding under Qt5



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



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



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



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



  • [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).



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



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


Log in to reply