qt中如何判断字符串是否为数字,整数,浮点数?

在 Qt 中,可以使用多种方法来判断字符串是否为数字、整数或浮点数。Qt 提供了一些方便的字符串和数值处理函数,可以帮助你实现这些判断。以下是几种常见的方法:

1. 使用 QRegularExpression

Qt 提供了 QRegularExpression 类,可以用来匹配字符串模式。

判断是否为数字(整数或浮点数)
cpp 复制代码
#include <QRegularExpression>
#include <QString>
#include <QDebug>

bool isNumber(const QString &str) {
    QRegularExpression re("^-?\\d+(\\.\\d+)?$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456.78";
    QString testStr3 = "abc";

    qDebug() << "Is testStr1 a number?" << isNumber(testStr1);  // true
    qDebug() << "Is testStr2 a number?" << isNumber(testStr2);  // true
    qDebug() << "Is testStr3 a number?" << isNumber(testStr3);  // false

    return 0;
}
判断是否为整数
cpp 复制代码
bool isInteger(const QString &str) {
    QRegularExpression re("^-?\\d+$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456";
    QString testStr3 = "456.78";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 an integer?" << isInteger(testStr1);  // true
    qDebug() << "Is testStr2 an integer?" << isInteger(testStr2);  // true
    qDebug() << "Is testStr3 an integer?" << isInteger(testStr3);  // false
    qDebug() << "Is testStr4 an integer?" << isInteger(testStr4);  // false

    return 0;
}
判断是否为浮点数
cpp 复制代码
bool isFloat(const QString &str) {
    QRegularExpression re("^-?\\d+\\.\\d+$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123.45";
    QString testStr2 = "-456.78";
    QString testStr3 = "123";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 a float?" << isFloat(testStr1);  // true
    qDebug() << "Is testStr2 a float?" << isFloat(testStr2);  // true
    qDebug() << "Is testStr3 a float?" << isFloat(testStr3);  // false
    qDebug() << "Is testStr4 a float?" << isFloat(testStr4);  // false

    return 0;
}

2. 使用 QString 的 toInt 和 toDouble 方法

另一种方法是尝试将字符串转换为整数或浮点数,并检查转换是否成功。

判断是否为数字(整数或浮点数)
cpp 复制代码
bool isNumber(const QString &str) {
    bool ok;
    str.toDouble(&ok);
    return ok || str.toInt(&ok) && ok;
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456.78";
    QString testStr3 = "abc";

    qDebug() << "Is testStr1 a number?" << isNumber(testStr1);  // true
    qDebug() << "Is testStr2 a number?" << isNumber(testStr2);  // true
    qDebug() << "Is testStr3 a number?" << isNumber(testStr3);  // false

    return 0;
}
判断是否为整数
cpp 复制代码
bool isInteger(const QString &str) {
    bool ok;
    return str.toInt(&ok) && ok;
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456";
    QString testStr3 = "456.78";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 an integer?" << isInteger(testStr1);  // true
    qDebug() << "Is testStr2 an integer?" << isInteger(testStr2);  // true
    qDebug() << "Is testStr3 an integer?" << isInteger(testStr3);  // false
    qDebug() << "Is testStr4 an integer?" << isInteger(testStr4);  // false

    return 0;
}
判断是否为浮点数
cpp 复制代码
bool isFloat(const QString &str) {
    bool ok;
    return str.toDouble(&ok) && ok;
}

int main() {
    QString testStr1 = "123.45";
    QString testStr2 = "-456.78";
    QString testStr3 = "123";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 a float?" << isFloat(testStr1);  // true
    qDebug() << "Is testStr2 a float?" << isFloat(testStr2);  // true
    qDebug() << "Is testStr3 a float?" << isFloat(testStr3);  // false
    qDebug() << "Is testStr4 a float?" << isFloat(testStr4);  // false

    return 0;
}

这两种方法各有优缺点,使用正则表达式可以精确匹配字符串模式,而使用 QString 的转换方法则更直观和简洁。选择哪种方法取决于你的具体需求和偏好。

相关推荐
TravisBytes2 小时前
面试总结:Qt 信号槽机制与 MOC 原理
qt·面试·职场和发展
@hdd2 小时前
深入理解 Qt 信号与槽机制:原理、用法与优势
qt
为啥不吃肉捏3 小时前
《我在技术交流群算命》(三):QML的Button为什么有个蓝框去不掉啊(QtQuick.Controls由Qt5升级到Qt6的异常)
开发语言·c++·qt·开源
冷白白7 小时前
【Qt】信号和槽机制
服务器·c++·qt
机器视觉知识推荐、就业指导17 小时前
Qt文本高亮显示【QSyntaxHighlighter】功能代码讲解
开发语言·qt
txwtech18 小时前
QT使用QAbstractTableModel 0x8读取访问权限冲突
开发语言·数据库·qt
码农客栈18 小时前
Qt QOpenGLWidget详解
qt
柳鲲鹏18 小时前
QT 5.15.2 开发地图ArcGIS 100.15.6(ArcGIS Runtime SDK for Qt)
开发语言·qt·arcgis
霜雪殇璃20 小时前
(定时器,绘制事件,qt简单服务器的搭建)2025.2.11
运维·服务器·qt·学习
Source.Liu1 天前
【CXX-Qt】1.1 Rust中的QObjects
开发语言·qt·rust