Use regular expressions:
QString s = "(2, 5.3, 1e8)";
QRegExp regexp("\\(([^,]+), ([^,]+), ([^)]+)\\)");
if (regexp.exactMatch(s)) {
double x = regexp.capturedTexts()[1].toDouble();
double y = regexp.capturedTexts()[2].toDouble();
double z = regexp.capturedTexts()[3].toDouble();
qDebug() << x << y << z;
}
Output: 2 5.3 1e+08
Be aware that arg
can produce a number representation with comma separator (e.g. '3,14') on systems with certain locales. Parsing this representation would be problematic. So you should use QLocale::c().toString(number)
to perform system independent conversion.