Question

Quel est le moyen le plus simple de le faire?

Était-ce utile?

La solution

Si par chaîne vous voulez dire std :: string , vous pouvez le faire avec cette méthode:

QString QString :: fromStdString (const std :: string & amp; str)

std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

Si par chaîne vous entendez const char * en code Ascii, vous pouvez utiliser cette méthode:

QString QString :: fromAscii (const char * str, int size = -1 )

const char* str = "Hello world";
QString qstr = QString::fromAscii(str);

Si vous avez const char * encodé avec un encodage système lisible avec QTextCodec :: codecForLocale () , vous devez utiliser cette méthode:

QString QString :: fromLocal8Bit (const char * str, int size = -1 )

const char* str = "zażółć gęślą jaźń";      // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);

Si vous avez const char * codé en UTF8, vous devrez utiliser cette méthode:

QString QString :: fromUtf8 (const char * str, int size = -1 )

const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
QString qstr = QString::fromUtf8(str);

Il existe également une méthode pour const ushort * contenant la chaîne codée en UTF16:

QString QString :: fromUtf16 (const ushort * unicode, int size = -1 )

const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
QString qstr = QString::fromUtf16(str);

Autres conseils

Si compilé avec la compatibilité STL, QString a un méthode statique pour convertir un std :: string en QString :

std::string str = "abc";
QString qstr = QString::fromStdString(str);

Autre manière:

std::string s = "This is an STL string";
QString qs = QString::fromAscii(s.data(), s.size());

Cela présente l'avantage de ne pas utiliser .c_str () , ce qui pourrait entraîner la copie de std :: string au cas où il n'y aurait pas d'endroit pour ajouter le code < > '\ 0' à la fin.

std::string s = "Sambuca";
QString q = s.c_str();

Avertissement: Cela ne fonctionnera pas si std :: string contient \ 0 s.

J'ai rencontré cette question parce que j'avais un problème en suivant les réponses, alors je publie ma solution ici.

Les exemples ci-dessus montrent tous des exemples avec des chaînes contenant uniquement des valeurs ASCII. Dans ce cas, tout fonctionne correctement. Cependant, s’il s’agit de chaînes dans Windows qui peuvent également contenir d’autres caractères, tels que des trémas allemands, ces solutions ne fonctionnent pas.

Le seul code qui donne des résultats corrects dans de tels cas est

std::string s = "Übernahme";
QString q = QString::fromLocal8Bit(s.c_str());

Si vous n'avez pas à gérer de telles chaînes, les réponses ci-dessus fonctionneront correctement.

De plus, pour convertir ce que vous voulez, vous pouvez utiliser la classe QVariant.

par exemple:

std::string str("hello !");
qDebug() << QVariant(str.c_str()).toString();
int test = 10;
double titi = 5.42;
qDebug() << QVariant(test).toString();
qDebug() << QVariant(titi).toString();
qDebug() << QVariant(titi).toInt();

sortie

"hello !"
"10"
"5.42"
5

Voulez-vous dire une chaîne C, comme dans une chaîne char * , ou un objet C ++ std :: string ?

Dans les deux cas, vous utilisez le même constructeur, comme indiqué dans la référence QT:

Pour une chaîne C normale, utilisez simplement le constructeur principal:

char name[] = "Stack Overflow";
QString qname(name);

Pour un std :: string , vous obtenez le char * dans le tampon et le transmettez au constructeur QString :

std::string name2("Stack Overflow");
QString qname2(name2.c_str());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top