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.
7 replies
On Qt 5, for the libraries itself, UTF-8 is the default encoding as of a couple of days1. It’s highly recommendable to switch to UTF-8 for your own sources too.
1 http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/
Nothing special need to do, it will works by default. If the exec-charset of your your compiler is UTF-8.
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).
On Qt 5, for the libraries itself, UTF-8 is the default encoding as of a couple of days1. It’s highly recommendable to switch to UTF-8 for your own sources too.1 http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/
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.
3. 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.
IMO, you need to provide more information.1. the SHA of Qt5’s source code
Qt git directory: 9985003ac4a42adfa35db286eda1b2ae9656d85b
qtbase: ac16d722140661cd21949ca321b659ba2c359388
2. The compiler you used.
- $ 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)
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.
In Qt4 in every single project I always add this:
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)?
3. Make sure abc is utf-8 encoded bytes.
char abc[]="ąśćółęńżź";
- 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).
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.
You must log in to post a reply. Not a member yet? Register here!



