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. QLineEdit autofill
Qt 6.11 is out! See what's new in the release blog

QLineEdit autofill

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.9k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by
    #1

    Hello,

    i would like to know how to automatically correct the numerical value upon the input.
    let's say, i type 10K in the QLineEdit field, but on the back i want this number autocratically translated into 10000.

    aswel if i type 1N (Nano), that it would adjust the value to 0.000001.

    would what the procedure?

    regards

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Something like:

      #include <QLineEdit>
      #include <QValidator>
      #include <QRegularExpression>
      #include <QApplication>
      
      class FixingValidator : public QDoubleValidator{
          Q_DISABLE_COPY(FixingValidator)
      public:
          using QDoubleValidator::QDoubleValidator;
          State validate(QString & input, int & pos) const override{
              const QRegularExpressionMatch res = vaidRegExp.match(input);
              if(!res.hasMatch())
                  return Invalid;
              QString nemericPart = res.captured(1);
              if(res.lastCapturedIndex()==1)
                  return QDoubleValidator::validate(nemericPart,pos);
              else
                  return QDoubleValidator::validate(nemericPart,pos) == Invalid ? Invalid : Intermediate;
          }
          void fixup(QString &input) const override{
              const QRegularExpressionMatch res = vaidRegExp.match(input);
              if(!res.hasMatch())
                  return;
              if(res.capturedRef(2).isEmpty())
                  return;
              double baseNumber = locale().toDouble(res.capturedRef(1));
              switch(res.capturedRef(2).at(0).unicode()){
              case 'k':
              case 'K':
                  input=locale().toString(baseNumber*1000.0);
                  break;
              case 'n':
              case 'N':
                  input=locale().toString(baseNumber/1000000.0);
                  break;
              }
          }
      private:
          const QRegularExpression vaidRegExp=QRegularExpression(QStringLiteral(R"**(^(.+?)([kKnN]?)$)**"));
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QLineEdit laxEdit;
          FixingValidator* valid= new FixingValidator(&laxEdit);
          QObject::connect(&laxEdit,&QLineEdit::textChanged,[&laxEdit,valid](const QString& txt){
              QString oldText=txt;
              valid->fixup(oldText);
              laxEdit.setText(oldText);
          });
          laxEdit.setValidator(valid);
          laxEdit.show();
          return a.exec();
      }
      
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by Natural_Bugger
        #3

        @VRonin

        thank your very much.

        i copied it to my project, first the class and compiled, everything ok.
        but than ...

        FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);
        

        results in:

        error: no matching function for call to 'FixingValidator::FixingValidator(QLineEdit**)'
             FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);
        

        i'm using a standard "project" with the designer and i try to reference the object in the *.ui file.
        i have 4 QLineEdits on my "form" on witch i want to apply this effect.

        ODБOïO 1 Reply Last reply
        0
        • N Natural_Bugger

          @VRonin

          thank your very much.

          i copied it to my project, first the class and compiled, everything ok.
          but than ...

          FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);
          

          results in:

          error: no matching function for call to 'FixingValidator::FixingValidator(QLineEdit**)'
               FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);
          

          i'm using a standard "project" with the designer and i try to reference the object in the *.ui file.
          i have 4 QLineEdits on my "form" on witch i want to apply this effect.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @Natural_Bugger hi
          a shoot in the dark

          FixingValidator* valid= new FixingValidator(ui->lineEdit_1);

          instead of

          FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);

          N 1 Reply Last reply
          5
          • ODБOïO ODБOï

            @Natural_Bugger hi
            a shoot in the dark

            FixingValidator* valid= new FixingValidator(ui->lineEdit_1);

            instead of

            FixingValidator* valid= new FixingValidator(&ui->lineEdit_1);

            N Offline
            N Offline
            Natural_Bugger
            wrote on last edited by
            #5

            @LeLev

            thank your very much, this error is eliminated.
            but the next line contains 4/5 errors.
            after correcting to your previous answer.

            QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,[ui->lineEdit_1,valid](const QString& txt){
            

            errors:

            error: capture of non-variable 'MainWindow::ui'
                     QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,[ui->lineEdit_1,valid](const QString& txt){
            
            
                                                                                     ^
            
            error: expected ',' before '->' token
                     QObject::connect(ui->lineEdit_resistor,&QLineEdit::textChanged,[ui->lineEdit_resistor,valid](const QString& txt){
                                                                                       ^
            
            error: expected identifier before '->' token
            
            error: expected ']' before ',' token
                     QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,[ui->lineEdit_1,valid](const QString& txt){
                                                                                                          ^
            
            In lambda function:
            
             error: expected '{' before ',' token
            
            In constructor 'MainWindow::MainWindow(QWidget*)':
            
            error: expected ')' before ']' token
                     QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,[ui->lineEdit_1,valid](const QString& txt){
                                                                                                                ^
            
            1 Reply Last reply
            0
            • N Offline
              N Offline
              Natural_Bugger
              wrote on last edited by
              #6

              if you remove "[" & "]", than ...

              2 errors remain.

              expected primary-expression before 'const'
                       QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,ui->lineEdit_1,valid(const QString& txt){
                                                                                                                  ^
              
              error: 'valid' cannot be used as a function
                       QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,ui->lineEdit_1,valid(const QString& txt){
                                                                                                                                    ^
              

              than i changed ...

              valid(const QString& txt) into valid->fixup(const QString& txt)

               QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,ui->lineEdit_1,valid->fixup(const QString& txt){
              

              than i tried:

              QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,ui->lineEdit_1,valid->fixup(ui->lineEdit_1->text()){
              

              but ...

              error: invalid initialization of non-const reference of type 'QString&' from an rvalue of type 'QString'
                       QObject::connect(ui->lineEdit_1,&QLineEdit::textChanged,ui->lineEdit_1,valid->fixup(ui->lineEdit_1->text()){
                                                                                                                                                    ^
              
              note:   initializing argument 1 of 'virtual void FixingValidator::fixup(QString&) const'
                   void fixup(QString &input) const override{
                        ^
              
              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                just replace [ui->lineEdit_1,valid] with [=]

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                N 1 Reply Last reply
                3
                • VRoninV VRonin

                  just replace [ui->lineEdit_1,valid] with [=]

                  N Offline
                  N Offline
                  Natural_Bugger
                  wrote on last edited by Natural_Bugger
                  #8

                  @VRonin

                  Awesome!

                  but i have 2 different types of fields, but 4 on total on witch i wanna apply this effect.

                  FixingValidator* valid= new FixingValidator(ui->lineEdit_1);
                  ...
                  ..
                  .
                  FixingValidator* valid= new FixingValidator(ui->lineEdit_2);
                  ...
                  ..
                  .
                  

                  i extended the class, ...

                      void fixup(QString &input) const override{
                              const QRegularExpressionMatch res = vaidRegExp.match(input);
                              if(!res.hasMatch())
                                  return;
                              if(res.capturedRef(2).isEmpty())
                                  return;
                              double baseNumber = locale().toDouble(res.capturedRef(1));
                              switch(res.capturedRef(2).at(0).unicode()){
                              case 'k':
                              case 'K':
                                  input=locale().toString(baseNumber*1000.0);
                                  break;
                              case 'n':
                              case 'N':
                                  input=locale().toString(baseNumber*1000000.0);
                                  break;
                              }
                          }
                      
                          void fixup2(QString &input) const override{
                              const QRegularExpressionMatch res = vaidRegExp.match(input);
                              if(!res.hasMatch())
                                  return;
                              if(res.capturedRef(2).isEmpty())
                                  return;
                              double baseNumber = locale().toDouble(res.capturedRef(1));
                              switch(res.capturedRef(2).at(0).unicode()){
                              case 'k':
                              case 'K':
                                  input=locale().toString(baseNumber/1000.0);
                                  break;
                              case 'n':
                              case 'N':
                                  input=locale().toString(baseNumber/1000000.0);
                                  break;
                              }
                          }
                      
                  

                  results in:

                  error: 'void FixingValidator::fixup2(QString&) const' marked 'override', but does not override
                       void fixup2(QString &input) const override{
                            ^
                  

                  i see that "virtual void fixup(QString &) const;" is part of QValidator.h
                  what's to do?

                  jsulmJ 1 Reply Last reply
                  0
                  • N Natural_Bugger

                    @VRonin

                    Awesome!

                    but i have 2 different types of fields, but 4 on total on witch i wanna apply this effect.

                    FixingValidator* valid= new FixingValidator(ui->lineEdit_1);
                    ...
                    ..
                    .
                    FixingValidator* valid= new FixingValidator(ui->lineEdit_2);
                    ...
                    ..
                    .
                    

                    i extended the class, ...

                        void fixup(QString &input) const override{
                                const QRegularExpressionMatch res = vaidRegExp.match(input);
                                if(!res.hasMatch())
                                    return;
                                if(res.capturedRef(2).isEmpty())
                                    return;
                                double baseNumber = locale().toDouble(res.capturedRef(1));
                                switch(res.capturedRef(2).at(0).unicode()){
                                case 'k':
                                case 'K':
                                    input=locale().toString(baseNumber*1000.0);
                                    break;
                                case 'n':
                                case 'N':
                                    input=locale().toString(baseNumber*1000000.0);
                                    break;
                                }
                            }
                        
                            void fixup2(QString &input) const override{
                                const QRegularExpressionMatch res = vaidRegExp.match(input);
                                if(!res.hasMatch())
                                    return;
                                if(res.capturedRef(2).isEmpty())
                                    return;
                                double baseNumber = locale().toDouble(res.capturedRef(1));
                                switch(res.capturedRef(2).at(0).unicode()){
                                case 'k':
                                case 'K':
                                    input=locale().toString(baseNumber/1000.0);
                                    break;
                                case 'n':
                                case 'N':
                                    input=locale().toString(baseNumber/1000000.0);
                                    break;
                                }
                            }
                        
                    

                    results in:

                    error: 'void FixingValidator::fixup2(QString&) const' marked 'override', but does not override
                         void fixup2(QString &input) const override{
                              ^
                    

                    i see that "virtual void fixup(QString &) const;" is part of QValidator.h
                    what's to do?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Natural_Bugger said in QLineEdit autofill:

                    error: 'void FixingValidator::fixup2(QString&) const' marked 'override', but does not override

                    Why fixup2?
                    fixup2 != fixup

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    N 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Natural_Bugger said in QLineEdit autofill:

                      error: 'void FixingValidator::fixup2(QString&) const' marked 'override', but does not override

                      Why fixup2?
                      fixup2 != fixup

                      N Offline
                      N Offline
                      Natural_Bugger
                      wrote on last edited by
                      #10

                      @jsulm

                      because i have 2 different type of QLineEdits formfields, one takes numbers bigger than 0 and the other takes number smaller than 0.

                      so i tried to extented the class and add an another method, called fixup2.

                      jsulmJ 1 Reply Last reply
                      0
                      • N Natural_Bugger

                        @jsulm

                        because i have 2 different type of QLineEdits formfields, one takes numbers bigger than 0 and the other takes number smaller than 0.

                        so i tried to extented the class and add an another method, called fixup2.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Natural_Bugger If you add a method which does not exist in the base class then you can't use override keyword as there is no such method in base class to override. So, remove override.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        N 1 Reply Last reply
                        3
                        • jsulmJ jsulm

                          @Natural_Bugger If you add a method which does not exist in the base class then you can't use override keyword as there is no such method in base class to override. So, remove override.

                          N Offline
                          N Offline
                          Natural_Bugger
                          wrote on last edited by
                          #12

                          @jsulm

                          it works, thank you very much.

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            Natural_Bugger
                            wrote on last edited by Natural_Bugger
                            #13

                            i still have problem with numbers containing a number using the method:
                            fixup2(QString &input).

                            1,2e-07
                            0,00012
                            

                            those that contain a comma.

                            case 'n':
                            case 'N':
                                            input=locale().toString(baseNumber/1000000.0);
                                            break;
                            

                            **"120n"**results in "0,00012" and doesn't work.
                            **"10n"**results in "1e-05" and does work.

                            no calculatiion takes place.

                            if(ui->lineEdit_1.toFloat() == 0.0){
                                   QMessageBox::information(this, "empty","",QMessageBox::Ok);
                            }
                            

                            this gets triggered.

                            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