Nominate our 2022 Qt Champions!

Add and remove a QIntValidator on a QSpinBox

  • Greetings.

    I wanted to know if it is possible to add and remove a QIntValidator on a QSpinBox.

    I know the function 'setValidator ()', but I'm not sure that can be used with a QSpinBox.

    Moreover, just as you can add / associate a QIntValidator using the 'setValidator ()' ... Is there a function to remove it?

    Thanks in advance for any help.

  • The QSpinBox has no exposed validator or way to set it. I don't see much point removing the numeric validator attached to a QSpinBox even if you could. By default the QSpinBox only allows integer numeric input, but you can adapt that using the two documented protected functions.

    On QLineEdit you can remove a validator set earlier by calling setValidator(0)

  • Thanks for the reply.

    Actually the problem I have is that I want to create a QSpinBox allowing entry only odd numbers ... really do not know if it is feasible to achieve.

    I also noticed that a user can enter QSpinBox the character '.', I thought I could prevent this from happening by using a QIntValidator

  • There is an easier way:

    yourSpinBox->setMinimum( 1 ); //or whatever odd nr'ed minimum you want
    yourSpinBox->setSingleStep( 2 );

  • That does not stop someone typing in "4". For that you need to use the protected valueFromText() function to convert "4" to either 3 or 5.

    A partial implementation:
    class OddSpin: public QSpinBox {
    OddSpin(QWidget *p = 0): QSpinBox(p) {

    int valueFromText(const QString &text) const {
    int value = text.toInt();
    if (value % 2 == 0)
    value = qMax(value, minimum());
    value = qMin(value, maximum());
    return value;

    Even then a programmer can call setValue() or set the range with even numbers. To make a bullet proof implementation I think you'd need to start with QAbstractSpinBox.

  • [quote author="ChrisW67" date="1358844429"]That does not stop someone typing in "4".[/quote]

    My bad...although you probably could still call

    yourSpinBox->setReadOnly( true );

    (if the range isn't too big)

    Alternatively, getting back to the original attempt using validators (don't know why I didn't just provide this before...early caffeine yet...internal brain rebellion, I don't know), but I believe this should work:

    QLineEdit *lineEdit = new QLineEdit;
    lineEdit->setValidator( yourValidator );
    yourSpinBox->setLineEdit( lineEdit ); //takes ownership

  • Thank you all for your contributions, truly have given me many ideas and options.

    I should clarify that the object 'SpinBox' that I want to add validation (only odd integers and if possible prevent the user can insert the character '.')
    Is part of a dialog that I created with Qt Designer (the which has, among other widgets, several QSpinBoxs).

    Now, with regard to the suggestion given by ChrisW67, I doubt has arisen as follows:

    How do I change (within the code of my program) the QSpinBox of QtDesigner by the SpinBox created by me (which, in fact, must be a subclass of QSpinBox)?

  • You want the "Promote" feature in Designer.
    "Promoting Widgets":

  • Regarding the suggestion given by goblincodingn (which I found very interesting), there is the problem that the function 'setLineEdit ()' is protected and I can not use it or invoke it from my dialog class.

    What I can do in this situation? ... Do you simply can not use the function or is there an alternative?

  • Sorry, I realise now that my code snippet above did not account for the protected nature of the far I have been rather rubbish at helping you here, my apologies!

    You can inherit from QSpinBox and re-implement the setLineEdit() function to set your specific line edit validator as opposed to the one that is passed in as parameter (that should be enough, but you will obviously have to confirm as I don't know for sure when that method gets called).

    I hope I didn't miss something yet again! :D

  • No problem ...

    You and others have given me many ideas

  • Regards

    I implemented a class Spinbox (QSpinBox subclass) that supports input only odd numbers (Based on the suggestion of ChrisW67).
    Inside the class constructor I add a QRegExpValidator on LineEdit to keep out of character '.'.

    I've turned part of the problem I had, though nothing in my implementation saves the programmer invokes the routine 'setValue ()'
    with an even number (as also mentioned ChrisW67).

    Well, in this case what could I do ... What function (or functions) should implement and / or redeploy?.

    I thought about creating a function (one slot) to verify and Spinbox Correct the input and then connect to the signal 'valueChanged ()',
    although I'm not sure if this is the best solution (the most efficient, clean, or at least the easiest).

    Thanks again for any help or comments.

Log in to reply