Skip to content

Commit 20def01

Browse files
committed
use QRegularExpression to be compatible with Qt6
1 parent 6833920 commit 20def01

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

examples/CalcQt/src/Calculator.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#include "Calculator.hpp"
22

3+
#include <QRegularExpression>
4+
35
QString Calculator::calculate(const QString& expression) const {
46
int result = 0;
57
char operation = '+';
6-
QRegExp regexp("(\\d+)");
7-
int pos = 0;
8-
while ((pos = regexp.indexIn(expression, pos)) != -1) {
9-
int value = regexp.cap(1).toInt();
8+
const QRegularExpression regexp("(\\d+)");
9+
QRegularExpressionMatchIterator matches = regexp.globalMatch(expression);
10+
11+
while (matches.hasNext()) {
12+
const auto match = matches.next();
13+
const int value = match.captured(1).toInt();
14+
1015
switch (operation) {
1116
case '+':
1217
result += value;
@@ -15,11 +20,13 @@ QString Calculator::calculate(const QString& expression) const {
1520
result -= value;
1621
break;
1722
}
18-
pos += regexp.matchedLength();
23+
24+
const int pos = match.capturedEnd();
1925
if (pos < expression.length()) {
2026
operation = expression.at(pos).toLatin1();
2127
}
2228
}
29+
2330
return QString::number(result);
2431
}
2532

0 commit comments

Comments
 (0)