myString=ui->total_lineEdit->text ();
myString.toDouble ();
ui->total_lineEdit->setText (myString);
QString myString;
myString=ui->total_lineEdit->text ();
myString.toDouble ();
ui->total_lineEdit->setText (myString);
To copy to clipboard, switch view to plain text mode
How is this a "solution"? Do you understand that your code does absolutely nothing to the string or the line edit contents? Line 1 declares a temporary QString instance. Line 2 retrieves the text into that string. Line 3 converts the string contents to a double, then throws the double away. Line 4 puts the unmodified string back into the line edit. Obviously, you are confused.
So what I am assuming you want to do is something like this: The user enters a string "1234.5" and you want it displayed with two digits of precision after the user is done: "1234.50". Then in your slot that handles the QLineEdit::editingFinished() signal, you could do something like this:
void MyWidget::onEditingFinished()
{
QString myString
= ui
->lineEdit
->text
();
bool didItWork;
double dValue = myString.toDouble( &didItWork );
if ( didItWork ) // didItWork would be false if the string couldn't be converted. What if the user didn't enter a number?
{
ui->lineEdit->setText( myNewString );
}
}
void MyWidget::onEditingFinished()
{
QString myString = ui->lineEdit->text();
bool didItWork;
double dValue = myString.toDouble( &didItWork );
if ( didItWork ) // didItWork would be false if the string couldn't be converted. What if the user didn't enter a number?
{
QString myNewString = QString( "%1" ).arg( dValue, 7, 'f', 2 );
ui->lineEdit->setText( myNewString );
}
}
To copy to clipboard, switch view to plain text mode
Look at the documentation for the QString::arg() version that takes a double as the first argument for an explanation of the formatting arguments.
Bookmarks