[Qt-interest] howto convert QString to char*

BRM bm_witness at yahoo.com
Tue Jul 13 20:23:03 CEST 2010


----- Original Message ----

> > 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
> 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.

Yeah, my bad there - you are correct; probably get other code that got me 
thinking of data() instead of c_str().
c_str() is the safer one, and is what I do use.

Ben




More information about the Qt-interest-old mailing list