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. [Solved]How to validate maximum value which must be filled in each row of QLineEdit in run time?
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to validate maximum value which must be filled in each row of QLineEdit in run time?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.1k 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.
  • F Offline
    F Offline
    frfm
    wrote on last edited by
    #1

    Hello guys....I have one dynamic matrix of QLineEdit in my class and I want validate the maximum value of each field according the values already filled in one row. For instance, in this matrix I want which the sum of values filled in each line don't exceed 50. How do I do this validation in run time?

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not just use a QSpinBox where you set the maximum to that value ?

      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
      0
      • F Offline
        F Offline
        frfm
        wrote on last edited by
        #3

        Hello SGaist, thanks so much for your answer.

        So, I have not used QSpinBox because QLineEdit is faster and practice to user type your data in this application, each row is an observation of an set of objects and each columns is day which this objects was observed, thus in each QLineEdit the user will enter how many objects reacted in an determinate environment, but this total of objects is defined by the user previously, so each row can't exceed this number total of objects. For instance, the number total of objects is 50, so for each row:

        matrix[i][ 0] + matrix[i][ 1] +.... + matrix[i][n] cant' exceed 50.

        PS: This total number of observations (row) and the total number of days (Columns) are defined previously by user (The matrix is dynamically created).

        Leave me show to you what I am doing.

        !http://i.imgur.com/zXTxOuM.png!

        Thanks in advance.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I see what you mean, but I fail to see why a QLineEdit would be better for inputing numbers. If it's only a question of not having the arrows on the right, they can be hidden.

          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
          0
          • F Offline
            F Offline
            frfm
            wrote on last edited by
            #5

            Sorry....I don't knew which I could hide the arrows. Thanks for the tip. So, I am using QSpinBox now, but I get the same result, I am not getting set the maximum value according with modifications which happens in run time in others QSpinBox of the same row. I tried this:

            -I have created one sum attribute. This sum is one array, each position is the sum accumulated of each row of matrix;

            • So, I configured maximum value of each QSpinBox this way.

            @matrix[i][j]->setMaximum(this->seeds - this->sum[i]);@

            • Finally, I have created too an Slot which refresh the variable sum ever which an QSpinBox emit one signal valueChanged.

            @connect(matrix[i][j], SIGNAL(valueChanged(int)), this, SLOT(sumValidator()));@

            But this not works.

            How can I do it work?

            Thanks in advance again.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Doesn't work is a bit too broad, what doesn't work ?

              Can you post the code where your setup the matrix/update the maximums etc. ?

              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
              0
              • F Offline
                F Offline
                frfm
                wrote on last edited by
                #7

                Yeah...Sorry again...

                Doesn't work the validation...In the other words, the sum maximum which each row can to have is defined by attribute this->seeds. That is, let's assume this->seeds = 50 and days (columns) = 5. If the user user type 10, 10, 10 respectively in the columns 0, 1, 2, thus not should be possible the user enter values higher which 20 in the rest of columns. Below is my code.

                Constructor of my class, where I create my matrix and connect my slot.

                @Matrix::Matrix(QWidget *parent, MainWindow *mainWindow, int repetitions, int days, int seeds) :
                QWidget(parent)
                {
                ...

                    this->repetitions = repetitions;
                    this->days = days;
                    this->seeds = seeds;
                
                    this->gridLayout = new QGridLayout();
                    this->matrix = new QSpinBox**[ this->repetitions ];
                    this->sum = new int[this->repetitions];
                
                    for(int i = 0; i < this->repetitions; i++){
                        matrix[ i ] = new QSpinBox*[ this->days + 1 ];
                        this->sum[i] = 0;
                    }
                

                for(int i = 0; i < this->repetitions; i++){

                        for(int j = 0; j < this->days + 1; j++){
                            
                                matrix[i][j] = new QSpinBox();
                                matrix[i][j]->setButtonSymbols(QAbstractSpinBox::NoButtons);
                                connect(matrix[i][j], SIGNAL(valueChanged(int)), this, SLOT(sumValidator()));
                                matrix[i][j]->setMaximum(this->seeds - this->sum[i]);
                                matrix[i][j]->setFixedHeight(25);
                                matrix[i][j]->setFixedWidth(60);
                                gridLayout->addWidget(matrix[i][j], i, j);
                
                         }
                  }
                
                .....
                

                }@

                My Slot sumValidation - Refresh the array attribute sum ever which an QSpinBox emit one signal valueChanged.

                @void Matrix::sumValidator(){

                for(int i = 0; i < this->repetitions; i++){
                
                    this->sum[i] = 0;
                
                    for(int j = 0; j < this->days + 1; j++){
                
                        this->sum[i] += this->matrix[i][j]->value();
                    }
                
                }
                

                }@

                Thanks so much for your support....

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  In sumValidator… You don't set any maximum

                  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
                  0
                  • F Offline
                    F Offline
                    frfm
                    wrote on last edited by
                    #9

                    SGaist, thanks for your answers, they helped me a lot...Sorry for delay, I came back because I discovered a way which works fine to me, because I also would like to show a error message...I rewrited the method eventFilter(QObject *obj, QEvent *event).

                    @

                    bool Matrix::eventFilter(QObject *obj, QEvent *event){

                    if(keyEvent->key() >= Qt::Key_0 && keyEvent->key() <= Qt::Key_9){

                            int i, idx, r, c, rowSpan, colSpan;
                    
                            if(qApp->focusWidget() == 0)
                                return true;
                    
                            idx = gridLayout->indexOf(qApp->focusWidget());
                    
                            if(idx == -1)
                                return true;
                    
                            gridLayout->getItemPosition(idx, &r, &c, &rowSpan, &colSpan);
                    
                            sum[r] = 0;
                    
                            for(i = 0; i < this->days; i++){
                    
                                if(i != c)
                    
                                    sum[r] += matrix[r][i]->text().toInt();
                            }
                    

                    /*
                    Every time which the SIGNAL valueChanged() is emitted for current lineEdit the value in matrix[i][j] is refreshed with this new value, so I check if sum of current row + value current lineEdit * 10 + Number typed is greater of MAX (this->seeds).
                    */

                            if(this->sum[r] + (matrix[r][c]->text().toInt() * 10 + (keyEvent->key() - Qt::Key_0) ) > this->seeds){
                    
                                QMessageBox error;
                                error.setWindowTitle(tr("Error"));
                                error.setText((QString(tr("Sum in each row should not be greater than %")).arg(this->seeds)));
                                error.exec&#40;&#41;;
                                return true;
                    
                            }
                    

                    }

                    return QObject::eventFilter(obj, event);

                    }

                    @

                    So, when I create each element of matrix I install EventFilter...

                    @

                    ...

                    matrix[i][j]->installEventFilter(this);

                    ...
                    @

                    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