01-04-2013 12:28 PM
I create share data like this:
QString data = QString::fromUtf8(tr("some localized string")); query->setData(data.toUtf8());
Why do the examples show fromUtf8 / toUtf8 when many of the languages I support need Utf16? Is it okay to use Utf16 or will something happen I'm not aware of?
Solved! Go to Solution.
01-04-2013 12:56 PM - edited 01-04-2013 01:06 PM
UTF8 was created for backwards compatibility with ASCII and lower 7 bits match ASCII encoding. It can be used with std C functions like strlen() without any conversion.
In UTF8 one character can take up to 6 bytes and it's capable of storing all characters UTF16 can store.
Source code files are usually created in UTF8, but QString uses UTF16 internally, that's why fromUtf8 conversion is needed.
For network communication UTF8 is usually better because it doesn't depend on byte order.
Byte order for UTF16 can depend on HW architecture. Also UTF16 text sometimes has BOM (byte order marker) prepended to assist in determining the byte order of the characters. UTF8 doesn't require it.
One of UTF16 advantages is memory usage: UTF8 can take 3 bytes per character for non-ASCII text while UTF16 uses 2 most of the time.
01-04-2013 01:11 PM - edited 01-04-2013 01:14 PM
If function expects const char * or ByteArray and encoding isn't specified then most likely it expects UTF8-encoded string. It should be zero-terminated in case of const char *.
I've used UTF8 strings in InvokeActionItem for BBM sharing and non-ascii characters were displayed correctly.
QString data = QString::fromUtf8("text");
query->setData(data.toUtf8()); // setData expects ByteArray
01-04-2013 01:41 PM - edited 01-04-2013 01:42 PM
I've enabled Chinese input in language settings. Text I typed in application was passed unchanged into BBM so it seems to work. UTF8 is capable of representing the entire characters range.