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 的转换方法则更直观和简洁。选择哪种方法取决于你的具体需求和偏好。

相关推荐
肥or胖10 小时前
【FFmpeg 快速入门】本地播放器 项目
开发语言·qt·ffmpeg·音视频
小灰灰搞电子16 小时前
Qt Quick 粒子系统详解
开发语言·qt
暴躁茹18 小时前
Qt 将触摸事件转换为鼠标事件(Qt4和Qt5及以上版本)
开发语言·qt·计算机外设
大专生学编程20 小时前
QT简介和QT环境搭建
c++·qt
feiyangqingyun20 小时前
Qt/C++开发监控GB28181系统/视频点播没有ssrc问题的处理/兼容各种设备和应用场景需求
c++·qt·gb28181
小堃学编程1 天前
QT跨平台应用程序开发框架(10)—— Qt窗口
开发语言·qt
轩宇^_^1 天前
Qt CMake 学习文档
数据库·qt·学习
小徐不徐说1 天前
QT技巧之快速搭建串口收发平台
开发语言·c++·qt·串口·软件构建·个人开发·通信
天堂陌客1 天前
QT 交叉编译环境下,嵌入式设备显示字体大小和QT Creator 桌面显示不一致问题解决
开发语言·qt·字库
小堃学编程1 天前
QT跨平台应用程序开发框架(9)—— 容器类控件
开发语言·qt