1.分割字符串QString到QStringList
接收数据 "%R1P,0,0:0,4.455493696036638,2.050167981242538,0.000002354383469,20981534,0.000099052486343,-0.000021364"
//假设数据格式是固定的并且以,分割
QStringList parts=dataString.split(",");
//检查是否有足够数据部分
if(parts.size()>=5){
//提取第五和第六部分(索引为4和5),他们包含弧度值
QString value1Str=parts[3].trimmed();//trimmed()去除前后的空格
QString value2Str=parts[4].trimmed();
qDebug()<<"1:"<<value1Str;
qDebug()<<"2:"<<value2Str;
// 尝试将字符串转换为浮点数
bool ok1 = false;
bool ok2 = false;
double value1 = value1Str.toDouble(&ok1);
double value2 = value2Str.toDouble(&ok2);
// qDebug()<<"1:"<<value1;
// qDebug()<<"2:"<<value2;
// 将弧度转换为度
double degrees1 = value1 * (180.0 / M_PI);
double degrees2 = value2 * (180.0 / M_PI);
// 计算度、分、秒
int wholeDegrees1 = static_cast<int>(degrees1);
double minutes1 = (degrees1 - wholeDegrees1) * 60;
int wholeMinutes1 = static_cast<int>(minutes1);
double seconds1 = (minutes1 - wholeMinutes1) * 60;
int wholeDegrees2 = static_cast<int>(degrees2);
double minutes2 = (degrees2 - wholeDegrees2) * 60;
int wholeMinutes2 = static_cast<int>(minutes2);
double seconds2 = (minutes2 - wholeMinutes2) * 60;
// 格式化输出
QString dms1 = QString("%1°%2'%3\"").arg(wholeDegrees1).arg(wholeMinutes1, 2, 10, QChar('0')).arg(seconds1, 5, 'f', 1);
QString dms2 = QString("%1°%2'%3\"").arg(wholeDegrees2).arg(wholeMinutes2, 2, 10, QChar('0')).arg(seconds2, 5, 'f', 1);
// 打印和显示转换后的度、分、秒
qDebug() << "水平角:" << dms1;
qDebug() << "垂直角:" << dms2;
// 在文本编辑框中显示提取并转换后的数据
ui->TeditRecv->append(QString("水平角: %1").arg(dms1));
ui->TeditRecv->append(QString("垂直角: %1").arg(dms2));