Solved QLineEdit Truncated Data
-
I am currently updating and editing a qt application that used 5.3.1 and now I'm using 5.5. We were using QLineEdit (with a validator) to allow a user to enter double values. In the 5.5 version the data gets truncated. Is there something I can do to get the full value? We are using latitudinal/longitudinal values and truncating throws off location data. If the user enters: -98.48894016 as soon as they hit enter, the data becomes -94.8890000.
-
Hi and welcome to devnet,
Can you show how you setup your QLineEdit ?
By the way, why not use a QDoubleSpinBox ?
-
@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. -
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(); }
-
@kmbar2013
Have you tried changingqDebug()
's floating point precision before outputting the double value? For example:
qDebug() << qSetRealNumberPrecision(12);
-
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.
-
@kmbar2013 said:
Ok. It might be so, I was referring to your second post, asqDebug
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 theQString::toDouble
function so you would know if the conversion has gone alright. Let us know how your tests turn out.Kind regards.
-
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?
-
@kmbar2013
I already mentioned thatqDebug
will round the value on output, did you try setting the precision forqDebug
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 thetoDouble()
method returns the correct value andqDebug
just outputs the rounded version. -
@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.
-
@kmbar2013
I'm glad it worked out.PS qDebug() << qSetRealNumberPrecision(12); didn't change the output.
This is certainly strange.