Qt-interest Archive, March 2002
Problems with QString::sprintf()
Message 1 in thread
I'm trying to use QString::sprintf with little luck.
I'm getting some values form a MySQL database. Next I try to format them
and print them out like so:
QString name = query.value(0).toString();
QString cc = query.value(1).toString();
float lat = query.value(2).toDouble();
float lon = query.value(3).toDouble();
output.sprintf("%-32s\t%-s\t%+03.3f\t%+03.3f",
name, cc, lat, lon);
cout << output << endl;
During compilation I get the following warnings:
g++ -c -pipe -Wall -W -O2 -DQT_NO_DEBUG -I/var/tmp/qt/include
-I/var/tmp/qt/mkspecs/default -o main.o main.cpp
main.cpp: In function `int main (int, char **)':
main.cpp:50: warning: cannot pass objects of non-POD type `class
QString' through `...'
main.cpp:50: warning: cannot pass objects of non-POD type `class
QString' through `...'
main.cpp:50: warning: format argument is not a pointer (arg 3)
main.cpp:50: warning: format argument is not a pointer (arg 4)
What am I doing wrong?
cheers,
tomek
Message 2 in thread
Hello,
You can't pass QStrings through QString::sprintf().
-Justin
On Thursday 28 February 2002 19:38, you wrote:
> I'm trying to use QString::sprintf with little luck.
>
> I'm getting some values form a MySQL database. Next I try to format them
> and print them out like so:
>
> QString name = query.value(0).toString();
> QString cc = query.value(1).toString();
> float lat = query.value(2).toDouble();
> float lon = query.value(3).toDouble();
>
> output.sprintf("%-32s\t%-s\t%+03.3f\t%+03.3f",
> name, cc, lat, lon);
>
> cout << output << endl;
>
> During compilation I get the following warnings:
>
> g++ -c -pipe -Wall -W -O2 -DQT_NO_DEBUG -I/var/tmp/qt/include
> -I/var/tmp/qt/mkspecs/default -o main.o main.cpp
> main.cpp: In function `int main (int, char **)':
> main.cpp:50: warning: cannot pass objects of non-POD type `class
> QString' through `...'
> main.cpp:50: warning: cannot pass objects of non-POD type `class
> QString' through `...'
> main.cpp:50: warning: format argument is not a pointer (arg 3)
> main.cpp:50: warning: format argument is not a pointer (arg 4)
>
> What am I doing wrong?
>
> cheers,
> tomek
Message 3 in thread
Yes, you can, but you'll lose some Unicode information:
sprintf("%s", string.latin1())
or just
sprintf("%s", (const char*)string)
Justin wrote:
>Hello,
>
>You can't pass QStrings through QString::sprintf().
>
>-Justin
>
>On Thursday 28 February 2002 19:38, you wrote:
>
>>I'm trying to use QString::sprintf with little luck.
>>
>>I'm getting some values form a MySQL database. Next I try to format them
>>and print them out like so:
>>
>> QString name = query.value(0).toString();
>> QString cc = query.value(1).toString();
>> float lat = query.value(2).toDouble();
>> float lon = query.value(3).toDouble();
>>
>> output.sprintf("%-32s\t%-s\t%+03.3f\t%+03.3f",
>> name, cc, lat, lon);
>>
>> cout << output << endl;
>>
>>During compilation I get the following warnings:
>>
>>g++ -c -pipe -Wall -W -O2 -DQT_NO_DEBUG -I/var/tmp/qt/include
>>-I/var/tmp/qt/mkspecs/default -o main.o main.cpp
>>main.cpp: In function `int main (int, char **)':
>>main.cpp:50: warning: cannot pass objects of non-POD type `class
>>QString' through `...'
>>main.cpp:50: warning: cannot pass objects of non-POD type `class
>>QString' through `...'
>>main.cpp:50: warning: format argument is not a pointer (arg 3)
>>main.cpp:50: warning: format argument is not a pointer (arg 4)
>>
>>What am I doing wrong?
>>
>>cheers,
>>tomek
>>
>
>--
>List archive and information: http://qt-interest.trolltech.com
>
>
Message 4 in thread
Am Freitag, 1. März 2002 04:38 schrieb Tomek Piatek:
>
> output.sprintf("%-32s\t%-s\t%+03.3f\t%+03.3f",
> name, cc, lat, lon);
use name.latin1(), cc.latin1() to get a const char * out of your QStrings
greetingsP.J.
--
[ signature omitted ]
Message 5 in thread
On Friday 01 March 2002 04:38 am, you wrote:
> I'm trying to use QString::sprintf with little luck.
>
> I'm getting some values form a MySQL database. Next I try to format them
> and print them out like so:
>
> QString name = query.value(0).toString();
> QString cc = query.value(1).toString();
> float lat = query.value(2).toDouble();
> float lon = query.value(3).toDouble();
>
> output.sprintf("%-32s\t%-s\t%+03.3f\t%+03.3f",
> name, cc, lat, lon);
You must convert QString objects to "const char*". Use:
name.latin1() or (const char*)name.local8Bit().
--
[ signature omitted ]