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. QLineEdit Truncated Data
Qt 6.11 is out! See what's new in the release blog

QLineEdit Truncated Data

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.1k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Can you show how you setup your QLineEdit ?

    By the way, why not use a QDoubleSpinBox ?

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

    K 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

      Can you show how you setup your QLineEdit ?

      By the way, why not use a QDoubleSpinBox ?

      K Offline
      K Offline
      kmbar2013
      wrote on last edited by
      #3

      @SGaist Thanks for the reply.
      I didn't use a QDoubleSpinBox because I honestly didn't think about it because I only think to use a spin box for small values like date/time. I subclassed QItemDelegate to use in a custom QTreeView.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kmbar2013
        wrote on last edited by
        #4

        I have an example without a validator but generally the same issue, see the qDebug output when using this example.
        MainWindow.ui has a QLineEdit lineEdit and two QLabels--label and label_2. lineEdit is initialized to 38.123456789, label is initialized to "User Entered" and label_2 is what the user will enter based on signal/slot in the following code. qDebug() will display 38.1235.

        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            connect(ui->lineEdit, &QLineEdit::editingFinished, this, &MainWindow::updateLabel);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::updateLabel()
        {
            ui->label_2->setText(ui->lineEdit->text());
            qDebug() << ui->lineEdit->text().toDouble();
        }
        
        kshegunovK 1 Reply Last reply
        0
        • K kmbar2013

          I have an example without a validator but generally the same issue, see the qDebug output when using this example.
          MainWindow.ui has a QLineEdit lineEdit and two QLabels--label and label_2. lineEdit is initialized to 38.123456789, label is initialized to "User Entered" and label_2 is what the user will enter based on signal/slot in the following code. qDebug() will display 38.1235.

          MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              connect(ui->lineEdit, &QLineEdit::editingFinished, this, &MainWindow::updateLabel);
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::updateLabel()
          {
              ui->label_2->setText(ui->lineEdit->text());
              qDebug() << ui->lineEdit->text().toDouble();
          }
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #5

          @kmbar2013
          Have you tried changing qDebug()'s floating point precision before outputting the double value? For example:
          qDebug() << qSetRealNumberPrecision(12);

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kmbar2013
            wrote on last edited by kmbar2013
            #6

            @kshegunov

            I'm not sure how that would help. I need to store the data, but once stored it's already truncated. If I did:

            double myDouble = ui->lineEdit->text().toDouble();
            

            and store myDouble in a vector somewhere else, the data will be stored incorrectly.

            kshegunovK 1 Reply Last reply
            0
            • K kmbar2013

              @kshegunov

              I'm not sure how that would help. I need to store the data, but once stored it's already truncated. If I did:

              double myDouble = ui->lineEdit->text().toDouble();
              

              and store myDouble in a vector somewhere else, the data will be stored incorrectly.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #7

              @kmbar2013 said:
              Ok. It might be so, I was referring to your second post, as qDebug might truncate the data itself. Make sure you have not any input masks and validators attached to the line edit and that the maxLength set for the control can accommodate the number of digits. I also suggest writing the string you get from the line edit to the debug stream so you can be sure that you get the string representation properly. After you are sure that the string is returned ok, you can start attaching your masks/validators and diagnose which one might be causing the problem. Also it is possible to pass a pointer to bool to the QString::toDouble function so you would know if the conversion has gone alright. Let us know how your tests turn out.

              Kind regards.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kmbar2013
                wrote on last edited by
                #8

                In my simple test case you can see that if the user types in the lineEdit 38.987654321, hits enter with the following code in my slot:

                qDebug() << ui->lineEdit->text()
                qDebug() << ui->lineEdit->text().toDouble()
                

                will result in

                38.987654321
                38.9877
                

                This actually happens when I use 5.3.1 or 5.5, so I'm not sure what the problem is in my simple case.

                Even trying:

                qDebug() << QString::number(ui->lineEdit->text().toDouble(), 'f', 13);
                qDebug() << QString::number(ui->lineEdit->text().toDouble(), 'f', 13).toDouble();
                
                

                will result in

                38.9876543210000
                38.9877
                

                Saving this data to a double variable will still result with the truncated value. What am I missing?

                1 Reply Last reply
                0
                • kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #9

                  @kmbar2013
                  I already mentioned that qDebug will round the value on output, did you try setting the precision for qDebug first (see my previous post)? Did you inspect the value of the double variable in the debugger? The last two lines just harden my suspicion that the toDouble() method returns the correct value and qDebug just outputs the rounded version.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kmbar2013
                    wrote on last edited by
                    #10

                    @kshegunov
                    Thanks, suggesting I look at the value in the debugger helped me pinpoint where the problem was occuring since I could see in the debugger that the double variable did have the correct data. I have to pass this data to javascript code and needed to format the data such that:

                    QString("doSomething(%1, %2)").arg(lat, 0, 'f', 13).arg(lng, 0, 'f', 13);
                    

                    where lat/lng are the double values.

                    PS qDebug() << qSetRealNumberPrecision(12); didn't change the output.

                    kshegunovK 1 Reply Last reply
                    0
                    • K kmbar2013

                      @kshegunov
                      Thanks, suggesting I look at the value in the debugger helped me pinpoint where the problem was occuring since I could see in the debugger that the double variable did have the correct data. I have to pass this data to javascript code and needed to format the data such that:

                      QString("doSomething(%1, %2)").arg(lat, 0, 'f', 13).arg(lng, 0, 'f', 13);
                      

                      where lat/lng are the double values.

                      PS qDebug() << qSetRealNumberPrecision(12); didn't change the output.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #11

                      @kmbar2013
                      I'm glad it worked out.

                      PS qDebug() << qSetRealNumberPrecision(12); didn't change the output.

                      This is certainly strange.

                      Read and abide by the Qt Code of Conduct

                      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