QTextStream question
-
@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;