PDA

View Full Version : editing string in QLineEdit with QRegExpValidator



Al_
28th April 2012, 11:51
Hi
I have a QLineEdit with a QRegExpValidator set. Now I want to allow my users to edit the string, e.g., insert characters in the middle of the string. How can I accomplish this?

Example: A QLineEdit with QRegExpValidator regExpValidator(QRegExp("I have (2 )?cats"), NULL); as validator allows to enter "I have cats". So far, so good. But: it should also allow to subsequently position the cursor in front of the 'c', then type '2', then space. It should allow to type '2' as the validation result should be QValidator::Intermediate because the expression might become valid when continuing to type at the cursor position. But apparently editing only works for appending text, not for inserting text; the second argument of QValidator::validate ( QString & input, int & pos ) const seems to be ignored (or only used as out parameter).

Is there a way to modify this behaviour? Thanks for any hint!

Al_

Complete application to test

#include <QRegExpValidator>
#include <QLineEdit>
#include <QApplication>

int main(int argc, char *argv[]){
QApplication app(argc, argv);
QLineEdit lineEdit(0);
QRegExpValidator regExpValidator(QRegExp("I have (2 )?cats"), NULL);
lineEdit.setValidator(&regExpValidator);
lineEdit.show();
return app.exec();}