Results 1 to 2 of 2

Thread: QSring utf8 and latin1

  1. #1
    Join Date
    Mar 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QSring utf8 and latin1

    Hi,

    I am using QT-5.
    I have used the below code:
    Qt Code:
    1. QString str1= QString::fromLatin1("abc÷ßà éöø");
    2. QString str= QString::fromUtf8("abc÷ßà éöø");
    3. if (str1==str)
    4. ui->textEdit_2->setText("true");
    5. else
    6. ui->textEdit_2->setText("false");
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by anda_skoa; 3rd March 2017 at 07:44. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSring utf8 and latin1

    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,
    _

Similar Threads

  1. Charset: latin1 in MySQL Why?
    By Mauricio in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2014, 09:37
  2. decoding from utf8
    By pcaeiro in forum Qt Programming
    Replies: 6
    Last Post: 25th August 2009, 12:51
  3. QFtp and non-Latin1 FTP servers
    By miwarre in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2009, 12:44
  4. UTF8 and ByteOrderMark (BOM)
    By SidGBF in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:25
  5. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.