Home > front end >  QSpinBox prevent the user from enternig thousand separators
QSpinBox prevent the user from enternig thousand separators

Time:02-02

In a QSpinBox, when the range is sufficient, the user is allowed to enter thousand separators.
Eg: 1.2.3.4 is a valid entry, en then fixup() just removes the dots. Resulting in 1234.

How can I prevent the user from entering thousand separators?

Previously I made something similar based on QLineEdit which uses validators.
But QAbstractSpinbox doesn't use QValidators. So I'm not sure how to proceed.

CodePudding user response:

Just override validate and reject input if contains undesired character.

It could be something like that:

QValidator::State MySpinBox::validate(QString &input, int &pos) const
{
    if (input.contains(ThousandSeparator)) {
        return QValidator::Invalid;
    }
    return QSpinBox::validate(input, pos);
}

Please do not be to greedy what user can and can't do, since to restrictive rules are annoying for the end user.

validate is called whenever content changes. fixup is called when value should be committed (widget losses focus or enter is pressed) and must be able to transform input from intermediate state to valid.

  •  Tags:  
  • Related