July 20, 2012

Hostel Hostel
Hobby Entomologist
187 posts

QString global variable and coding problem

 

I have a problem with QString which is a global variable. Coding of this variable is wrong. File is in UTF8. Here is example code:

  1. #include <QtCore/QCoreApplication>
  2. #include <QTextCodec>
  3. #include <QDebug>
  4.  
  5. QString global( "ąęśćź" );
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QCoreApplication a(argc, argv);
  10.  
  11.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  12.     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  13.  
  14.     QString inside( "ąęśćź" );
  15.  
  16.     qDebug() << "Global: " << global; // print Global:  "ąęśćź"  - it's wrong
  17.     qDebug() << "Inside: " << inside; // print Inside:  "ąęśćź" - good
  18.  
  19.     return a.exec();
  20. }

How to solve it?

3 replies

July 20, 2012

dmcr dmcr
Ant Farmer
161 posts

Hello,

How about utf-8 BOM.
if you select “add if encoding is UTF-8” (on windows), it may work correctly.

July 20, 2012

sidewinder sidewinder
Ant Farmer
65 posts

Your global QString is created before you set codec for c strings to UTF8 so default Latin1 is used. Hence text corruption.

 Signature 

“Never memorize what you can look up in books.”
Albert Einstein

July 20, 2012

Hostel Hostel
Hobby Entomologist
187 posts

sidewinder your are right. To print correct global string this is required:

  1. global.toLatin1()

 
  ‹‹ QTextEdit with Enter in searching      Is that meego will reborn? ››

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