tags:

views:

2298

answers:

5

I have tried lookin for the answer to this question, but was not able to do this.

Whats the most basic way to do it?

+1  A: 
QString qstr("This is my c-string");

like here: http://lists.trolltech.com/qt-interest/2002-03/thread00047-0.html

Kamil Szot
You wouldn't want to construct the QString with `new'.
Intransigent Parsnip
@Rohan - Thank you for your comment. I'm removing this from my answer.
Kamil Szot
+2  A: 
std::string s = "Sambuca";
QString q = s.c_str();
wilhelmtell
+1  A: 

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());
gavinb
+6  A: 

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
A: 

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 the std::string to copy itself in case there is no place to add the '\0' at the end.

shoosh