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
QtWS25 Last Chance

Assigning LineEdit input value to a variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.6k 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 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.
    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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

        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.
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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.

        mrjjM 1 Reply Last reply
        2
        • JonBJ JonB

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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 :)

            mrjjM 1 Reply Last reply
            0
            • JonBJ JonB

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

              • Login

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