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. How to restrict QLineEdit to take "comma" and "plus" as input value?
QtWS25 Last Chance

How to restrict QLineEdit to take "comma" and "plus" as input value?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 4.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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by kishore_hemmady
    #1

    Hi,,

    how can I avoid entering comma (,) and plus (+) to QLineEdit field in my Qt ui application??
    and also QSpinBox to not to take + (plus)??

    1 Reply Last reply
    0
    • ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by VRonin
      #2

      hi @kishore_hemmady ,
      see void QLineEdit::setValidator(const QValidator *v) http://doc.qt.io/qt-5/qlineedit.html#setValidator

      QRegExp exp = QRegExp("your regExp");
      QRegExpValidator v = QRegExpValidator(exp,this);
       yourLineEdit.setValidator(v);
      
      1 Reply Last reply
      3
      • K Offline
        K Offline
        kishore_hemmady
        wrote on last edited by kishore_hemmady
        #3

        thank you @LeLev

        please tell me how can i set range for this field using the below snippet?

        QRegExp rx("-?\\d{1,3}");
        QValidator *validator = new QRegExpValidator(rx, this);
        ui->VideoHeight->setValidator(validator);
        

        I want to set the range between 1 to 100.

        Gojir4G 1 Reply Last reply
        0
        • K kishore_hemmady

          thank you @LeLev

          please tell me how can i set range for this field using the below snippet?

          QRegExp rx("-?\\d{1,3}");
          QValidator *validator = new QRegExpValidator(rx, this);
          ui->VideoHeight->setValidator(validator);
          

          I want to set the range between 1 to 100.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @kishore_hemmady Hi,

          Better to use QIntValidator in this case:

          QValidator *validator = new QIntValidator(1, 100, this);
          // the edit lineedit will only accept integers between 1 and 100 
          edit->setValidator(validator);
          
          1 Reply Last reply
          4
          • K Offline
            K Offline
            kishore_hemmady
            wrote on last edited by
            #5

            hi @Gojir4 ,

            thank you,
            i had used the same but ,i was able to enter comma (,)and plus (+) symbol if i use the above code,which i don't want.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mad-hatter
              wrote on last edited by mad-hatter
              #6

              Hello,

              You could install an eventFilter on your lineEdit and trap the comma, '+' and '-' sign and use the int validator as well.

              Regards

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                What is your widget purpose ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • KillerSmathK Offline
                  KillerSmathK Offline
                  KillerSmath
                  wrote on last edited by KillerSmath
                  #8

                  @kishore_hemmady

                  You can create a RegularExpression to accept just 0 or 1-99 or 100

                  QRegExp regExp = QRegExp("^(0|[1-9][0-9]?|100)$");
                  

                  | = OR
                  ? = OPTIONAL

                  NOTE: if you are using Qt5.*, i suggest you to use QRegularExpression instead of QRegExp, because QRegExp is an old class of regular expression of Qt4 :)
                  http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users

                  @Computer Science Student - Brazil
                  Web Developer and Researcher
                  “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                  1 Reply Last reply
                  6
                  • K Offline
                    K Offline
                    kishore_hemmady
                    wrote on last edited by kishore_hemmady
                    #9

                    Hi @SGaist

                    its a variable , i have to set it like this,
                    ui->height->setValidator(newQIntValidator(minValue,maxValue, this));
                    if i use this then the height field will take + and , values.

                    minValue and maxValue values are system dependent

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      If you still have a comma that you can enter then there's something else that's wrong neither QIntValidator nor QSpinBox allows that kind of number.

                      What is the problem with the plus sign ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • KillerSmathK Offline
                        KillerSmathK Offline
                        KillerSmath
                        wrote on last edited by KillerSmath
                        #11

                        @kishore_hemmady
                        If you really need to avoid the + signal and just accept numbers.

                        1) You can create a custom QIntValidator and reimplement the validate function to accept what you need.

                        // customintvalidator.h
                        #include <QIntValidator>
                        
                        class CustomIntValidator : public QIntValidator {
                        public:
                            CustomIntValidator (int bottom, int top, QObject * parent) : QIntValidator(bottom, top, parent) {}
                        
                            QValidator::State validate(QString &s, int &pos) const {
                                Q_UNUSED(pos);
                                if (s.isEmpty()){
                                    return QValidator::Intermediate; // accept but is not valid
                                }
                        
                                if( s.startsWith("+") ){
                                    return QValidator::Invalid;
                                }
                        
                                if( bottom() < 0 && s.startsWith("-") && s.length() == 1 ){
                                    return QValidator::Intermediate;
                                }
                        
                                // check number
                        
                                bool isNumber;
                                int value = locale().toInt(s, &isNumber);
                        
                                if (isNumber && bottom() <= value && value <= top()) {
                                    return QValidator::Acceptable;
                                }
                        
                                return QValidator::Invalid;
                            }
                        
                        };
                        
                        // mainwindow.cpp
                        ui->lineEdit->setValidator(new CustomIntValidator(-100 , 100, this));
                        

                        This custom IntValidator accepts positive and negative numbers incluinding -0 interpreted as 0
                        And ignore comma, dot and plus simbols.

                        2) You can create a filter to capture the Comma and Plus Keydown event

                        // filterobject.h
                        #include <QEvent>
                        #include <QKeyEvent>
                        #include <QObject>
                        
                        class FilterObject : public QObject {
                        public:
                            explicit FilterObject(QObject *parent = 0) : QObject(parent){
                            }
                            
                            bool eventFilter(QObject *object, QEvent *event)
                            {
                                Q_UNUSED(object);
                                if (event->type() == QEvent::KeyPress) {
                                    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                                    switch(keyEvent->key()){
                                    case Qt::Key_Plus:
                                        return true;
                                    case Qt::Key_Comma:
                                        return true;
                                    default:
                                        return false;
                                    }
                                }
                                return false;
                            }
                        };
                        
                        // mainwindow.cpp
                        ui->lineEdit->installEventFilter(new FilterObject(ui->lineEdit));
                        

                        I don't believe these are the best solutions for this problem but works.

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        K 1 Reply Last reply
                        4
                        • KillerSmathK KillerSmath

                          @kishore_hemmady
                          If you really need to avoid the + signal and just accept numbers.

                          1) You can create a custom QIntValidator and reimplement the validate function to accept what you need.

                          // customintvalidator.h
                          #include <QIntValidator>
                          
                          class CustomIntValidator : public QIntValidator {
                          public:
                              CustomIntValidator (int bottom, int top, QObject * parent) : QIntValidator(bottom, top, parent) {}
                          
                              QValidator::State validate(QString &s, int &pos) const {
                                  Q_UNUSED(pos);
                                  if (s.isEmpty()){
                                      return QValidator::Intermediate; // accept but is not valid
                                  }
                          
                                  if( s.startsWith("+") ){
                                      return QValidator::Invalid;
                                  }
                          
                                  if( bottom() < 0 && s.startsWith("-") && s.length() == 1 ){
                                      return QValidator::Intermediate;
                                  }
                          
                                  // check number
                          
                                  bool isNumber;
                                  int value = locale().toInt(s, &isNumber);
                          
                                  if (isNumber && bottom() <= value && value <= top()) {
                                      return QValidator::Acceptable;
                                  }
                          
                                  return QValidator::Invalid;
                              }
                          
                          };
                          
                          // mainwindow.cpp
                          ui->lineEdit->setValidator(new CustomIntValidator(-100 , 100, this));
                          

                          This custom IntValidator accepts positive and negative numbers incluinding -0 interpreted as 0
                          And ignore comma, dot and plus simbols.

                          2) You can create a filter to capture the Comma and Plus Keydown event

                          // filterobject.h
                          #include <QEvent>
                          #include <QKeyEvent>
                          #include <QObject>
                          
                          class FilterObject : public QObject {
                          public:
                              explicit FilterObject(QObject *parent = 0) : QObject(parent){
                              }
                              
                              bool eventFilter(QObject *object, QEvent *event)
                              {
                                  Q_UNUSED(object);
                                  if (event->type() == QEvent::KeyPress) {
                                      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                                      switch(keyEvent->key()){
                                      case Qt::Key_Plus:
                                          return true;
                                      case Qt::Key_Comma:
                                          return true;
                                      default:
                                          return false;
                                      }
                                  }
                                  return false;
                              }
                          };
                          
                          // mainwindow.cpp
                          ui->lineEdit->installEventFilter(new FilterObject(ui->lineEdit));
                          

                          I don't believe these are the best solutions for this problem but works.

                          K Offline
                          K Offline
                          kishore_hemmady
                          wrote on last edited by
                          #12

                          hi @KillerSmath
                          thank you for your answer.

                          1 Reply Last reply
                          1

                          • Login

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