Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    6
    12
    3352
    Loading More Posts
    • 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
      kishore_hemmady last edited by kishore_hemmady

      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 Reply Quote 0
      • ODБOï
        ODБOï last edited by VRonin

        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 Reply Quote 3
        • K
          kishore_hemmady last edited by 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.

          Gojir4 1 Reply Last reply Reply Quote 0
          • Gojir4
            Gojir4 @kishore_hemmady last edited by

            @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 Reply Quote 4
            • K
              kishore_hemmady last edited by

              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 Reply Quote 0
              • M
                mad-hatter last edited by mad-hatter

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • KillerSmath
                    KillerSmath last edited by KillerSmath

                    @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 Reply Quote 6
                    • K
                      kishore_hemmady last edited by kishore_hemmady

                      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 Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 2
                        • KillerSmath
                          KillerSmath last edited by 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.

                          @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 Reply Quote 4
                          • K
                            kishore_hemmady @KillerSmath last edited by

                            hi @KillerSmath
                            thank you for your answer.

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post