On the TR () function and Chinese garbled problem in QT

Source: Internet
Author: User
about the TR () function in Qt

Drift in the forum and often encounter a problem with TR-related problems. There are two kinds of people who use tr: (1) Because of the old problem of discovering Chinese, then search, find a lot of people with TR, so he also began to use TR (2) Another type of people, is indeed out of the need for internationalization, will need to be displayed in the interface of the files are used in TR package, there are two: (2a) with TR wrapped in English ( The most recommended use, the source code in English, and then provide English to other languages translation package (2b) with TR wrapped in Chinese (source code in Chinese, and then provide Chinese to other languages translation package)

Note Oh, if you're using TR to wrap Chinese characters but don't belong to (2b), then this is a signal: you're misusing tr what you need is qstring, not tr

If you really belong to (2b), please be prepared, you may also encounter a lot of difficulties, please consider QT internationalization (source contains Chinese) bit analysis

What is TR doing? What's the difference here?

QString Text1 = qobject::tr ("Hello");
 QString Text2 = QString ("Hello");

TR is used to internationalize, if you provide a Chinese translation package for this program (where Hello is translated into Chinese "hello"), then Text1 content will be Chinese "hello", if you provide and use Japanese translation package for the program, then Text1 content will be Japanese.

TR is a multi-level function call to achieve the translation operation, there is a price, so should not be used when the best not to use. objects of concern

This article focuses on the case where the Chinese string is included in TR or translate:

QOBJECT::TR ()

Qcoreapplication::translate ()

Qtextcodec::setcodecfortr

This question is much to be said. Because the coding problems involved and qstring and Chinese problems are exactly the same, but one is used setcodecforcstrings a setcodecfortr. a brief review of Qstring's Chinese problems QString Unicode, there is no problem with Chinese support

"I am Chinese" This is a narrow string of traditional const char * When you assign a narrow string to qstring, we need to tell it what kind of encoding (GBK, utf-8?) to use for our narrow strings. Windows is generally GBK, other platforms generally utf-8)

Example:

QString S1 = "I am Chinese";
QString S2 ("I am Chinese");
QString S3;
S3 = "I am Chinese"

S1 and S2 are qstring constructors qstring (const char * str)

S3 is using the QString assignment operator QString & operator= (const char * str)

If you do not specify the encoding, S1,S2,S3 will be all (most people in the country called) garbled. Because qstring the const char * According to Latin1, not the GBK or UTF8 that the user expects.

Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("GB2312"));
Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("UTF-8"))

One of these two statements solves the problem, as to how to select it, depending on how the source code file is encoded (Notepad opens to save as viewable), the latter if it is UTF-8 encoded, and the former if it is ANSI-encoded. qobject::tr

To tell you the truth, using Chinese in TR is not a good idea. But since there is always someone to use (whether (1) or (2b)), and people always encounter problems, so it is easy to tidy up.

Compared with qcoreapplication::translate, we should use a lot of TR, although many people do not know exactly what TR is doing ^_^

TR ("I am Chinese");

This call is the following function (at least we can think so).

QString qobject::tr (const char * sourcetext, const char * disambiguation = 0, int n =-1)

Exactly like qstring ("I am Chinese"), you must tell tr what encoding this narrow string is. If you don't tell it, it will use Latin1. So the so-called garbled problem came out.

How do you tell tr what kind of code you wrote about the characters you saved on the disk? That's exactly what

QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB2312")); 
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("UTF-8"));

Do. The principles of these two choices are not repeated here, as they are exactly the same as the preceding text.

If your code uses UTF8, you can use TrUtf8 without having to set SETCODECFORTR.

If you only care about garbled problems, that's all you can do (no more attention to coding below). If you want to learn more about TR, you might as well. Go on..

qcoreapplication::translate

We know that TR is used to implement the internationalization of the program (or multi-language translation), look at the QT related data, we know that the implementation of this function also has the following function:

QString qcoreapplication::translate (const char * context, const char * sourcetext, const char * disambiguation, Encoding encoding, int n)

In fact, this is the real translation operation of the function, the previous we mentioned TR finally by calling the function to achieve the translation function (later we will see how the TR call translate).

There are more detailed explanations for TR and this function in manual. Let's take a quick look at its parameters: context, which is generally the name of the class that needs to be translated SourceText the string that needs to be translated. (The encoding we are concerned with is actually the code) disambiguation to eliminate ambiguity. (for example, there are two "close" in our class, one meaning is closed and the other is intimate.) It is obvious that translators need to know this distinction) encoding specifies the encoding. It has two values.

CODECFORTR uses the encoding of the SETCODECFORTR () setting to explain the SourceText UnicodeUTF8 uses UTF8 encoding to interpret the sourcetext in fact, these two correspond to the TR and TrUtf8 n processing single plural (for Chinese, Does not exist this problem) tr and translate

The description of the two functions, one in the Qobject manual, the other in the Qcoreapplication manual.

Introduce the relationship between TR and translate. As mentioned earlier, TR calls the translate. If this is the case, there is no evidence, and it is hard for us to believe. Okay, go on. TR WHERE to define

You might say, "It's not nonsense, manual, it's a static member function of Qobject." But also has the source code for the proof:

From Src/corelib/kernel/qobject.h 
#ifdef qdoc 
static QString tr (const char *sourcetext, const char *comment = 0, I NT n =-1);
Static QString TrUtf8 (const char *sourcetext, const char *comment = 0, int n =-1);
#endif

Hey, almost been cheated, found no: they were wrapped in preprocessing statements.

What does that mean? Explains that this code is just used to generate the beautiful document of QT (qdoc3 extracts information from code, generates a series of HTML manual).

Ah, that is to say, this is false. So what's the real definition? In a place where everyone is familiar, guess.

This is

Q_object

The definition of the macro is in Src/corelib/kernel/qobjectdefs.h

#define Q_OBJECT \ Public
: \
q_object_check \
static const qmetaobject staticmetaobject; \
Q_object_ge Tstaticmetaobject \
Virtual const qmetaobject *metaobject () const; \
virtual void *qt_metacast (const char *); \< c6/> qt_tr_functions \
virtual int qt_metacall (qmetaobject::call, int, void * *); \
Private:

where the macro qt_tr_functions

#  define qt_tr_functions \
static inline QString TR (const char *s, const char *c = 0) \
{return Staticmet aobject.tr (S, c);  } \
static inline QString TrUtf8 (const char *s, const char *c = 0) \
{return Staticmetaobject.trutf8 (S, c);} \
Static inline QString tr (const char *s, const char *c, int n) \
{return staticmetaobject.tr (s, c, n);} \<  c16/> Static inline QString TrUtf8 (const char *s, const char *c, int n) \
{return Staticmetaobject.trutf8 (s, c, N); }

Now see: TR calls the TR function of the Staticmetaobject object, and Staticmetaobject is defined in the Xxx.moc or moc_xxx.cpp file that MOC generates (you can always verify).

Staticmetaobject is an example of a qmetaobject class, and we continue to look at the source code for this class:

/*!
\internal 
*
/QString qmetaobject::tr (const char *s, const char *c) const
{return
qcoreapplication::tr Anslate (D.stringdata, S, c, QCOREAPPLICATION::CODECFORTR);
}
/*!
\internal 
* 
/QString Qmetaobject::trutf8 (const char *s, const char *c) const 
{     

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.