PDA

View Full Version : QSring utf8 and latin1



abhinav_oct30
1st March 2017, 03:44
Hi,

I am using QT-5.
I have used the below code:


QString str1= QString::fromLatin1("abc÷ßà éöø");
QString str= QString::fromUtf8("abc÷ßà éöø");
if (str1==str)
ui->textEdit_2->setText("true");
else
ui->textEdit_2->setText("false");

This always results in false.
Why is it like this?
Internally QString should be in same format,its character pointer which have different format,for which we use fromLatin1 or fromUtf8.

anda_skoa
3rd March 2017, 07:48
Why do you expect the result to be true?

You have two identical string literals containing non-ascii characters and pass them through two different codec functions.
Naturally you end up with two different results as each codec function interprets those non-ascii characters differently.

Which one of those is getting the correct interpretation depends on the encoding of your sources file:

if the source file is encoded in UTF-8, then the string literals is UTF-8, so fromUtf8() will generate the correct QString

Cheers,
_