QTextStream question
-
Blushing I still ask you. ;) I have int a=1 and int b=2 -- how to print it as "01.02" using QTextStream manipulators? I tried the following way but without success.
QTextStream cout(stdout); cout.setCodec("UTF-8"); int a=1, b=2; cout << qSetPadChar('0') << qSetFieldWidth(2) << a << "." << b << Qt::endl;
-
@JonB said in QTextStream question:
@qAlexKo Like I said, remove
<< Qt::endl
?Yes, the last cout << Qt:endl is to blame.
int a=1; cout << "test" << Qt::endl; cout << qSetPadChar('0') << qSetFieldWidth(2) << a; //cout << Qt::endl;
If I stream to QTextStream everything is OK
QString qstr; QTextStream qstream(&qstr); int aa=1; qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa; cout << qstr << Qt::endl;
-
@qAlexKo
If your combination ofqSetPadChar('0') << qSetFieldWidth(2)
does not do it I am not sure. Could you show just what output you do get with your code? (Yours probably wouldn't be quite right though, even if it worked on the integers, because your"."
field would also get these, wouldn't it?) -
@qAlexKo
Your reply crossed with my update. At least one of those0
s is (unfortunately) for the.
field. Start by getting rid of that to see what is going on. And another0
may be from the<< Qt::endl
? I think you have those for each<<
. You are getting something like (with spacing so we can read):01 0. 02 0\n
-
@qAlexKo
If/when this does not get you quite what you want, or it's too difficult/fiddly/messy. Since you only want those options against (single digit) integers, but they will like affect everything on the stream, I would useQString::number()
or other ofQString
numeric formatting methods to produce desired number format and send that as a string rather then fiddling withQTextStream
formatting directives. -
@JonB said in QTextStream question:
@qAlexKo Like I said, remove
<< Qt::endl
?Yes, the last cout << Qt:endl is to blame.
int a=1; cout << "test" << Qt::endl; cout << qSetPadChar('0') << qSetFieldWidth(2) << a; //cout << Qt::endl;
If I stream to QTextStream everything is OK
QString qstr; QTextStream qstream(&qstr); int aa=1; qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa; cout << qstr << Qt::endl;
-
-
@JonB said in QTextStream question:
Looks simpler
In the full variant "01.02" it is important to set zero width for "." output.
QString qstr; QTextStream qstream(&qstr); int aa=1, bb=2; qstream << qSetPadChar('0') << qSetFieldWidth(2) << aa << qSetFieldWidth(0) << "." << qSetFieldWidth(2) << bb; cout << qstr << Qt::endl;
-
@qAlexKo said in QTextStream question:
In the full variant "01.02" it is important to set zero width for "." output.
So? I would build a string for the whole of
01.02
, or for each number separately with a"."
field and set the "gap" between fields to zero/empty. Up to you. -
-
@qAlexKo said in QTextStream question:
The variant with cout also works if do this:
Yes, which is why I wrote earlier that persisting to do it this way gets "difficult/fiddly/messy" and that using a
QString
method likenumber()
orarg()
seems so much easier. I can't keep repeating the same thing so I have said my piece now. -
@JoeCFD said in QTextStream question:
std::cout << qPrintable( QString( "%1." ).arg( a, 2, 10, QChar( '0' ) ) ) << qPrintable( QString( "%1" ).arg( b, 2, 10, QChar( '0' ) ) ) << std::endl;
Compare your statement with mine if I define a simple macro:
#define co(width, what) qSetPadChar('0') << qSetFieldWidth(width) << what << qSetFieldWidth(0)Then: "01.02" output is much simpler:
cout << co(2,a) << co(0,'.') <<co(2, b) << Qt::endl;