TR () functions in QT

Source: Internet
Author: User
Document directory
  • A brief review of Chinese qstring Problems
  • Where is TR defined?

During the forum, we often encounter tr-related problems. There are two types of people using TR:

  • (1) because he found that there was a problem with the old Chinese, and then searched and found that many people use TR, he also began to use tr
  • (2) another type of person, indeed out of the needs of internationalization, will need to display files on the interface are packaged with TR, there are two kinds:
    • (2a) Use tr to enclose English (the most recommended usage is the source code in English, and a translation package from English to other languages is provided)
    • (2B) Use tr to enclose Chinese characters (the source code uses Chinese characters and then provides translation packages from Chinese to other languages)

Note: If you are using TR to enclose Chinese characters but do not belong to (2B), this is a signal:

  • You are misuse tr
  • What you need is qstring, not tr

If you do belong to (2B), please be prepared. You may encounter many difficulties. Please considerAnalysis of QT internationalization (when the source code contains Chinese characters)

What does tr do? What are the differences between the two?

QString text1 = QObject::tr("hello"); QString text2 = QString("hello");

TR is used for internationalization. If you provide a Chinese translation package for this program (hello is translated into Chinese "hello"), the content of text1 will be Chinese "hello "; if you provide the program with a Japanese translation package, the content of text1 will be Japanese.

TR is a multi-level function call to implement translation operations at a cost, so it is best not to use it when it is not used.

Objects of interest

This article focuses on the situation where tr or translate contains Chinese strings:

  • Qobject: TR ()

  • Qcoreapplication: translate ()

  • Qtextcodec: setcodecfortr

How much can this question be said. Because the encoding problem involved is exactly the same as that in qstring and Chinese, only setcodecforcstrings is used and setcodecfortr is used.

A brief review of Chinese qstring Problems
  • The Unicode used by qstring does not have any problem in Chinese support
  • "I am a Chinese character"This is traditionalConst char *Narrow string

  • When assigning a narrow string to a qstring, we need to tell it the encoding of our narrow string (GBK? , UTF-8 ?)
  • The encoding mainly depends on the encoding of the source code file (GBK for Windows, UTF-8 for other platforms)

Example:

Qstring S1 = "I am a Chinese"; qstring S2 ("I am a Chinese"); qstring S3; S3 = "I am a Chinese"
  • S1 and S2 use the qstring constructor.Qstring (const char * Str)

  • S3 uses the qstring value assignment operator.Qstring & operator = (const char * Str)

If no encoding is specified, S1, S2, and S3 are all garbled characters. Because qstringConst char *According to Latin1, rather than the GBK or utf8.

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"))

One of these two statements can solve the problem. As for how to choose one, we will not repeat it here.

Qobject: TR

To be honest, it is not a good idea to use Chinese in tr. However, since there are always people using it (no matter (1) or (2B), and there are always people encountering problems, we should simply sort it out.

ComparedQcoreapplication: TranslateMany people are familiar with TR, although many people do not know what TR is.Pai_^

 

 

TR ("I am a Chinese ");

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

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

It is exactly the same as qstring ("I am a Chinese character"). Do you have to tell tr what encoding this narrow string is? If you don't tell it, it uses latin1. So the so-called garbled problem came out.

How can I tell tr the encoding of the Chinese characters you wrote on the disk? This is exactly

QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

. The two selection principles are the same as those in the previous article.

If your encoding uses utf8, you can directly use trutf8 without setting setcodecfortr.

If you only care about Garbled text, you can do so far (encoding is not followed below ). If you want to learn more about TR, you may as well .. Continue ..

 

Qcoreapplication: Translate

We know that TR is used to implement program internationalization (or multi-language translation). If we look at QT-related documents, we know that the following function is implemented:

QString QCoreApplication::translate ( const char * context, const char * sourceText, const char * disambiguation, Encoding encoding, int n )

In fact, this is the function that actually performs translation operations. The TR we mentioned above is to call this function to implement translation functions (we will see how tr calls translate later ).

Manual has a detailed explanation of TR and this function. Let's take a look at the following parameters:

  • Context context, which is generally the name of the class where the string to be translated is located
  • The string to be translated by sourcetext. (The encoding we focus on is actually its encoding)
  • Disambiguation is used to eliminate ambiguity. (For example, there are two "close" in our class. One is closed, and the other is intimate. Clearly, let the translators know the difference)
  • Encoding specifies the encoding. It has two values.
    • Codecfortr uses the encoding set by setcodecfortr () to explain sourcetext

    • Unicodeutf8 uses utf8 encoding to explain sourcetext
    • In fact, these two correspond to TR and trutf8 respectively.
  • N processing the singular and plural (this problem does not exist for Chinese)
Tr and translate

The two functions are described in manual of qobject and manual of qcoreapplication.

Describes the relationship between Tr and translate. As mentioned above, TR calls translate. If there is no evidence, it is hard to convince everyone. Okay. Continue.

Where is TR defined?

You may say: Isn't that nonsense? Manual writes it clearly. It is a static member function of qobject. And the source code is used as evidence:

// From src/corelib/kernel/qobject. h # ifdef qdoc static qstring tr (const char * sourcetext, const char * Comment = 0, int n =-1); static qstring trutf8 (const char * sourcetext, const char * Comment = 0, int n =-1); # endif

Hey, I almost got cheated. I found no: they were wrapped by pre-processing statements.

What does this mean? This code is describedOnlyIt is used to generate beautiful documents of QT (qdoc3 extracts information from the code and generates a series of manual in HTML format ).

Ah, that is to say, this is false. What is the real definition ?? In a place that everyone is familiar with, guess?

This is

Q_OBJECT

The macro is defined in src/corelib/kernel/qobjectdefs. h.

#define Q_OBJECT \ public: \ Q_OBJECT_CHECK \ static const QMetaObject staticMetaObject; \ Q_OBJECT_GETSTATICMETAOBJECT \ virtual const QMetaObject *metaObject() const; \ virtual void *qt_metacast(const char *); \ QT_TR_FUNCTIONS \ virtual int qt_metacall(QMetaObject::Call, int, void **); \ private:

The macro qt_tr_functions

#  define QT_TR_FUNCTIONS \ static inline QString tr(const char *s, const char *c = 0) \ { return staticMetaObject.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); } \ static inline QString trUtf8(const char *s, const char *c, int n) \ { return staticMetaObject.trUtf8(s, c, n); }

Now we can see that tr calls the tr function of the staticmetaobject object. The definition of staticmetaobject is in the XXX. MoC or moc_xxx.cpp file generated by MOC (which you can verify at any time ).

Staticmetaobject is an instance of the qmetaobject class. Let's continue to look at the source code of this class:

/*! \internal */ QString QMetaObject::tr(const char *s, const char *c) const { return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr); } /*! \internal */ QString QMetaObject::trUtf8(const char *s, const char *c) const {     return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8); 

}

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.