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. QIntValidator for unsigned long values

QIntValidator for unsigned long values

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • P Offline
    P Offline
    p_Each
    wrote on last edited by
    #1

    Hello,

    I need to have a Qlineedit which must accept only integer values from 0 to 2^32. I am trying to do it with the QIntValidator, setting 0 and UINT_MAX as limits, but if I do it like this it doesn't accept values higher than 9.
    Has anyone ever managed to make it work or should I look somewhere else (e.g. Regular Expressions)?

    Thank you in advance

    JonBJ 1 Reply Last reply
    0
    • P p_Each

      Hello,

      I need to have a Qlineedit which must accept only integer values from 0 to 2^32. I am trying to do it with the QIntValidator, setting 0 and UINT_MAX as limits, but if I do it like this it doesn't accept values higher than 9.
      Has anyone ever managed to make it work or should I look somewhere else (e.g. Regular Expressions)?

      Thank you in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @p_Each
      Can't use UINT_MAX for a (signed) int/int32. There is no QUintValidator. You sure you can't manage with range to 2 ^ 31 - 1 (INT_MAX)? If not, I think you'll find it essentially impossible to write a (reasonable) regular expression for this, so write your own deriving from QValidator, inspired by QIntValidator but allowing unsigned?

      doesn't accept values higher than 9.

      BTW: unless it's to do with your upper bound being treated as negative (which it may be), this sounds more like QValidator::Intermediate result than QValidator::Invalid. Just be aware this is how Qt QValidators work.

      P 1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3
        class UIntValidator : public QValidator {
            Q_OBJECT
        public:
            UIntValidator(quint32 minimum, quint32 maximum, QObject *parent = nullptr)
                : QValidator(parent)
                , m_min(minimum)
                , m_max(maximum)
            {}
            UIntValidator(QObject *parent = nullptr)
                : UIntValidator(std::numeric_limits<quint32>::min(),std::numeric_limits<quint32>::max(),parent)
            {}
            quint32 bottom() const { return m_min; }
            quint32 top() const { return m_max; }
            void setBottom(quint32 minimum){
                if(m_min==minimum)
                    return;
                m_min=minimum;
                changed();
            }
            void setTop(quint32 maximum){
                if(m_max==maximum)
                    return;
                m_max=maximum;
                changed();
            }
            void setRange(quint32 minimum, quint32 maximum){
                setBottom(minimum);
                setTop(maximum)
            }
            QValidator::State validate(QString &input, int&) const override{
                bool convertOk=false;
                quint32 numInput = input.toUInt(&convertOk);
                if(!convertOk)
                    return QValidator::Invalid;
                if(numInput<m_min)
                    return QValidator::Intermediate;
                if(numInput>m_max)
                    return QValidator::Invalid;
                return QValidator::Acceptable;
            }
        private:
            quint32 m_min;
            quint32 m_max;
        };
        

        "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

        JonBJ P 2 Replies Last reply
        6
        • VRoninV VRonin
          class UIntValidator : public QValidator {
              Q_OBJECT
          public:
              UIntValidator(quint32 minimum, quint32 maximum, QObject *parent = nullptr)
                  : QValidator(parent)
                  , m_min(minimum)
                  , m_max(maximum)
              {}
              UIntValidator(QObject *parent = nullptr)
                  : UIntValidator(std::numeric_limits<quint32>::min(),std::numeric_limits<quint32>::max(),parent)
              {}
              quint32 bottom() const { return m_min; }
              quint32 top() const { return m_max; }
              void setBottom(quint32 minimum){
                  if(m_min==minimum)
                      return;
                  m_min=minimum;
                  changed();
              }
              void setTop(quint32 maximum){
                  if(m_max==maximum)
                      return;
                  m_max=maximum;
                  changed();
              }
              void setRange(quint32 minimum, quint32 maximum){
                  setBottom(minimum);
                  setTop(maximum)
              }
              QValidator::State validate(QString &input, int&) const override{
                  bool convertOk=false;
                  quint32 numInput = input.toUInt(&convertOk);
                  if(!convertOk)
                      return QValidator::Invalid;
                  if(numInput<m_min)
                      return QValidator::Intermediate;
                  if(numInput>m_max)
                      return QValidator::Invalid;
                  return QValidator::Acceptable;
              }
          private:
              quint32 m_min;
              quint32 m_max;
          };
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @VRonin
          You are too kind to write the code! :)

          1 Reply Last reply
          0
          • JonBJ JonB

            @p_Each
            Can't use UINT_MAX for a (signed) int/int32. There is no QUintValidator. You sure you can't manage with range to 2 ^ 31 - 1 (INT_MAX)? If not, I think you'll find it essentially impossible to write a (reasonable) regular expression for this, so write your own deriving from QValidator, inspired by QIntValidator but allowing unsigned?

            doesn't accept values higher than 9.

            BTW: unless it's to do with your upper bound being treated as negative (which it may be), this sounds more like QValidator::Intermediate result than QValidator::Invalid. Just be aware this is how Qt QValidators work.

            P Offline
            P Offline
            p_Each
            wrote on last edited by
            #5

            @JonB thanks, I suspected it but I still wanted to check. The max value needs to be 2^32-1, so I guess I will try extending QIntValidator

            1 Reply Last reply
            0
            • VRoninV VRonin
              class UIntValidator : public QValidator {
                  Q_OBJECT
              public:
                  UIntValidator(quint32 minimum, quint32 maximum, QObject *parent = nullptr)
                      : QValidator(parent)
                      , m_min(minimum)
                      , m_max(maximum)
                  {}
                  UIntValidator(QObject *parent = nullptr)
                      : UIntValidator(std::numeric_limits<quint32>::min(),std::numeric_limits<quint32>::max(),parent)
                  {}
                  quint32 bottom() const { return m_min; }
                  quint32 top() const { return m_max; }
                  void setBottom(quint32 minimum){
                      if(m_min==minimum)
                          return;
                      m_min=minimum;
                      changed();
                  }
                  void setTop(quint32 maximum){
                      if(m_max==maximum)
                          return;
                      m_max=maximum;
                      changed();
                  }
                  void setRange(quint32 minimum, quint32 maximum){
                      setBottom(minimum);
                      setTop(maximum)
                  }
                  QValidator::State validate(QString &input, int&) const override{
                      bool convertOk=false;
                      quint32 numInput = input.toUInt(&convertOk);
                      if(!convertOk)
                          return QValidator::Invalid;
                      if(numInput<m_min)
                          return QValidator::Intermediate;
                      if(numInput>m_max)
                          return QValidator::Invalid;
                      return QValidator::Acceptable;
                  }
              private:
                  quint32 m_min;
                  quint32 m_max;
              };
              
              P Offline
              P Offline
              p_Each
              wrote on last edited by
              #6

              @VRonin I can't express how grateful I am for this. I think there's a typo in the validate function (the condition of the third if statement should be numInput > m_max), but otherwise it worked. Thanks!

              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