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. QDoubleSpinbox::validate(),how to make QDoubleSpinbox allow user INSERT number/character after Dot when i setDecimals(3).

QDoubleSpinbox::validate(),how to make QDoubleSpinbox allow user INSERT number/character after Dot when i setDecimals(3).

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.4k Views
  • 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    Unsolved QDoubleSpinbox::validate(),how to make QDoubleSpinbox allow user INSERT number/character after Dot when i setDecimals(2).

    eg.current value is 1.23, then i want to change it into 1.12, but i cannot just input 1 before 2 (which is 1.123 while editting) then when finished editting, the value become 1.12.
    another eg. current value is 1.23, the user can input even character like abcd (which is 1.aaaaa23 while editting), then when finished editting, the value become 1.23.
    thank you !!!

    1 Reply Last reply
    0
    • X Offline
      X Offline
      XavierLL
      wrote on last edited by
      #2

      Hi,

      I came up with one possible solution:

      You have to subclass QDoubleSpinBox and play with the methods validate and fixup.
      Validate is called every time you enter a character.
      Fixup is called when finished editing and Validate fails.

      So in validate I check for the whole number entered to be correct, if not I return QValidator::Intermediate.

      The test I am doing is very simple, you should implement a better test.
      For instance I made a text.split(",") just to test if there were more than 1 "," in the text.

      This is the code:
      Header

      #ifndef MYDOUBLESPINBOX_H
      #define MYDOUBLESPINBOX_H
      
      #include <QDoubleSpinBox>
      
      class MyDoubleSpinBox : public QDoubleSpinBox
      {
          Q_OBJECT
      public:
          explicit MyDoubleSpinBox(QWidget *parent = 0);
      
          virtual void fixup(QString &input) const override;
      protected:
          virtual QValidator::State validate(QString &input, int &pos) const override;
          bool isValid(const QString& text) const;
      
      };
      
      #endif // MYDOUBLESPINBOX_H
      

      CPP

      #include "MyDoubleSpinBox.h"
      
      #include <QDebug>
      #include <QLineEdit>
      #include <QRegExp>
      
      MyDoubleSpinBox::MyDoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
      {
      
      }
      
      QValidator::State MyDoubleSpinBox::validate(QString &input, int &pos) const
      {
          Q_UNUSED(pos);
      
          if(isValid(input))
              return QValidator::Acceptable;
      
          return QValidator::Intermediate;
      }
      
      void MyDoubleSpinBox::fixup(QString &input) const
      {
          input.remove(QRegExp("[^0-9,]"));
          QList<QString> list = input.split(",");
          if(list.size() > 1)
          {
              if(list[1].size() > decimals())
                  list[1].remove(decimals(),list[1].size()-decimals());
              input = list[0] + "," + list[1];
          }
      }
      
      bool MyDoubleSpinBox::isValid(const QString& text) const
      {
          int idx = text.indexOf(QRegExp("[^0-9,]"), 0);
          if(idx != -1)
              return false;
          QList<QString> list = text.split(",");
          if(list.size() > 2)
              return false;
      
          return true;
      }
      

      Hope it helps!

      O 1 Reply Last reply
      1
      • X XavierLL

        Hi,

        I came up with one possible solution:

        You have to subclass QDoubleSpinBox and play with the methods validate and fixup.
        Validate is called every time you enter a character.
        Fixup is called when finished editing and Validate fails.

        So in validate I check for the whole number entered to be correct, if not I return QValidator::Intermediate.

        The test I am doing is very simple, you should implement a better test.
        For instance I made a text.split(",") just to test if there were more than 1 "," in the text.

        This is the code:
        Header

        #ifndef MYDOUBLESPINBOX_H
        #define MYDOUBLESPINBOX_H
        
        #include <QDoubleSpinBox>
        
        class MyDoubleSpinBox : public QDoubleSpinBox
        {
            Q_OBJECT
        public:
            explicit MyDoubleSpinBox(QWidget *parent = 0);
        
            virtual void fixup(QString &input) const override;
        protected:
            virtual QValidator::State validate(QString &input, int &pos) const override;
            bool isValid(const QString& text) const;
        
        };
        
        #endif // MYDOUBLESPINBOX_H
        

        CPP

        #include "MyDoubleSpinBox.h"
        
        #include <QDebug>
        #include <QLineEdit>
        #include <QRegExp>
        
        MyDoubleSpinBox::MyDoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
        {
        
        }
        
        QValidator::State MyDoubleSpinBox::validate(QString &input, int &pos) const
        {
            Q_UNUSED(pos);
        
            if(isValid(input))
                return QValidator::Acceptable;
        
            return QValidator::Intermediate;
        }
        
        void MyDoubleSpinBox::fixup(QString &input) const
        {
            input.remove(QRegExp("[^0-9,]"));
            QList<QString> list = input.split(",");
            if(list.size() > 1)
            {
                if(list[1].size() > decimals())
                    list[1].remove(decimals(),list[1].size()-decimals());
                input = list[0] + "," + list[1];
            }
        }
        
        bool MyDoubleSpinBox::isValid(const QString& text) const
        {
            int idx = text.indexOf(QRegExp("[^0-9,]"), 0);
            if(idx != -1)
                return false;
            QList<QString> list = text.split(",");
            if(list.size() > 2)
                return false;
        
            return true;
        }
        

        Hope it helps!

        O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        @XavierLL thank you very much:)

        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