Qt6 topics merged with General

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)

    1. As you can tell, I am quite lost. All help will be greatly appreciated.

  • Lifetime Qt Champion

    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;

  • @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 a QDoubleSpinBox widget, as that will limit.validate input and return the value as a double instead 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 on QDoubleSpinBox just as much as on QLineEdit.

  • Lifetime Qt Champion

    Oh I miss the float thing :)
    Then converting to an int wont make him happy

  • @mrjj
    Wait to see, it may not mean that at all! It might be to do with a "floating window", I don't know :)

  • Lifetime Qt Champion

    Also if that float is money, he might want to look at QLocale
    to get the separators right.

Log in to reply