qt-interest@trolltech.com
[Top] [All Lists]

Re: how to print QString contents - QT4

Subject: Re: how to print QString contents - QT4
From: Konrad Rosenbaum
Date: Fri, 31 Aug 2007 09:15:40 +0200
On Friday 31 August 2007, Vahe Avedissian wrote:
> What am I missing? It's got to be trivially simple to do?

What is the exact code you are using?

This will work:
QString mystring="hallo";
qDebug("%s",mystring.toAscii().data());

-> it creates a temporary byte array, extracts its data and destroys the 
temporary data after the command finishes

This will work too:
QString mystring="hallo";
QByteArray myarray=mystring.toAscii();
char *str=myarray.data();
qDebug("%s",str);

-> here the byte array is not temporary, so its data is guaranteed to exist 
for a while (until the byte array is destroyed or changed)

This will NOT work:
QString mystring="hallo";
char *str=mystring.toAscii().data();
qDebug("%s",str);

-> it creates a temporary byte array, extracts its data, then destroys the 
temporary object, loses the data, and finally tries to display the lost 
data



        Konrad
<Prev in Thread] Current Thread [Next in Thread>