[Solved] QInputDialog::getText() and QRegExp
-
wrote on 13 Feb 2013, 14:06 last edited by
Hi,
I am using QInputDialog::getText() to get a string from the user. I would like to add some constraints on the string, using a QRegExp, but according to the doc, there is no such feature with QInputDialog (Qt::InputMethodHints doesn't fit my needs). What is the better way to do such a thing?
-
wrote on 13 Feb 2013, 14:27 last edited by
I think, you should use QRegExp by your input object (e.g. QLineEdit, QTextEdit).
It contrains the editing of the object, so the user cannot type in wrong charactersEDIT: Another way is to check, if the string is in the right format:
[code] QRegExp rx("*.txt");
rx.setPatternSyntax(QRegExp::Wildcard);
rx.exactMatch("README.txt"); // returns true
rx.exactMatch("welcome.txt.bak"); // returns false[/code]
See also: http://qt-project.org/doc/qt-4.8/qregexp.html -
wrote on 13 Feb 2013, 18:52 last edited by
Hi,
The way to go is probably to catch the close event and check with a QRegExp the stuff that is inserted (like Serenity does). When the string is oke (or empty to escape it) you can accept the close event. If not you could ignore the event with maybe an information box on top to indicate that the user should do it right this time. Or maybe catch some other event or signal (inputchanged or something).
Greetz -
wrote on 14 Feb 2013, 09:08 last edited by
Thx for the reply, I will investigate them.
-
Hi,
Did you consider to write your own input dialog using a QRegExpValidator on the line edit ?
Hope it helps
-
wrote on 14 Feb 2013, 10:17 last edited by
SGaist was just touch quicker than me, I've actually done this before as below:
regexpinputdialog.h
@
#ifndef REGEXPINPUTDIALOG_H
#define REGEXPINPUTDIALOG_H#include <QDialog>
class QLabel;
class QLineEdit;
class QDialogButtonBox;
class QRegExpValidator;class RegExpInputDialog : public QDialog
{
Q_OBJECT
public:
explicit RegExpInputDialog(QWidget *parent = 0, Qt::WindowFlags=0);void setTitle(const QString &title); void setLabelText(const QString &label); void setText(const QString &text); void setRegExp(const QRegExp ®Exp); QString getLabelText(); QString getText(); static QString getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp ®Exp, bool *ok, Qt::WindowFlags flags=0);
signals:
private slots:
void checkValid(const QString &text);private:
QLabel *label;
QLineEdit *text;
QDialogButtonBox *buttonBox;
QRegExp regExp;
QRegExpValidator *validator;
};#endif // REGEXPINPUTDIALOG_H
@regexpinputdialog.cpp
@
#include <QtGui>
#include "regexpinputdialog.h"RegExpInputDialog::RegExpInputDialog(QWidget *parent, Qt::WindowFlags flags) :
QDialog(parent)
{
if(flags!=0) setWindowFlags(flags);QVBoxLayout *l=new QVBoxLayout(this); label=new QLabel(this); regExp=QRegExp("*"); regExp.setPatternSyntax(QRegExp::Wildcard); validator=new QRegExpValidator(regExp); text=new QLineEdit(this); text->setValidator(validator); connect(text, SIGNAL(textChanged(QString)), this, SLOT(checkValid(QString))); buttonBox=new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal, this); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); l->addWidget(label); l->addWidget(text); l->addWidget(buttonBox);
}
void RegExpInputDialog::setTitle(const QString &title){ setWindowTitle(title); }
void RegExpInputDialog::setLabelText(const QString &label){ this->label->setText(label); }
void RegExpInputDialog::setText(const QString &text){ this->text->setText(text); }void RegExpInputDialog::setRegExp(const QRegExp ®Exp){
validator->setRegExp(regExp);
checkValid(text->text());
}QString RegExpInputDialog::getLabelText(){ return label->text(); }
QString RegExpInputDialog::getText(){ return text->text(); }void RegExpInputDialog::checkValid(const QString &text){
QString _text=QString(text);
int pos=0;
bool valid=validator->validate(_text, pos)==QRegExpValidator::Acceptable;
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}QString RegExpInputDialog::getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp ®Exp, bool *ok, Qt::WindowFlags flags){
RegExpInputDialog *r=new RegExpInputDialog(parent, flags);
r->setTitle(title);
r->setLabelText(label);
r->setText(text);
r->setRegExp(regExp);
*ok=r->exec()==QDialog::Accepted;
if(*ok) return r->getText();
else return QString();
}
@Usage example
@
...
bool ok;
QRegExp regExp("*.txt");
regExp.setPatternSyntax(QRegExp::Wildcard);
QString text=RegExpInputDialog::getText(this, "Title", "Label", "Text", regExp, &ok);
qDebug() << "Text:" << text;
...
@Hope this helps ;o)
-
wrote on 14 Feb 2013, 15:55 last edited by
Thx jazzycamel, that's exactly what I want to achieve :) .
2/7