How to get float from QtextEdit?
I have a QTextEdit which takes input of floats, 4 floats per row.
I need to write all of this floats to an array ( just for example)
i tried to get it by
but it works only for integers and only if they are positive;
i guess that there should be simpler solution than doing it by regular expressions.
Why using QTextEdit for that ?
What about a widget made with several QDoubleSpinBox ?
It depends what you try to achieve.
It might be better to go for a space only or use the whitespace example from documentation.
Floats are not only composed of digits,but also of decimal points and a character for exponent. That is causing the problem for you.
@koahnig i actually tried to go for a space only, but there is a problem that when new line start there is no space and last number from first line is merged with first of second;
My input is like
-1.2 2 3.5 3 1 2 3 4 34 5.7 3 2
and when getting 4th number it returns 31 because there is no space.
I can not use QDoubleSpinBox because number of rows should be from 1 to 1000;
In that case wouldn't a QTableWidget be more fitting ? Especially since you input rows of numbers.
While you should follow @SGaist 's suggestions as I think they are guiding you in the correct direction, for referece, you can use this pattern in a global match with QRegularExpression to extract the floating point numbers from the string. The result: https://regex101.com/r/gusSIZ/1
temp.replace ("\n", " ");
Getting floats by
Everything seems to be working right
In that case, there's no need to involve
QRegExp, use the string version of split.
In any case, you should replace your use of
QRegularExpression. The former is deprecated in Qt 5.