How to I get the UTF-8 data into a QString

Go To


via server API's i'm getting contact information using member of structure:

char displayName[USERNAME_MAX_SIZE];

One of those names is a European name with special characters: "Per Spånt"

the structure of the displayName is as follows:

enter image description here

When I import 'displayName' into a QString via the "fromUtf8" function, I am getting the following QString: enter image description here

How can I get the correct string into my QString without converting the special character into two weird characters?

2012-04-03 19:42
by JasonGenX
Are you sure the conversion is incorrect? That looks very much like UTF-8 interpreted as Latin1 - Mat 2012-04-03 19:53
Well... fromUtf8 is the answer. It works for me. How about showing some code - ypnos 2012-04-03 19:53
Gentlemen, my bad- I was "fromUtf8"'ing the wrong string - JasonGenX 2012-04-03 19:58


FWIW, this is what works and doesn't work for me using Qt 4.7.4.

#include <iostream>
#include <string>

#include <QDebug>
#include <QString>

int main()
    char name[7] = "Sp__nt";

    std::cout << name << std::endl;                           // works

    qDebug() << QString::fromUtf8( name );                    // does not work
    qDebug() << QString::fromAscii( name );                   // works
    qDebug() << QString::fromLatin1( name );                  // works
    qDebug() << QString::fromStdString( name );               // works

    return 0;
2012-04-03 19:57
by Troubadour