Qt之正则表达式一二三

/**
*    RegExpTest.h
*/
#include 
#include 
class QLabel;
class QLineEdit;
class QRegExp;
class QRegExpValidator;
class QVBoxLayout;
class QString;

class Parser : public QValidator
{
    Q_OBJECT
public:
    explicit Parser(QObject *parent = 0);
    QValidator::State validate(QString& input,int& pos)const; //override
};

class RegExpTest : public QDialog
{
	Q_OBJECT

public:
	RegExpTest(QWidget *parent = 0, Qt::WFlags flags = 0);
	~RegExpTest();
	void InterfaceInit();

public slots:
	void DisPlayInfoLab(QString);
    void DisPlayForTestRef(QString);
    void ParserTestLab(QString);
private:
	QLabel *DisPlayLab;
	QLabel *DisPlayAllLab;
	QLineEdit *InputLineEdit;
	QVBoxLayout *MainLayout;
    Parser *m_parser;
};//RegExpTest.h
/**
*    RegExpTest.cpp
*/
#include 
#include 
#include 
#include 
#include 
#include 
RegExpTest::RegExpTest(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	InterfaceInit();
	this->setCursor(Qt::CrossCursor);
    this->setWindowTitle("QRegExp");
}

RegExpTest::~RegExpTest()
{

}

void RegExpTest::InterfaceInit()
{
	DisPlayLab = new QLabel("Input");
	DisPlayAllLab = new QLabel;
	InputLineEdit = new QLineEdit();
	MainLayout = new QVBoxLayout();
    m_parser = new Parser(this);
	this->setLayout(MainLayout);
	MainLayout->addWidget(DisPlayLab);
	MainLayout->addWidget(InputLineEdit);
	MainLayout->addWidget(DisPlayAllLab);
	DisPlayAllLab->setStyleSheet("Background-color:rgb(205,173,0)");
	this->setStyleSheet("Background-color:rgb(238,238,209)");
	InputLineEdit->setMaxLength(6);
	//connect(InputLineEdit,SIGNAL(textChanged(QString)),this,SLOT(DisPlayInfoLab(QString)));
 //   connect(InputLineEdit,SIGNAL(textChanged(QString)),this,SLOT(DisPlayForTestRef(QString)));
    connect(InputLineEdit,SIGNAL(textChanged(QString)),this,SLOT(ParserTestLab(QString)));
}
/* Method 1: 不能直接使用虚类QValidator,只能使用其派生类QIntValidator和QDoubleValidator,
*  前者不能输入Double,主要对输入的数据的大小进行限制*/
void RegExpTest::DisPlayInfoLab(QString strInfo)
{
    QDoubleValidator* isValid(new QDoubleValidator());
    InputLineEdit->setValidator(isValid);
	DisPlayAllLab->setText(InputLineEdit->text()+"\n");
}
/* Method 2: 正则表达式,一般使用这个*/
void RegExpTest::DisPlayForTestRef(QString strInfo)
{
    QRegExp strRegExp("^2[0-9]{5}$");
    QRegExpValidator *InputRegExpValid(new QRegExpValidator(strRegExp,this));
    InputLineEdit->setValidator(InputRegExpValid);
    QString strLineEditInfo = InputLineEdit->text();
    DisPlayAllLab->setText(strLineEditInfo + "\n");
}
/*子类化QValidator后使用重载的validate()*/
void RegExpTest::ParserTestLab(QString strInfo)
{
    QString strLineEdit = InputLineEdit->text();
    int pos = 0;
    const QString str = "123";
    if(m_parser->validate(strLineEdit,pos) == QValidator::Acceptable)
    {
        strLineEdit = strLineEdit.remove(str);
        DisPlayAllLab->setText(strLineEdit + "\n");
    }else{
        strLineEdit = strLineEdit;
        DisPlayAllLab->setText(strLineEdit);
    }
}

Parser::Parser(QObject *parent)
{

}
/* Method 3: 如果有一些是正则表达式不能处理的可以使用这个方式处理---子类化QValidator,
*  必须要重载validator函数,因为在QValidator类中validate是个接口*/
QValidator::State Parser::validate(QString& strInput,int& CursorPos)const
{
    QString StrInput(strInput);
    static QValidator::State CurState;
    if( StrInput.contains("123"))
    {
       CurState = Acceptable;
    }else{
       CurState = Intermediate;
    }
    return CurState;
}

 

你可能感兴趣的:(Qt学习总结)