[Qt-interest] howto convert QString to char*

Will Rutherdale (rutherw) rutherw at cisco.com
Tue Jul 13 20:07:31 CEST 2010


> Interesting; however, that has never seemed to work for me - 
> I usually get 
> compilation issues; so I typically just do:
> 
> QString qtString = "someString";
> std::string stdCString = qtString.toStdString();
> char* charPtr = stdCString.data();
> // charPtr is only good while stdCString (i) doesn't change, 
> and (ii) remains in 
> scope
> 
> Ben

That's odd.  I usually use c_str().  c_str() adds a terminating \0 at
the end, but data() does not.  At least that's what Stroustrup says in
Section 20.3.7 of _The C++ Programming Language_.  Usually when I want
to print a char const *, I want that \0.

Also, I don't understand why you've had trouble with the intermediate
QByteArray.  It works fine for me.

-Will




More information about the Qt-interest-old mailing list