How to convert QString to const char *
-
wrote on 26 Mar 2011, 02:08 last edited by
Hi,
Is there any way to convert a QString to a const char * ?
Thanks.
-
wrote on 26 Mar 2011, 02:24 last edited by
You can do this:
@QString myString = "BlaBla"
char* myChar = myString.toStdString().c_str();@Look this link "C++":http://www.cplusplus.com/reference/string/string/c_str/.
BR.
-
wrote on 26 Mar 2011, 02:25 last edited by
-
wrote on 26 Mar 2011, 07:42 last edited by
[quote author="Thanatos.jsse" date="1301106274"]You can do this:
@QString myString = "BlaBla"
char* myChar = myString.toStdString().c_str();@
[/quote]Take care of the lifetime of these variables!
if you have an in parameter, you can do such things, although I would not got the way with std::string:@
void foo(const char*);QSTring text; foo(text.toUtf8().constData());
@
aditionally, check:
- QByteArray toAscii () const
- QByteArray toLatin1 () const
- QByteArray toLocal8Bit () const
It's up to you, which encoding you use.
But all these functions return temporary objects, and calling constData returns the buffer of the temp objects. In the next line of code (when foo() returns) the temp object is destroyed and the buffer gets invalid! -
wrote on 26 Mar 2011, 09:39 last edited by
So there is no way...
Luckily there is good old std c++ :
string appName = m_sAppName.toStdString(); -
wrote on 26 Mar 2011, 09:46 last edited by
Why should the be no way? I showed you some...
it is, but which format do you need?
- utf8?
- 7-bit ASCII?
- 8-bit ASCI with with regional code?
- MBCS?
all are const char* formats.
QString is unicode.in general, it is possible:
@
QString text;
std::string s = text.toLatin1().constData;
foo(s.c_str());
@If you really need a const char* I would convert it to and std::string (but reagrd the conversion!) and use the std::string. Who shall otherwise handle the memory? who shall allocate it? who shall free it?
you can also do the following:
@
QString text;chat* p = new char[text.length() + 1); strcpy(p, text.toLatin1().constData());
@
Take care that text.length() might not valid if you then convert to utf8!
-
wrote on 26 Mar 2011, 09:55 last edited by
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. -
wrote on 26 Mar 2011, 11:22 last edited by
if you use toStdString, take care of the encoding.
QString is unicode. So That is why I've written the stuff of the encoding. -
wrote on 28 Mar 2011, 03:04 last edited by
[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.
-
wrote on 28 Mar 2011, 14:20 last edited by
-
wrote on 7 Aug 2013, 09:33 last edited by
thank you sir it was helpful
-
wrote on 7 Aug 2013, 09:52 last edited by
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
-
wrote on 7 Aug 2013, 11:45 last edited by
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.
-
wrote on 10 Aug 2013, 06:29 last edited by
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.
-
wrote on 30 Jun 2015, 20:49 last edited by
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!
-
wrote on 30 Jun 2015, 21:01 last edited by
qPrintable is the best way
-
wrote on 20 Oct 2015, 07:40 last edited by
Just QString.c_str() it works for me.