Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QIntValidator for unsigned long values



  • 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



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



  • 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;
    };
    


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



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



  • @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!


Log in to reply