PDA

View Full Version : Cast to const char *



brevleq
2nd January 2009, 22:07
How can I cast a QString to const char *??

I've tried QString::toStdString but it returns a std::string so if I can cast it to const char *... but I don't know how!

wysota
2nd January 2009, 22:22
You have to go through byte array. See docs for QString for transformations to byte array and then use QByteArray::constData() to go to const char *. Just make sure you either copy the data or store the byte array in a real variable if you want to use that pointer later on.

spirit
3rd January 2009, 06:41
you can also use qPrintable it is equivalent to Wysota's suggestion.

caduel
3rd January 2009, 09:39
... but note that this is not merely a cast but a conversion. Also note that when the temporary objects (e.g. the std::string or the QByteArray) go out of scope the pointer returned gets invalid.