1.利用正则表达式拆分字符串
QString str = "[ 1920 , 1080^22/33*55 ]"; QStringList sl = str.mid( 1, str.size() - 2 ).split( QRegExp( "[,*/^]" ) ); qDebug() << sl.first(); //第一个数字 qDebug() << sl.last(); //第二个数字
2.toPointF()使用
#include <QVariant>
#include <QPointF>
QPointF nn( 1.0, 2.0 ); QVariant ss( nn ); QPointF aa = ss.toPointF(); double zx = aa.rx(); double zz = aa.ry();
3.Qt写文件
#include <QCoreApplication> #include<QFile> #include <QDir> #include <QTextStream> int main( int argc, char* argv[] ) { QCoreApplication a( argc, argv ); const QString& robotDir = QString( "C:/Users/zhangxiangbz/Desktop/untitled/" ); QString m_infoFile = QString( "%1/station_list.ini" ).arg( robotDir ); QDir roboDir( robotDir ); if ( !roboDir.exists() ) { roboDir.mkpath( robotDir ); } else { int i = 1; QString b = QString( "%1" ).arg( i, 3, 10, QChar( '0' ) );//整形数据用0补全 001 double L = 1.0; QString c = QString::number( L, 'f', 2 );//位数控制 double x = 3.1, y = 2.2, z = 3.14; int id = 2; QString str = QString( "id:%1 pose:[%2, %3, %4]" ).arg( id ).arg( c ).arg( y ).arg( z ); QFile infoFile( m_infoFile ); if ( infoFile.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) )//插入文件末尾 { QTextStream out( &infoFile ); out << str << " ";//每次写入数据换行 } infoFile.close(); } }