162

What is the most basic way to do it?

Angie Quijano
  • 3,308
  • 3
  • 20
  • 30
NeverAgain
  • 1,743
  • 3
  • 13
  • 9

7 Answers7

243

If compiled with STL compatibility, QString has a static method to convert a std::string to a QString:

std::string str = "abc";
QString qstr = QString::fromStdString(str);
sth
  • 200,334
  • 49
  • 262
  • 354
  • 3
    This should actually be avoided in my opinion. If the version of the standard library QT was compiled with is somehow different from what you're compiling with, you're going to have trouble. In creates an unnecessary dependency between QT and libstdc++ that exists nowhere else in QT. – shoosh Sep 12 '13 at 20:16
  • @shoosh: To avoid that couldn't one just do `QString qstr = QString(str.c_str());`? Not sure whether `QString` copies what's passed into it. – Claudiu May 09 '14 at 20:21
  • @shoosh: I understand your concerns about compatability, but both QString and std::string can contain null characters. `fromStdString` will preserve these, constructing from `.c_str` won't. (And it was precisely how to do this that led me to this question.) – Martin Bonner supports Monica Oct 04 '17 at 12:07
  • @MartinBonner QByteArray has a ctor that takes const char* and length. QString has a ctor that takes QByteArray – shoosh Oct 04 '17 at 19:24
174

If by string you mean std::string you can do it with this method:

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

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

If by string you mean Ascii encoded const char * then you can use this method:

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

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

If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method:

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);

If you have const char * that's UTF8 encoded then you'll need to use this method:

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);

There's also method for const ushort * containing UTF16 encoded string:

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);
Kamil Szot
  • 15,477
  • 6
  • 53
  • 62
13

Alternative way:

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

This has the advantage of not using .c_str() which might cause the std::string to copy itself in case there is no place to add the '\0' at the end.

shoosh
  • 70,450
  • 50
  • 199
  • 310
11
std::string s = "Sambuca";
QString q = s.c_str();

Warning: This won't work if the std::string contains \0s.

Claudiu
  • 206,738
  • 150
  • 445
  • 651
wilhelmtell
  • 53,297
  • 19
  • 89
  • 128
3

I came across this question because I had a problem when following the answers, so I post my solution here.

The above examples all show samples with strings containing only ASCII values, in which case everything works fine. However, when dealing with strings in Windows whcih can also contain other characters, like german umlauts, then these solutions don't work

The only code that gives correct results in such cases is

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

If you don't have to deal with such strings, then the above answers will work fine.

Devolus
  • 20,356
  • 11
  • 56
  • 104
  • This is dependent not on std::string but the encoding of your source file with the string literal. If your source file is UTF8, it will work but break your call to `fromLocal8Bit()` – namezero Dec 06 '19 at 18:48
0

Do you mean a C string, as in a char* string, or a C++ std::string object?

Either way, you use the same constructor, as documented in the QT reference:

For a regular C string, just use the main constructor:

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

For a std::string, you obtain the char* to the buffer and pass that to the QString constructor:

std::string name2("Stack Overflow");
QString qname2(name2.c_str());
Tarod
  • 5,804
  • 5
  • 38
  • 45
gavinb
  • 16,950
  • 2
  • 42
  • 53
  • 1
    True enough, although the OP didn't mention embedded NULLs. If that is required, you can use `QByteArray::QByteArray (const char* data, int size)` to wrapper the buffer first, and then pass that to the `QString` constructor. – gavinb Aug 11 '11 at 03:06
0

Moreover, to convert whatever you want, you can use the QVariant class.

for example:

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();

output

"hello !"
"10"
"5.42"
5
Gabriel de Grimouard
  • 1,739
  • 13
  • 19