我有一个包含"-3.5“的'QString‘,但是如果我试图使用'toInt’方法将其转换为整数,它将返回0。为什么?
QString strTest = "-3.5";
int intTest = strTest.toInt();
qDebug() << intTest;
intTest是0吗?
发布于 2016-10-02 04:39:04
与标准库中的std::stoi
和流不同,Qt字符串要求整个字符串是执行转换的有效整数。您可以使用toDouble
作为解决方案。
您还应该使用可选的ok
参数来检查错误:
QString strTest = "-3.5";
book ok;
int intTest = strTest.toInt(&ok);
if(ok) {
qDebug() << intTest;
} else {
qDebug() << "failed to read the string";
}
发布于 2016-10-02 04:41:03
https://stackoverflow.com/questions/39817068
复制