[Qt-interest] transforming QString into char *

Volker volker at openbios.org
Mon Mar 23 17:33:01 CET 2009


Am Montag 23 März 2009 15:32:43 schrieb Bob Hood:
> Giancarlo Amati wrote:
> > Hello everybody,
> >
> > probably has been aswered already to this question...but I try:
> >
> > how can I transform a QString into a char * ?
>
> There are several to*() methods for converting the internal contents of
> the QString to something else.  Most return a QByteArray, which needs a
> further cast.
>
> I tend to use something like:
>
>     const char *string_data = myString.toLatin1().constData();
Is this really safe? 
There is a temporary object of type QByteArray involved, which IMHO is 
destroyed once the statement is executed.
 It seems to me, string_data points to invalid memory afterwards - if not: 
who's is responsible for freeing that memory?


On the other hand, a statement like this
someFunction( myString.toLatin1().constData() );
should be ok, as the temp objects remain valid until the whole statement is 
finished (provided someFunction does not remember the passed pointer)



Regards
.....Volker








More information about the Qt-interest-old mailing list