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. Assigning LineEdit input value to a variable
Forum Update on Monday, May 27th 2025

Assigning LineEdit input value to a variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.7k Views
  • 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.
  • F Offline
    F Offline
    FamaFrench
    wrote on 13 Feb 2021, 22:57 last edited by
    #1

    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.
    J 1 Reply Last reply 14 Feb 2021, 08:15
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 Feb 2021, 08:14 last edited by mrjj
      #2

      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;

      1 Reply Last reply
      0
      • F FamaFrench
        13 Feb 2021, 22:57

        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.
        J Online
        J Online
        JonB
        wrote on 14 Feb 2021, 08:15 last edited by JonB
        #3

        @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.

        M 1 Reply Last reply 14 Feb 2021, 08:16
        2
        • J JonB
          14 Feb 2021, 08:15

          @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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 14 Feb 2021, 08:16 last edited by
          #4

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

          J 1 Reply Last reply 14 Feb 2021, 08:18
          0
          • M mrjj
            14 Feb 2021, 08:16

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

            J Online
            J Online
            JonB
            wrote on 14 Feb 2021, 08:18 last edited by
            #5

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

            M 1 Reply Last reply 14 Feb 2021, 08:22
            0
            • J JonB
              14 Feb 2021, 08:18

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 14 Feb 2021, 08:22 last edited by
              #6

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

              1 Reply Last reply
              0

              1/6

              13 Feb 2021, 22:57

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved