QT开发中文网

Qt C++ QLineEdit 获取当前输入框 (001)所有数字包含0

要获取QLineEdit中输入框(001)中的所有数字(包含0),你可以稍微修改之前给出的代码。将正则表达式改为 \\d*,即匹配零个或多个数字。以下是修改后的示例代码:

#include <QApplication>
#include <QRegularExpression>
#include <QDebug>
#include <QLineEdit>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QLineEdit lineEdit;
    lineEdit.setText("(001)");

    QString text = lineEdit.text();

    QRegularExpression regex("\\d*"); // 正则表达式匹配零个或多个数字

    QRegularExpressionMatchIterator it = regex.globalMatch(text);
    
    QStringList numbers;

    while (it.hasNext()) {
        QRegularExpressionMatch match = it.next();
        QString number = match.captured(0);
        if (!number.isEmpty()) {
            numbers << number;
        }
    }

    qDebug() << "Numbers: " << numbers;

    return app.exec();
}

在上述代码中,我们使用了正则表达式 \\d* 来匹配零个或多个数字,然后在循环中检查捕获到的子字符串是否为空,如果不为空,则将其添加到字符串列表 numbers 中。

这样就可以获取包含0的所有数字了。请根据你的实际需要进行相应的修改。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=8931

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?