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?
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?
QString qstr("This is my c-string");
like here: http://lists.trolltech.com/qt-interest/2002-03/thread00047-0.html
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());
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);
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.