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. SetValidator doesn't work
Forum Updated to NodeBB v4.3 + New Features

SetValidator doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.5k Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I wrote:
    ui->lineEdit->setValidator(new QIntValidator(-30,+30,this));
    It works only for numbers <-30 but it doesn't work for numbers>30

    1 Reply Last reply
    0
    • mranger90M Offline
      mranger90M Offline
      mranger90
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What exactly do you mean by but it doesn't work for numbers>30 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ? 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          What exactly do you mean by but it doesn't work for numbers>30 ?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @SGaist it doesn't permit to write numbers <-30 but it permits to write in LineEdit numbers>30

          mrjjM 1 Reply Last reply
          0
          • ? A Former User

            @SGaist it doesn't permit to write numbers <-30 but it permits to write in LineEdit numbers>30

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @vale88
            Hi
            From I understand, QIntValidator returns QValidator::Intermediate for when the value is outside the limit
            and QLineEdit accepts that.
            You can try to use a custom validator
            like
            ui->lineEdit->setValidator(new MyIntRangeValidator(-30,30,this));

            class MyIntRangeValidator : public QIntValidator
            {
            public:
                MyIntRangeValidator(int bottom, int top, QObject *parent) :
                    QIntValidator(bottom, top, parent)
                {
                }
            
                QValidator::State validate(QString &s, int &) const override
                {
                    if (s.isEmpty() || s == "-") {
                        return QValidator::Intermediate;
                    }
            
                    bool ok;
                    int d = s.toInt(&ok);
                    if (ok && d >= bottom() && d <= top() ) {
                        return QValidator::Acceptable;
                    } else {
                        return QValidator::Invalid;
                    }
                }
            };
            

            Which works more like expected.

            ? 1 Reply Last reply
            0
            • mrjjM mrjj

              @vale88
              Hi
              From I understand, QIntValidator returns QValidator::Intermediate for when the value is outside the limit
              and QLineEdit accepts that.
              You can try to use a custom validator
              like
              ui->lineEdit->setValidator(new MyIntRangeValidator(-30,30,this));

              class MyIntRangeValidator : public QIntValidator
              {
              public:
                  MyIntRangeValidator(int bottom, int top, QObject *parent) :
                      QIntValidator(bottom, top, parent)
                  {
                  }
              
                  QValidator::State validate(QString &s, int &) const override
                  {
                      if (s.isEmpty() || s == "-") {
                          return QValidator::Intermediate;
                      }
              
                      bool ok;
                      int d = s.toInt(&ok);
                      if (ok && d >= bottom() && d <= top() ) {
                          return QValidator::Acceptable;
                      } else {
                          return QValidator::Invalid;
                      }
                  }
              };
              

              Which works more like expected.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @mrjj I solved in a more simple way:

              void MainWindow::on_Value_textChanged(const QString &arg1)
              {

              //se i valori immessi non sono nel range impedisco di poterli scrivere
              if(arg1.toInt()>30 || arg1.toInt()<-30)
              {
                 //se ad esempio cerchero di scrivere 40 scriverò 4
                  int arg2 = arg1.toInt()/10;
                  ui->Value->setText(QString::number(arg2));
              
              
               }
              
              mrjjM 1 Reply Last reply
              1
              • ? A Former User

                @mrjj I solved in a more simple way:

                void MainWindow::on_Value_textChanged(const QString &arg1)
                {

                //se i valori immessi non sono nel range impedisco di poterli scrivere
                if(arg1.toInt()>30 || arg1.toInt()<-30)
                {
                   //se ad esempio cerchero di scrivere 40 scriverò 4
                    int arg2 = arg1.toInt()/10;
                    ui->Value->setText(QString::number(arg2));
                
                
                 }
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @vale88
                Indeed it is :) , but would be a bit messy if u needed various LineEdits with slightly different
                ranges.
                However, why call toInt() so many times?

                void MainWindow::on_Value_textChanged(const QString &arg1)
                {
                
                //se i valori immessi non sono nel range impedisco di poterli scrivere
                int value = arg1.toInt();
                if(value>30 || value<-30)
                {
                   //se ad esempio cerchero di scrivere 40 scriverò 4
                    ui->Value->setText(QString::number(value/10));
                
                
                 }
                
                ? 1 Reply Last reply
                1
                • mrjjM mrjj

                  @vale88
                  Indeed it is :) , but would be a bit messy if u needed various LineEdits with slightly different
                  ranges.
                  However, why call toInt() so many times?

                  void MainWindow::on_Value_textChanged(const QString &arg1)
                  {
                  
                  //se i valori immessi non sono nel range impedisco di poterli scrivere
                  int value = arg1.toInt();
                  if(value>30 || value<-30)
                  {
                     //se ad esempio cerchero di scrivere 40 scriverò 4
                      ui->Value->setText(QString::number(value/10));
                  
                  
                   }
                  
                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  @mrjj it's an error, it isn't necessary

                  1 Reply Last reply
                  1

                  • Login

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