March 26, 2011

acropole acropole
Lab Rat
5 posts

How to convert QString to const char *

 

Hi,

Is there any way to convert a QString to a const char * ?

Thanks.

13 replies

March 26, 2011

Thanatos.jsse Thanatos.jsse
Lab Rat
72 posts

You can do this:

  1. QString myString = "BlaBla"
  2. char* myChar = myString.toStdString().c_str();

Look this link C++ [cplusplus.com].

BR.

March 26, 2011

peppe peppe
Ant Farmer
1026 posts

http://lmgtfy.com/?q=convert+qstring+to+const+char+*

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

March 26, 2011

Gerolf Gerolf
Robot Herder
3235 posts
Thanatos.jsse wrote:
You can do this:
  1. QString myString = "BlaBla"
  2. char* myChar = myString.toStdString().c_str();

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:

  1.     void foo(const char*);
  2.  
  3.     QSTring text;
  4.     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!

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 26, 2011

acropole acropole
Lab Rat
5 posts

So there is no way…

Luckily there is good old std c++ :
string appName = m_sAppName.toStdString();

March 26, 2011

Gerolf Gerolf
Robot Herder
3235 posts

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:

  1.     QString text;
  2.     std::string s = text.toLatin1().constData;
  3.     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:

  1.     QString text;
  2.  
  3.     chat* p = new char[text.length() + 1);
  4.     strcpy(p, text.toLatin1().constData());

Take care that text.length() might not valid if you then convert to utf8!

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 26, 2011

acropole acropole
Lab Rat
5 posts

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.

March 26, 2011

Gerolf Gerolf
Robot Herder
3235 posts

if you use toStdString, take care of the encoding.
QString is unicode. So That is why I’ve written the stuff of the encoding.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

March 28, 2011

Thanatos.jsse Thanatos.jsse
Lab Rat
72 posts
Gerolf wrote:
Take care of the lifetime of these variables!

Ok Gerolf, I’ll take care in the following contributions.

Thanks.

March 28, 2011

shiroki shiroki
Lab Rat
6 posts

check this http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa

August 7, 2013

sumeetrt sumeetrt
Lab Rat
2 posts

thank you sir it was helpful

August 7, 2013

sumeetrt sumeetrt
Lab Rat
2 posts

i want to type cast QByteArray to a QString

  1. QByteArray  myAppString[4096] ;
  2. 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
  1. QString *str1 = new QString(myAppString) ;

it give me error

is there any way to do conversion of Qbytearray to QString

August 7, 2013

Andre Andre
Robot Herder
6296 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 10, 2013

uranusjr uranusjr
Lab Rat
84 posts

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 [qt-project.org] for more detailed explanation.

And I’d also like to recommend some good reading on character encoding [joelonsoftware.com] if you’re not already familiar with its concept.

 
  ‹‹ [Solved] Why there is no toTitleCase for QString      keeping content of plugin without loading the plugin ››

You must log in to post a reply. Not a member yet? Register here!