3
votes

I have a QSpinBox and I want it to only accept positive natural numbers. This works fine, until I enter a dot .

If I enter 234235.23456and change the focus to some other spinbox, the value is changed to 23423523456.

So I am looking for a way to ignore everything behind the .

Is this possible without subclassing QSpinBox?

1
use a comma? Qt respects localization settings (IIRC) so that may help - ratchet freak
Probably no, but QDoubleSpinBox with setDecimals(0) might be your workaround. :p Otherwise, just reimplement this. - lpapp
I first tried the QDoubleSpinBox with setDecimals(0) but it acts the same way - user2699453
Does it work as expected if you enter a comma? - Silicomancer
the Spinbox only takes digits from 0-9 and if setDecimals(0) it takes .. - user2699453

1 Answers

2
votes

I found a fix for my porblem:

QDoubleSpinBox * box = new QDoubleSpinBox();

box->setDecimals(0);
box->setSingleStep(1.0);

box->findChild<QLineEdit*>()->setValidator(new QRegExpValidator(QRegExp(QString("^[1-9][0-9]*$"))));