T
T
tschin2016-12-08 15:58:24
Qt
tschin, 2016-12-08 15:58:24

Why does QSpinbox allow null input?

Hello! There is a QSpinbox, it has boundary values ​​​​from 1 to 83. The default value is 1. However, the spinbox skips (allows you to enter) such values:

0000000
01
083
0

That is, it allows you to enter zero. Of course, after the input is finished, zero is changed to the default value. But I would like to disable zero input altogether. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-12-08
@khrisanfov

You need to inherit the QSpinBox class and implement the validate method

class SpinBox : public QSpinBox
{
public:
    SpinBox(QWidget *parent = 0) : QSpinBox(parent) {}

    QValidator::State validate(QString &input, int &pos) const
    {
        if (input != "0") {
            return QValidator::Acceptable;
        } else {
            return QValidator::Invalid;
        }
    }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question