Results 1 to 5 of 5

Thread: Validating multiple discontinuous ranges

  1. #1
    Join Date
    Aug 2010
    Posts
    11
    Thanks
    1

    Question Validating multiple discontinuous ranges

    I have a large amount of generated QLineEdits that I want to put numerical range validation on. QIntValidator provides this for a single range, but I want to make it possible to validate on an arbitrary number of discontinuous ranges as read from an XML file. For example, I may want the lineEdit to be able to take integers in the range of (0-50), (100-200) and (1000-1400), but not the ranges in between those.

    I had the idea of reimplementing QIntValidator to contain a list of QIntValidators for each range I want to add to it, but looking at the documentation further I don't think that will work very well.

    I guess the other option would be parsing the xml myself and constructing regular expressions at run time for use in QRegExpValidator, but that seems excessive in terms of both time and effort and performance for the payoff.

    This would be simple if Qt allowed multiple validators to be put on one object, but if you set a more than one validator the second one replaces the first one.

    Thanks in advance for any ideas.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Validating multiple discontinuous ranges

    Why not create your own validator?

  3. #3
    Join Date
    Aug 2010
    Posts
    11
    Thanks
    1

    Default Re: Validating multiple discontinuous ranges

    Why not create your own validator?
    Well, a good way of doing that is essentially what I'm asking about. How would I go about doing that? Could I make a validator that keeps its own list of one validator for each separate range?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Validating multiple discontinuous ranges

    This is a five second example. The logic is all wrong, that's up to you to fix :-)
    But this is the idea:

    header file
    Qt Code:
    1. #ifndef QRANGEVALIDATOR_H
    2. #define QRANGEVALIDATOR_H
    3.  
    4. #include <QValidator>
    5.  
    6. #include <QList>
    7. #include <QString>
    8.  
    9. struct Range
    10. {
    11. int low;
    12. int high;
    13. };
    14.  
    15. class QRangeValidator : public QValidator
    16. {
    17. Q_OBJECT
    18. public:
    19. explicit QRangeValidator(QObject *parent = 0);
    20.  
    21. void addRange(int low, int high);
    22.  
    23. QValidator::State validate(QString &input, int &pos) const;
    24.  
    25. signals:
    26.  
    27. public slots:
    28.  
    29. private:
    30. QList<Range *> m_ranges;
    31.  
    32. };
    33.  
    34. #endif // QRANGEVALIDATOR_H
    To copy to clipboard, switch view to plain text mode 

    cpp file
    Qt Code:
    1. #include "qrangevalidator.h"
    2.  
    3. QRangeValidator::QRangeValidator(QObject *parent) :
    4. QValidator(parent)
    5. {
    6. }
    7.  
    8. void QRangeValidator::addRange(int low, int high)
    9. {
    10. Range *newRange = new Range;
    11. newRange->low = low;
    12. newRange->high = high;
    13.  
    14. m_ranges.append(newRange);
    15. }
    16.  
    17. QValidator::State QRangeValidator::validate(QString &input, int &pos) const
    18. {
    19. int value = input.toInt();
    20. bool inRanges = false;
    21.  
    22. foreach(Range *r, m_ranges) {
    23. if ( (value >= r->low) && (value <= r->high) ) {
    24. inRanges = true;
    25. break;
    26. }
    27. }
    28.  
    29. if (!inRanges)
    30. return QValidator::Acceptable;
    31. else
    32. return QValidator::Invalid;
    33. }
    To copy to clipboard, switch view to plain text mode 


    Simple usage example:
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9.  
    10. m_validator = new QRangeValidator;
    11.  
    12. m_validator->addRange(10, 20);
    13. m_validator->addRange(30, 40);
    14.  
    15. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(checkRange()));
    16. }
    17.  
    18. Widget::~Widget()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void Widget::checkRange()
    24. {
    25. int pos = 0;
    26. QString txt = ui->lineEdit->text();
    27.  
    28. if (m_validator->validate(txt, pos))
    29. ui->lineEdit->setText("Invalid number");
    30. else
    31. ui->lineEdit->setText("Yes, correct");
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tbscope for this useful post:

    Nivek (19th August 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    11
    Thanks
    1

    Default Re: Validating multiple discontinuous ranges

    Thanks tbscope, I'll try that out!

Similar Threads

  1. Limit window size to certain ranges
    By markwestcott in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 10:50
  2. discontinuous QTimeLine problem
    By yagabey in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 22:20
  3. Validating a tabel
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2008, 12:31
  4. Validating QLineEdit
    By OriginalCopy in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2007, 18:12
  5. Validating IP address [SOLVED]
    By deepakn in forum Newbie
    Replies: 0
    Last Post: 28th June 2007, 14:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.