转发:http://blog.csdn.net/tgzjz/article/details/45074237
在由int, float, double 这样的基本数据类型转换为QString的方法大致如下:
以int为例:
以float为例,或者:
在工作中,上述转换用的不是很多,大多数都是由QString类型转换到基本的数据类型使用的比较多,由于Qt的API有方法,所以用起来很简单,方法大致如下:
以转为float, double为例:
上面这种方法比较适合与科学计数法的表示:
需要注意的是,经常QString的内容转换成对应的基本数据类型是建立在QString的内容正确性的基础上进行的,
假设QString的内容是一个基本数据类型无法识别的情况下会导致转换错误,为了提高程序的健壮性,我们通常会在转换时加一个bool型的变量用于判断转换是否成功,用法如下:
以转int为例:
转换为QString后就可以进行QString与QDateTime进行转换了,转换的方法如下:
》》 QDateTime 转换为 QString
函数原型:QString QDateTime::toString ( Qt::DateFormat format = Qt::TextDate ) const
》》QString 转换为 QDateTime
函数原型:QDateTime QDateTime::fromString ( const QString & string, const QString & format ) [static]