Results 1 to 5 of 5

Thread: Validating multiple discontinuous ranges

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

  2. #2
    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?

  3. #3
    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 

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

    Nivek (19th August 2010)

  5. #4
    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, 09:50
  2. discontinuous QTimeLine problem
    By yagabey in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 21:20
  3. Validating a tabel
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2008, 11:31
  4. Validating QLineEdit
    By OriginalCopy in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2007, 17:12
  5. Validating IP address [SOLVED]
    By deepakn in forum Newbie
    Replies: 0
    Last Post: 28th June 2007, 13: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
  •  
Qt is a trademark of The Qt Company.