I'm trying to write a method that changes font-size in a html string. When I click "minus" button, I multiply the current size with 0.8, when I click "plus" button, I multiply the current size with 1.25!
The problem is at the htmlStyle.replace(pos, length, newString);
line, at the 'newString' parameter. I figured out, that if I pass a string which contains a number in string format which was multiplied with for example 1.2, that's ok. But if I pass a number in string format which was multiplied with 1.22 or 1.34 or any floating point number with two decimal places, it causes an infinite loop and the app crashes.
I really don't get what's the problem here, since these are strings, and problem occurs after the 'newString' was calculated successfully.
Any ideas? I tried to figure this for 2 days, but I'm still clueless...
Here's the full method:
QString RichTextSize::setSizes(QString htmlStyle, float multiplier)
{
QRegExp rx("(\\d+)pt");
int pos = 0, length = 0;
QString newString;
while ((pos = rx.indexIn(htmlStyle, pos)) != -1) {
length = rx.cap(1).length();
newString.setNum(rx.cap(1).toInt()*multiplier);
htmlStyle.replace(pos, length, newString);
pos += rx.matchedLength();
}
return htmlStyle;
}
EDIT: I got a notification about a possible duplicate question. Well, my problem is not floating point math, but it's realted to float somehow. Floating point operations work without problems. When the trouble happens, that's with a float number converted to string. It converts successfully, but in the 'replace()' method it causes infinite loop. It's realted to floating point decimal places somehow, but since that operation works and problem is with a string converted from that number, I don't understand what happens here.