Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to get float from QtextEdit?

    General and Desktop
    4
    8
    2186
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      Ololoev999 last edited by

      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

      ui->textEdit->toPlainText().split(QRegExp("\\D+"))[i]).toFloat();
      

      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.

      K 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Why using QTextEdit for that ?

        What about a widget made with several QDoubleSpinBox ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 4
        • K
          koahnig @Ololoev999 last edited by koahnig

          @Ololoev999

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          O 1 Reply Last reply Reply Quote 3
          • O
            Ololoev999 @koahnig last edited by Ololoev999

            @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.
            @SGaist
            I can not use QDoubleSpinBox because number of rows should be from 1 to 1000;

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              In that case wouldn't a QTableWidget be more fitting ? Especially since you input rows of numbers.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 1
              • V
                VRonin last edited by VRonin

                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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                O 1 Reply Last reply Reply Quote 2
                • O
                  Ololoev999 @VRonin last edited by Ololoev999

                  @VRonin @SGaist
                  Appreciate your help.
                  I have done it easier.
                  i made a temp QString where QtextEdit.toPlainText() goes
                  and replaced all "\n" with " " using

                  temp.replace ("\n", " ");
                  

                  Getting floats by

                  temp.split(QRegExp(" "))[i].toFloat();
                  

                  Everything seems to be working right

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 QRegExp with QRegularExpression. The former is deprecated in Qt 5.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 3
                    • First post
                      Last post