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
#ifndef QRANGEVALIDATOR_H
#define QRANGEVALIDATOR_H
#include <QValidator>
#include <QList>
#include <QString>
struct Range
{
int low;
int high;
};
{
Q_OBJECT
public:
explicit QRangeValidator
(QObject *parent
= 0);
void addRange(int low, int high);
signals:
public slots:
private:
QList<Range *> m_ranges;
};
#endif // QRANGEVALIDATOR_H
#ifndef QRANGEVALIDATOR_H
#define QRANGEVALIDATOR_H
#include <QValidator>
#include <QList>
#include <QString>
struct Range
{
int low;
int high;
};
class QRangeValidator : public QValidator
{
Q_OBJECT
public:
explicit QRangeValidator(QObject *parent = 0);
void addRange(int low, int high);
QValidator::State validate(QString &input, int &pos) const;
signals:
public slots:
private:
QList<Range *> m_ranges;
};
#endif // QRANGEVALIDATOR_H
To copy to clipboard, switch view to plain text mode
cpp file
#include "qrangevalidator.h"
QRangeValidator
::QRangeValidator(QObject *parent
) :{
}
void QRangeValidator::addRange(int low, int high)
{
Range *newRange = new Range;
newRange->low = low;
newRange->high = high;
m_ranges.append(newRange);
}
{
int value = input.toInt();
bool inRanges = false;
foreach(Range *r, m_ranges) {
if ( (value >= r->low) && (value <= r->high) ) {
inRanges = true;
break;
}
}
if (!inRanges)
else
}
#include "qrangevalidator.h"
QRangeValidator::QRangeValidator(QObject *parent) :
QValidator(parent)
{
}
void QRangeValidator::addRange(int low, int high)
{
Range *newRange = new Range;
newRange->low = low;
newRange->high = high;
m_ranges.append(newRange);
}
QValidator::State QRangeValidator::validate(QString &input, int &pos) const
{
int value = input.toInt();
bool inRanges = false;
foreach(Range *r, m_ranges) {
if ( (value >= r->low) && (value <= r->high) ) {
inRanges = true;
break;
}
}
if (!inRanges)
return QValidator::Acceptable;
else
return QValidator::Invalid;
}
To copy to clipboard, switch view to plain text mode
Simple usage example:
#include "widget.h"
#include "ui_widget.h"
ui(new Ui::Widget)
{
ui->setupUi(this);
m_validator = new QRangeValidator;
m_validator->addRange(10, 20);
m_validator->addRange(30, 40);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(checkRange()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::checkRange()
{
int pos = 0;
QString txt
= ui
->lineEdit
->text
();
if (m_validator->validate(txt, pos))
ui->lineEdit->setText("Invalid number");
else
ui->lineEdit->setText("Yes, correct");
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_validator = new QRangeValidator;
m_validator->addRange(10, 20);
m_validator->addRange(30, 40);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(checkRange()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::checkRange()
{
int pos = 0;
QString txt = ui->lineEdit->text();
if (m_validator->validate(txt, pos))
ui->lineEdit->setText("Invalid number");
else
ui->lineEdit->setText("Yes, correct");
}
To copy to clipboard, switch view to plain text mode
Bookmarks