QString与其它类型的转换

1. QString与int、double等类型的转换

QString转int、double:

QString intStr = "10";
int intNum = intStr.toInt();
QString doubleStr = "11.11";
double doubleNum = doubleStr.toDouble();

int、double转QString:

int intNum = 10;
QString intStr = QString("%1").arg(intNum);
double doubleNum = 11.11;
QString doubleStr = QString("%1").arg(doubleNum);

2. QString与QDateTime的转换

QString转QDateTime:

QString timeStr = "2010-10-10 10:10:10";
QDateTime time = QDateTime::fromString(timeStr, "yyyy-MM-dd hh:mm:ss");

QDateTime转QString:

QDateTime time = QDateTime(QDate(2011, 11, 11), QTime(11, 11, 11));
QString timeStr = time.toString("yyyy-MM-dd hh:mm:ss");

你可能感兴趣的:(QString与其它类型的转换)