May 29, 2012

matthewpl matthewpl
Lab Rat
8 posts

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

May 29, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

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

May 29, 2012

Volker Volker
Robot Herder
5428 posts

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/

May 29, 2012

matthewpl matthewpl
Lab Rat
8 posts
1+1=2 wrote:
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).

Volker wrote:
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.

May 29, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

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

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

  1. char abc[]="ąśćółęńżź";

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

May 30, 2012

matthewpl matthewpl
Lab Rat
8 posts

1+1=2 wrote:
IMO, you need to provide more information.

1. the SHA of Qt5’s source code

Qt git directory: 9985003ac4a42adfa35db286eda1b2ae9656d85b
qtbase: ac16d722140661cd21949ca321b659ba2c359388

1+1=2 wrote:
2. The compiler you used.

  1. $ g++ -v
  2. Using built-in specs.
  3. COLLECT_GCC=g++-4.7
  4. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
  5. Target: x86_64-linux-gnu
  6. 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
  7. Thread model: posix
  8. gcc version 4.7.0 (Debian 4.7.0-9)

1+1=2 wrote:
Don’t told us you used something like

  1. 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:

  1. 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)?

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

  1. matthew@pingwinek:~/tmp$ cat main.cpp
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  char abc[]="ąśćółęńżź";
  9.  
  10.  cout << sizeof(abc) << endl;
  11.  
  12.  return 0;
  13. }
  14.  
  15. matthew@pingwinek:~/tmp$ g++ main.cpp -o cpp
  16. matthew@pingwinek:~/tmp$ ./cpp
  17. 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).

May 30, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

You can write a simple example like this

  1. #include <QApplication>
  2. #include <QLabel>
  3.  
  4. #if _MSC_VER >= 1600
  5. #pragma execution_character_set("utf-8")
  6. #endif
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication a(argc, argv);
  11.     QLabel label("ąśćółęńżź");
  12.     label.show();
  13.  
  14.     return a.exec();
  15. }

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

May 30, 2012

matthewpl matthewpl
Lab Rat
8 posts

I get something like this (window style: oxygen, app style: plastique because of

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

 
  ‹‹ [SOLVED] Painting into a QWidget from a 3rd party DLL?      Build *both* static and dynamic libs ››

You must log in to post a reply. Not a member yet? Register here!