//klasa Widget//
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QLineEdit* edit;
public slots:
void onReturnPressed();
};
//////////////////////////////////////
//konstruktor QWidget//
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
edit = new QLineEdit(this);
connect(edit,SIGNAL(returnPressed()),this,SLOT(onReturnPressed()));
}
//////////////////////////////////////////////////////////
//klasa dziedzicząca od QValidator///
#include <QObject>
#include <QValidator>
#include <QRegExp>
#include <QRegExpValidator>
#define CORRECT_NUMBER "ISBN\\x20(?=.{13}$)\\d{1,5}" \
"([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$"
class MyValidator : public QValidator
{
Q_OBJECT
public:
explicit MyValidator(QObject *parent = nullptr);
QValidator::State validate(QString & text, int &) const
{
QRegExp regEx(CORRECT_NUMBER);
QRegExpValidator validator(regEx);
bool solution = regEx.exactMatch(text);
if(solution)
return QValidator::Acceptable;
else
return QValidator::Invalid;
}
};
////////////////////////////////////////
//slot "onReturnPressed"
void Widget::onReturnPressed()
{
MyValidator *valid;
valid = new MyValidator;
QString text = edit->text();
int zero = 0;
auto solution = valid->validate(text,zero);
if(solution == QValidator::Acceptable)
{
QMessageBox::information(this,"Congrats","Number is valid",QMessageBox::Ok);
}
}
//////////////////////////////////
//main()
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Cześć, robię program z QValidator, ma on wyświetlić komunikat, jeżeli podany numer jest poprawnym numerem ISBN. Powyższy kod niestety nie działa :( . Jak mogę go poprawić?