简体   繁体   中英

QT 4 <->QT 5 Encoding

I am trying to account for the use of both QT4 and QT5. The system uses both versions but they're in separate products that share common files.

I have found a solution that works but it has to do a version check each time and I would like to find a way to have a "one encoding fits both" scenario.

My current solution is something like this, using a micro sign (µ) as an example.

The QT 4 encoding only needs a nibble from the byte which in this case is (\\xb5)

Qt 5 needs the whole byte (\\xc2\\xb5).

I have also tried using the C++ code (\µ) but that produced a Î 1/4

c++

#include <QtCore/QtGlobal>
#if QT_VERSION >= 0x050000
print out a QString with the code \xc2\xb5
#else
print out a QString with the code \xb5
#endif

Is there a better way to do this and avoid the need for a version check? I have around 50 of these checks that I would like to eliminate.

The problem is that by default, QString in Qt4, when working with char* or const char* arguments, treats them as ASCII strings (see the docs ), while in Qt5 they are UTF-8 strings ( the docs ). So what you think "only needs a nibble from the byte" is actually "needs the ASCII value for the character". The µ character has code 0xB5 in Latin1, which is the default code page for Qt4's ASCII. And the C2 B5 is the UTF-8 representation of the code point corresponding to this character.

So what you actually need to do is to avoid the constructor QString::QString(const char*) , and instead use QString::fromUtf8 explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM