QDoubleSpinbox::validate(),how to make QDoubleSpinbox allow user INSERT number/character after Dot when i setDecimals(3).



  • Unsolved QDoubleSpinbox::validate(),how to make QDoubleSpinbox allow user INSERT number/character after Dot when i setDecimals(2).

    eg.current value is 1.23, then i want to change it into 1.12, but i cannot just input 1 before 2 (which is 1.123 while editting) then when finished editting, the value become 1.12.
    another eg. current value is 1.23, the user can input even character like abcd (which is 1.aaaaa23 while editting), then when finished editting, the value become 1.23.
    thank you !!!



  • Hi,

    I came up with one possible solution:

    You have to subclass QDoubleSpinBox and play with the methods validate and fixup.
    Validate is called every time you enter a character.
    Fixup is called when finished editing and Validate fails.

    So in validate I check for the whole number entered to be correct, if not I return QValidator::Intermediate.

    The test I am doing is very simple, you should implement a better test.
    For instance I made a text.split(",") just to test if there were more than 1 "," in the text.

    This is the code:
    Header

    #ifndef MYDOUBLESPINBOX_H
    #define MYDOUBLESPINBOX_H
    
    #include <QDoubleSpinBox>
    
    class MyDoubleSpinBox : public QDoubleSpinBox
    {
        Q_OBJECT
    public:
        explicit MyDoubleSpinBox(QWidget *parent = 0);
    
        virtual void fixup(QString &input) const override;
    protected:
        virtual QValidator::State validate(QString &input, int &pos) const override;
        bool isValid(const QString& text) const;
    
    };
    
    #endif // MYDOUBLESPINBOX_H
    

    CPP

    #include "MyDoubleSpinBox.h"
    
    #include <QDebug>
    #include <QLineEdit>
    #include <QRegExp>
    
    MyDoubleSpinBox::MyDoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
    {
    
    }
    
    QValidator::State MyDoubleSpinBox::validate(QString &input, int &pos) const
    {
        Q_UNUSED(pos);
    
        if(isValid(input))
            return QValidator::Acceptable;
    
        return QValidator::Intermediate;
    }
    
    void MyDoubleSpinBox::fixup(QString &input) const
    {
        input.remove(QRegExp("[^0-9,]"));
        QList<QString> list = input.split(",");
        if(list.size() > 1)
        {
            if(list[1].size() > decimals())
                list[1].remove(decimals(),list[1].size()-decimals());
            input = list[0] + "," + list[1];
        }
    }
    
    bool MyDoubleSpinBox::isValid(const QString& text) const
    {
        int idx = text.indexOf(QRegExp("[^0-9,]"), 0);
        if(idx != -1)
            return false;
        QList<QString> list = text.split(",");
        if(list.size() > 2)
            return false;
    
        return true;
    }
    

    Hope it helps!



  • @XavierLL thank you very much:)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.