QString QString::arg(long a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString QString::arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
1、QString格式化arg前面自动补0
2、进制转换
QString ss = QString("十进制64转为十六进制%1").arg(64, 0, 16);
//0代表宽度,16表示16进制,空位补零
3、数字转QString,并保留小数点位数
//整数转QString
int num1=123;
QString str1 = QString::number(num1);
qDebug()<<"str1:"<<str1<<endl;
//小数转QString,保留两位小数
double num2 = 123.456789;
QString str2=QString::number(num2,'f',2);
qDebug()<<"str2:"<<str2<<endl;
4、数字转QString,使用asprintf
ui->comboBox_pulse->addItem(QString::asprintf("%d", PULSE_LOW));
ui->lineEdit_x->setText(QString::asprintf("%.3f", m_stuMotionPos.X));
5、QString中的arg还可以对浮点类型的数字按照一定的精度来转换
QString QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const
Format Meaning
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g use e or f format, whichever is the most concise
G use E or f format, whichever is the most concise
QString ss = QString("delta %1").arg(123.45678, 0, 'g', 6);
结果是ss="delta 123.456"。浮点类型格式化为只显示六位有效数字,并且是采用四舍五入的方式。
6、QString/string/wstring的转换
QString::fromStdString()
QString::fromStdWString()
str.toStdString()
QObject::tr("位置超出范围").toStdWString()