Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get float from QtextEdit?

How to get float from QtextEdit?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.0k Views
  • 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 Offline
    O Offline
    Ololoev999
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      4
      • O Ololoev999

        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 Offline
        K Offline
        koahnig
        wrote on last edited by koahnig
        #3

        @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
        3
        • K 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.

          O Offline
          O Offline
          Ololoev999
          wrote on last edited by Ololoev999
          #4

          @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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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
              2
              • VRoninV 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

                O Offline
                O Offline
                Ololoev999
                wrote on last edited by Ololoev999
                #7

                @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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  3

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved