Assigning LineEdit input value to a variable
-
Okay, here's what I'm attempting to accomplish.
1.Have the user enter a value into a QLineEdit object (Float only)
2.Save that input to a variable
My problem:
1.is editingFinished(); an appropriate function for saving input in LineEdit box?So far, my code is:
void MainWindow::on_lineEdit_4_editingFinished() { int CouponRate; QString Coup = ui->CPR->text(); }(CPR is the object name of the lineedit)
- As you can tell, I am quite lost. All help will be greatly appreciated.
-
Hi and welcome to the forums
Well the editingFinished, is emitted when the user press return/enter or simply leave the LineEdit so if that fit how they should use it, its
ok place to do it. ( most use cases fit this use)void MainWindow::on_lineEdit_4_editingFinished() { QString Coup = ui->CPR->text(); float CouponRate = Coup.ToFloat(); // get it as float }Note that are other ToXX for other types.
Also, note that ToXXX can take a bool to tell if the convert went ok or not.
That can be used to validate user input; -
Okay, here's what I'm attempting to accomplish.
1.Have the user enter a value into a QLineEdit object (Float only)
2.Save that input to a variable
My problem:
1.is editingFinished(); an appropriate function for saving input in LineEdit box?So far, my code is:
void MainWindow::on_lineEdit_4_editingFinished() { int CouponRate; QString Coup = ui->CPR->text(); }(CPR is the object name of the lineedit)
- As you can tell, I am quite lost. All help will be greatly appreciated.
@FamaFrench said in Assigning LineEdit input value to a variable:
Have the user enter a value into a QLineEdit object (Float only)
What does "Float only" mean? If you mean a floating point value (
12.34) you would be best using aQDoubleSpinBoxwidget, as that will limit.validate input and return the value as adoubleinstead of you doing conversion work.editingFinished()is a perfectly good signal to use to detect end of editing and ready to save. It is available onQDoubleSpinBoxjust as much as onQLineEdit. -
@FamaFrench said in Assigning LineEdit input value to a variable:
Have the user enter a value into a QLineEdit object (Float only)
What does "Float only" mean? If you mean a floating point value (
12.34) you would be best using aQDoubleSpinBoxwidget, as that will limit.validate input and return the value as adoubleinstead of you doing conversion work.editingFinished()is a perfectly good signal to use to detect end of editing and ready to save. It is available onQDoubleSpinBoxjust as much as onQLineEdit. -
@mrjj
Wait to see, it may not mean that at all! It might be to do with a "floating window", I don't know :)