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.

9 replies

March 26, 2011

Thanatos.jsse Thanatos.jsse
Ant Farmer
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
1025 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
Area 51 Engineer
3210 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
Area 51 Engineer
3210 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 conat 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
Area 51 Engineer
3210 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
Ant Farmer
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

 
  ‹‹ [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!