Results 1 to 4 of 4

Thread: Utf8 conversion from stdstring to qstring

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Utf8 conversion from stdstring to qstring

    Ok, so I have a file. Inside it it has a word (better: a filename) which contains greek characters. This is the code to read the file with the greek word and to add this word to a label:
    Qt Code:
    1. char home1 [ 60 ]="/home/alex/check.txt";
    2. char line1 [ 300 ];
    3. FILE *file1 = fopen ( home1, "r" );
    4. fgets ( line1, sizeof line1, file1 );
    5. fclose ( file1 ); //Now `line1` has the word with the greek characters
    6. string name = string(line1);
    7. name.erase(name.length()-1); //Sorting the `name` to delete "\n"
    8. cout << name; // Check: Here `name` is outputed normally, as it should (with the greek characters, so it's OK till now.
    9. QString image_name = QString::fromStdString(name).toUtf8(); //Here must be the evil function that destroys the `name`
    10. ui->image_name->setText(image_name.toUtf8()); //Finally, after the previous function has 'destroyed' the greek letters of `name`, the malformed `name` is put to the lineEdit :(
    To copy to clipboard, switch view to plain text mode 
    Read the code's comments

    Thx in advance for any reply
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Utf8 conversion from stdstring to qstring

    Oh yes. You need to do:
    Qt Code:
    1. QString image_name = QString::fromUtf8(name.c_str());
    To copy to clipboard, switch view to plain text mode 
    Once the text is in a QString and properly converted, you no longer need to bother about encoding until you are going to output it to a non-qt library or to a file.

    But why not use QTextStream and QTextCodec? That'll pretty much give you a two or three line solution with the same result.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    hakermania (27th December 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Utf8 conversion from stdstring to qstring

    Thx buddy, your solution works if I convert ui->image_name->setText(image_name.toUtf8()); to ui->image_name->setText(image_name);
    Please, propose your solution, if you want so.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Utf8 conversion from stdstring to qstring

    It would probably be something like this:

    Qt Code:
    1. QFile f("/home/alex/check.txt");
    2. f.open(QIODevice::ReadOnly);
    3. QTextStream stream(&f);
    4. stream.setCodec(QTextCodec::codecForName("UTF-8"));
    5.  
    6. QString line1 = stream.readLine();
    7. qDebug() << "read from file:" << line1;
    8. ui->image_name->setText(QDir::toNativeSeparators(line1));
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. BSTR to QString Conversion
    By bismitapadhy in forum Qt Programming
    Replies: 9
    Last Post: 16th February 2012, 12:51
  2. Qstring conversion UTF8
    By harleyskater in forum Qt Programming
    Replies: 12
    Last Post: 25th June 2010, 11:59
  3. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 22:16
  4. qreal -> QString conversion
    By MarkoSan in forum Newbie
    Replies: 2
    Last Post: 13th April 2008, 14:37
  5. UTF8 to ISO 8859... conversion
    By claudio in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2007, 21:24

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
  •  
Qt is a trademark of The Qt Company.