| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 6 | |
Hi,
I seem to be having problems printing the data contents of QString objects.
I've tried the qDebug, and printf using the dat, toAscii, to unicode, etc... methods.
It compiles, but at runtime I either see the first character only, or get a crash with
"Illegal instruction" message.
This seemed to work with QT3.x and I cannot find any documentation or examples of
how to do this correctly.
What am I missing? It's got to be trivially simple to do?
Vahe Avedissian
____________________________________________________________________________________
Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, photos & more.
http://mobile.yahoo.com/go?refer=1GNXIC
--
[ signature omitted ]
On Friday 31 August 2007, Vahe Avedissian wrote: > Hi, > > I seem to be having problems printing the data contents of QString objects. > > I've tried the qDebug, and printf using the dat, toAscii, to unicode, > etc... methods. It compiles, but at runtime I either see the first > character only, or get a crash with "Illegal instruction" message. > > This seemed to work with QT3.x and I cannot find any documentation or > examples of how to do this correctly. > > What am I missing? It's got to be trivially simple to do? It should be a matter of: #include <QtDebug> qDebug() << myQString1 << myQString2; If you get crashes, then I'd start to do some memory debugging. Valgrind comes to mind. -- [ signature omitted ]
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
Attachment:
Attachment:
pgpyTWkb2KJ4q.pgp
Attachment:
signature.asc
Attachment:
signature.asc
Description: PGP signature
Message 4 in thread
On fredag den 31. August 2007, Konrad Rosenbaum wrote:
> On Friday 31 August 2007, Vahe Avedissian wrote:
> > What am I missing? It's got to be trivially simple to do?
All functions with a ... parameter (like printf and qDebug) can only operate
on basic types like int, pointers, etc. This is a language issue, not a Qt
issue.
You can use qDebug() << mystring instead. You need to #include <QDebug> for
this to work.
> What is the exact code you are using?
>
> This will work:
> QString mystring="hallo";
> qDebug("%s",mystring.toAscii().data());
Or use qDebug("%s", qPrintable(mystring));
Bo.
--
[ signature omitted ]
Message 5 in thread
Vahe Avedissian schrieb:
> Hi,
>
> I seem to be having problems printing the data contents of QString objects.
>
> I've tried the qDebug, and printf using the dat, toAscii, to unicode, etc... methods.
> It compiles, but at runtime I either see the first character only, or get a crash with
> "Illegal instruction" message.
>
> This seemed to work with QT3.x and I cannot find any documentation or examples of
> how to do this correctly.
>
> What am I missing?
The last method call to data():
qDebug ("The string is: %s", theQString.toLatin1().data());
Note that toLatin1(), toAscii() etc. return a QCString only - from this
you still need to pass along the actual char * to qDebug, with the
data() method.
Cheers, Oliver
--
[ signature omitted ]
Message 6 in thread
Or you can use the qPrintable macro:
qDebug("%s", qPrintable(myQString));
On Aug 31, 2007, at 7:48 AM, Till Oliver Knoll
<oliver.knoll@xxxxxxxxxxx> wrote:
> Vahe Avedissian schrieb:
>> Hi,
>> I seem to be having problems printing the data contents of QString
>> objects.
>> I've tried the qDebug, and printf using the dat, toAscii, to
>> unicode, etc... methods.
>> It compiles, but at runtime I either see the first character only,
>> or get a crash with
>> "Illegal instruction" message.
>> This seemed to work with QT3.x and I cannot find any documentation
>> or examples of
>> how to do this correctly.
>> What am I missing?
>
> The last method call to data():
>
> qDebug ("The string is: %s", theQString.toLatin1().data());
>
> Note that toLatin1(), toAscii() etc. return a QCString only - from
> this you still need to pass along the actual char * to qDebug, with
> the data() method.
>
> Cheers, Oliver
> --
> =====================================================================
> *** AutoForm is proud winner of the AUTOMOTIVE NEWS PACE AWARD 2004.
> This prestigious award honours our continuous game-changing
> innovations and recognizes our commitment to global leadership
> in sheet metal forming technology.
> ---------------------------------------------------------------------
> AutoForm Engineering GmbH Tel: +41 43 444 6161
> Technoparkstrasse 1 Fax: +41 43 444 6162
> CH-8005 Zurich / Switzerland www.autoform.com
> =====================================================================
> The information contained in this e-mail is confidential and is
> provided only for the intended addressee's use. If you received this
> e-mail in error, please delete it and contact us. Thank you.
> =====================================================================
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
--
[ signature omitted ]
Message 7 in thread
On Friday 31 August 2007, Brant Sears wrote:
> Or you can use the qPrintable macro:
> qDebug("%s", qPrintable(myQString));
>
> On Aug 31, 2007, at 7:48 AM, Till Oliver Knoll
>
> <oliver.knoll@xxxxxxxxxxx> wrote:
> > Vahe Avedissian schrieb:
> >> Hi,
> >> I seem to be having problems printing the data contents of QString
> >> objects.
> >> I've tried the qDebug, and printf using the dat, toAscii, to
> >> unicode, etc... methods.
> >> It compiles, but at runtime I either see the first character only,
> >> or get a crash with
> >> "Illegal instruction" message.
> >> This seemed to work with QT3.x and I cannot find any documentation
> >> or examples of
> >> how to do this correctly.
> >> What am I missing?
> >
> > The last method call to data():
> >
> > qDebug ("The string is: %s", theQString.toLatin1().data());
> >
> > Note that toLatin1(), toAscii() etc. return a QCString only - from
> > this you still need to pass along the actual char * to qDebug, with
> > the data() method.
> >
> > Cheers, Oliver
Or you can use the friendly sintax described here:
http://doc.trolltech.com/4.3/qtglobal.html#qDebug
qDebug() << myString << myOtherObject ;
this may also help:
http://doc.trolltech.com/4.3/debug.html#providing-support-for-the-qdebug-stream-operator
--
[ signature omitted ]
Description: This is a digitally signed message part.
Message 8 in thread
Iulian M schrieb:
> > ...
> Or you can use the friendly sintax described here:
>
> http://doc.trolltech.com/4.3/qtglobal.html#qDebug
OR... you can simply use a cast, as in
qDebug ("The string is: %s", (char *)myQString);
I believe this is a typical job qualification question at Trolltech:
"Tell us at least 4 different ways as to make a QString printable on
stdout/stderr!"
;)
Have a nice week-end!
Oliver
--
[ signature omitted ]
Message 9 in thread
On Friday 31 August 2007 16:01:40 Till Oliver Knoll wrote:
> Iulian M schrieb:
> > > ...
> >
> > Or you can use the friendly sintax described here:
> >
> > http://doc.trolltech.com/4.3/qtglobal.html#qDebug
>
> OR... you can simply use a cast, as in
>
> qDebug ("The string is: %s", (char *)myQString);
That will fail if you compiled your code with QT_NO_CAST_TO_ASCII.
It is a good idea to compile your code that way (and with
QT_NO_CAST_FROM_ASCII) to avoid implicit casts that throw away the encoding
of your strings.
> I believe this is a typical job qualification question at Trolltech:
> "Tell us at least 4 different ways as to make a QString printable on
> stdout/stderr!"
I'll pass that on to Human Resources ;-)
--
[ signature omitted ]
Description: This is a digitally signed message part.