要获取QLineEdit中输入框(001)中的所有数字,你可以使用正则表达式来提取数字。在Qt C++中,可以使用QRegularExpression类来实现这个功能。下面是一个示例代码:
#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();
numbers << match.captured(0);
}
qDebug() << "Numbers: " << numbers;
return app.exec();
}
在上面的代码中,我们创建了一个QLineEdit对象 lineEdit
并设置了文本为 (001)
。然后我们使用 text()
函数获取文本内容,并将其存储在变量 text
中。
接下来,我们定义了一个正则表达式 \\d+
来匹配一个或多个数字。然后通过调用 globalMatch()
函数和迭代器 it
来查找匹配项。
最后,在循环中使用 captured(0)
函数获取捕获到的第一个子字符串(即匹配到的数字),并将其添加到字符串列表 numbers
中。
你可以根据需要修改以上示例代码以适应你的具体需求。
内容由零声教学AI助手提供,问题来源于学员提问