Bokambo
Bokambo

Reputation: 4480

Chinese Text Display in QT Embedded Linux?

I am using below code to display chinese text on click of a button , its working fine in Windows but when i try in Embedded device it show some junk values. I am using "Batang" Font . This font is installed in my Embedded device.

QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
      QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
      QString qString1 = tr("鳶尾花");
      QByteArray byteArray = qString1.toUtf8();
      const char* cString = byteArray.data();
      QString qString2 = QString::fromUtf8(cString);
      QTextCodec::setCodecForTr(QTextCodec::codecForName(cString));
      ui->txtFirstname->setText(qString2);

Any help is appreciated. Thanks

Upvotes: 1

Views: 2099

Answers (3)

stackr
stackr

Reputation: 2742

Try using a different encoding and not UTF8 depends on the characters you will be using. Hope this helps.

  • Guobiao is mainly used in Mainland China and Singapore. All Guobiao standards are prefixed by GB, the latest version is GB18030 which is a one, two or four byte encoding.
  • Big5, used in Taiwan, Hong Kong and Macau, is a one or two byte encoding.
  • Unicode, with the set of CJK Unified Ideographs.

Read this for more info: http://doc.qt.io/qt-5/codec-big5.html because the characters u use seem to be Big5 encoding characters A tutorial can be found here: http://doc.qt.io/qt-5/qtextcodec.html

Upvotes: 1

Bokambo
Bokambo

Reputation: 4480

I resolved using :

 QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));

Big5 was not giving me correct result. Thanks.

Upvotes: 1

Tim Meyer
Tim Meyer

Reputation: 12600

When you added the line

QTextCodec::setCodecForTr(QTextCodec::codecForName(cString));

you probably thought the following overload:

QTextCodec * QTextCodec::codecForName ( const QByteArray & name ) [static]

would try to find the best codec for the characters in the byte array you supplied.

However, this function tries to find the codec which has a name closest to the value you supplied, so you would have to do something like

QTextCodec::setCodecForTr(QTextCodec::codecForName("Big5"));

instead.

Have you tried leaving out that line? You are already setting the text codec a few lines above anyway.

Upvotes: 1

Related Questions