Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add and remove a QIntValidator on a QSpinBox
Forum Updated to NodeBB v4.3 + New Features

Add and remove a QIntValidator on a QSpinBox

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 9.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    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.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isaacEnrique
        wrote on last edited by
        #3

        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

        Isaac Pérez
        Programming is understanding.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goblincoding
          wrote on last edited by
          #4

          There is an easier way:

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

          http://www.goblincoding.com

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            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 {
            Q_OBJECT
            public:
            OddSpin(QWidget *p = 0): QSpinBox(p) {
            setMinimum(1);
            setSingleStep(2);
            }

            protected:
            int valueFromText(const QString &text) const {
            int value = text.toInt();
            if (value % 2 == 0)
            --value;
            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goblincoding
              wrote on last edited by
              #6

              [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 morning...no 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
              @

              http://www.goblincoding.com

              1 Reply Last reply
              0
              • I Offline
                I Offline
                isaacEnrique
                wrote on last edited by
                #7

                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)?

                Isaac Pérez
                Programming is understanding.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  You want the "Promote" feature in Designer.
                  "Promoting Widgets":http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html#promoting-widgets

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    isaacEnrique
                    wrote on last edited by
                    #9

                    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?

                    Isaac Pérez
                    Programming is understanding.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goblincoding
                      wrote on last edited by
                      #10

                      Sorry, I realise now that my code snippet above did not account for the protected nature of the method...so 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

                      http://www.goblincoding.com

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        isaacEnrique
                        wrote on last edited by
                        #11

                        No problem ...

                        You and others have given me many ideas

                        Isaac Pérez
                        Programming is understanding.

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          isaacEnrique
                          wrote on last edited by
                          #12

                          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.

                          Isaac Pérez
                          Programming is understanding.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved