How to use QString.arg
-
Dear all,
runningDate is a QDateTime object with the date value of 12.Aug.2018. With the following command
cout << QString("%1").arg(runningDate.date().month(), 2, '0').toStdString() << endl;
I expect the output "08", since fieldwidth is 2 and fillchar is 0. But the output is "8". How can integer 8 be converted into "08"?
Weichao -
Actually there are several static number() functions. But none of them contains fieldwidth and fillchar.
-
@Weichao-Wang
I believe you are trying to use this overload:
http://doc.qt.io/qt-5/qstring.html#arg-4
QString QString::arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
So I make your parameters wrong. I think you're passing
'0'
as thebase
(so base 48!) and therefore' '
as thefillChar
, so I would guess you're actually seeing<space>8
as the output?Try:
QString("%1").arg(runningDate.date().month(), 2, 10, '0').toStdString()
?
-
Now the output is 8.000000000... (some 30 '0').
-
In the documentation we can find
QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
It contains only 3 parameters.
I'm using Qt 4.6. -
@Weichao-Wang why reinventing the wheel?
cout << runningDate.toString("MM").toStdString() << endl;
-
Now I've changed the line into following and the output is 08:
cout << QString("%1").arg(QString::number(runningDate.date().month()), 2, '0').toStdString() << endl;
I miss something like:
static QString QString::number(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' '))
which would convert an interger into a string with desired length and fillchar without creating a QString object. -
@Weichao-Wang said in How to use QString.arg:
In the documentation we can find
QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
It contains only 3 parameters.
I'm using Qt 4.6.Your code reads:
QString("%1").arg(runningDate.date().month(), 2, '0')
QDate::date().month()
returns anint
not aQString
. So how do you figure it is calling your overload withconst QString & a
? -
@J.Hilk
this is ingenious! Thank you!
Actually I need "20180803" for 3.Aug.2018. With date.toString("yyyyMMdd") I've solved the problem with one stroke.
But for a normal integer i can we only use the following or is there a simpler conversion?
QString("%1").arg(QString::number(i), 2, '0' ) -
@JonB said in How to use QString.arg:
@Weichao-Wang said in How to use QString.arg:
In the documentation we can find
QString QString::arg ( const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) const
It contains only 3 parameters.
I'm using Qt 4.6.Your code reads:
QString("%1").arg(runningDate.date().month(), 2, '0')
QDate::date().month()
returns anint
not aQString
. So how do you figure it is calling your overload withconst QString & a
?Yes, I've noticed the problem and changed it to
QString("%1").arg(QString::number(i), 2, '0') -
@Weichao-Wang like @JonB said all overloads of arg that accept an int want a base to convert it to string
QString("%1").arg(QString::number(i), 2, '0' );
==
QString("%1").arg(i, 2, 10, QChar('0') );