In order to adapt to multiple data types, QString::arg has multiple overloaded versions.
1、QString::arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' '))
Parameter 2 is the minimum width, if it is less than this width, use parameter three to complete:
#define debug qDebug()<<
int main(int argc, char *argv[])
{
debug QString("hi,my name is %1").arg(QString("张三"),10,QChar{'k'});
}
Because there are too many overloaded versions of arg, the QChar type of parameter 3 must be clearly written.
If parameter 2 is negative, then the generated replacement text is left-justified:
2. QString::arg( qlonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char('')) ( qlonglong is long long type)
Parameters 2 and 4 are the same as above, and parameter 3 is to specify the base used to convert an integer to a character string.
#define debug qDebug()<<
int main(int argc, char *argv[])
{
debug QString("我有 %1 元人民币。").arg(qlonglong{99999},13,10,QChar{'w'});
}
Hexadecimal:
Other types of integer and char arg overloading are similar to this form, and they also have 4 parameters. The first parameter is different, and the last 3 parameters and usage are the same.
3、QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' '))
Parameter 2 and parameter 5 are the same as above.
Floating-point numbers in c/c++ can be expressed in scientific notation; they will be rounded up after the significant digits are exceeded. Parameter 3 indicates the format:
- e: use scientific notation, use e for scientific notation
- E: Use scientific notation, use E for scientific notation
- f: do not use scientific notation
- g: Use the succinct one of e and f
- G: Use the succinct one of E and f
Parameter 4 represents the precision, that is, a few decimal places after the decimal point.
4、QString::arg(const QString &a1, const QString &a2)
There are multiple replacement strings in an arg.
This type is only suitable for strings, it won't work if you change to other types: