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. [SOLVED] QLineEdit, setNum(), How to show more than 6 decimal places of a number?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QLineEdit, setNum(), How to show more than 6 decimal places of a number?

Scheduled Pinned Locked Moved General and Desktop
qlineedit
5 Posts 2 Posters 12.6k Views 2 Watching
  • 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.
  • N Offline
    N Offline
    newe12
    wrote on last edited by newe12
    #1

    Hi!

    If I use a QLineEdit for the "output" of a double number, it does not show more than 6 decimal places.

    QRegExpValidator or QDoubleValidator can be used to set a validator to "input" a number in QLineEdit very well. Even more than 6 digits!

    If I use:

    lineedit->setMaxLength(2);  // then the lineEdit can be truncated to max. 2 characters to be shown;
    lineedit->setMaxLength(10); // then the lineEdit displays max. 10 characters, but stops after the 6th decimal place 
    //(e.g. 0.123456)
    

    Is there a possibility to tell QLineEdit to display up till 10 decimal places (e.g. 0.0123456789 or 1.0123456789 or...)?

    Any suggestions? Please share!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QLineEdit only displays text (QString).
      How are you setting the number to display? The truncation happens when you convert the number to text, not in the QLineEdit.

      1 Reply Last reply
      1
      • N Offline
        N Offline
        newe12
        wrote on last edited by
        #3

        Hi @Chris-Kawa!

        Thanks!!!

        I nearly solved it with your hint:

         lineedit->setText(resultString.setNum(result,'g', 9)); // it displays 8 decimal places
         lineedit->setText(resultString.setNum(result,'g', 12)); // it displays also 8 decimal places
        

        Therefore, with

        ...,'g', 9));
        

        it displays two more decimal places. But, it cuts it off after 8 even for

        ...,'g', 12));
        

        I am quite happy with this solution. Nevertheless, if someone has got a suggestion for displaying 10 decimal places, please post!

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          As described in the documentation you are interested in the 'f' format with the precision set. The precision param in the 'g' format specifies number of significant digits, not the number of digits after decimal point.

          double num = 0.123456789123456789;
          lineedit->setText(QString::number(num, 'f', 10));
          
          1 Reply Last reply
          1
          • N Offline
            N Offline
            newe12
            wrote on last edited by
            #5

            Solved! It is working now!

            I had set:

            lineedit->setMaxLength(10);
            

            before.

            This has been limiting:

            ...,'g', 10));
            

            After I have removed it, it works!

            @Chris-Kawa thanks for the other post, but

            'g', 10
            

            works fine now! Thanks anyway!

            1 Reply Last reply
            0

            • Login

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