PDA

View Full Version : How to build Qstring from char or string?



tonnot
27th September 2010, 08:16
Wich is the fast and symple way for build a QString from a char or string value ?
I want to pass some string to a Qdialog that wants a QString ....
Thanks

kabanek
27th September 2010, 08:36
it works for me

std::string ss = "test";

QString aa(ss.c_str());

qDebug()<<aa;

Zlatomir
27th September 2010, 08:51
it works for me

std::string ss = "test";

QString aa(ss.c_str());

qDebug()<<aa;
As far as i know, that QString is going to be valid as long as the std::string will be. (it share data with that pointer returned by c_str() ) Please contradict me if i'm wrong!

So a better solution is the static member of QString fromStdString() (http://doc.qt.nokia.com/4.7/qstring.html#fromStdString) for std::string
Or fromAscii(), fromLatin1() (http://doc.qt.nokia.com/4.7/qstring.html#fromAscii) for char* string.

tonnot
27th September 2010, 10:58
I want to use strings at Qmessagebox.
Exists a function at QmessageBox to convert string to Qstring (that is the parameter waited by Qmessabox) ?
I dont want to write no include if it is not neccesary.
Thanks Kabanek & Zlatomir

Zlatomir
27th September 2010, 11:34
You don't have to include anything, both the methods i suggested are in the QString (static members of QString), if you didn't notice, in my previous post the functions names are links to documentation.

You can use it like this:


std::string str = "Text";
QString inText = QString::fromStdString(str);
what_ever->setText(inText);