How to convert QString to const char *
-
I said there is no way because you said the const char * is lost immediatly at the next line of code.
But I found a way using usual c++, as edited in my previous post. -
if you use toStdString, take care of the encoding.
QString is unicode. So That is why I've written the stuff of the encoding. -
[quote author="Gerolf" date="1301125342"]
Take care of the lifetime of these variables![/quote]Ok Gerolf, I'll take care in the following contributions.
Thanks.
-
-
thank you sir it was helpful
-
i want to type cast QByteArray to a QString
@
QByteArray myAppString[4096] ;
QString myString ;
@
now i have some data in myAppstring now i want it to convert in QString
so i have QString Constructor no 9 - which take const ByteArray
but it is now working
@
QString *str1 = new QString(myAppString) ;
@
it give me erroris there any way to do conversion of Qbytearray to QString
-
A QByteArray is not much more than a nice wrapper around a char*, so the same rules apply.
If you say that you get an error and you want help on that error, it is best to actually mention said error.
-
QString Constructor No 9 (which takes a QByteArray) uses UTF-8, so you need to ensure that your byte array uses that encoding. It is better to use QString's static methods fromLatin1, fromLocal8Bit and fromUtf8 IMO because they are more explicit.
You can check "the documentation":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html for more detailed explanation.
And I'd also like to recommend some "good reading on character encoding":http://www.joelonsoftware.com/articles/Unicode.html if you're not already familiar with its concept.
-
This can be used for eg. to send debugging information from your GUI to the console; since Debug requires a const char * :
QString str = ui->lineEdit->text(); // lineEdit was already defined in a suitable context!
Debug(&str.toStdString()[0]);Hope it worked!
-
qPrintable is the best way
-
@dangelog Actually, I was redirected here from google search, which makes your post look pretty retarded.
-
Just QString.c_str() it works for me.