[Solved] QInputDialog::getText() and QRegExp



  • 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?



  • 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 characters

    EDIT: 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



  • 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



  • Thx for the reply, I will investigate them.


  • Lifetime Qt Champion

    Hi,

    Did you consider to write your own input dialog using a QRegExpValidator on the line edit ?

    Hope it helps



  • 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 &regExp);
    
    QString getLabelText();
    QString getText();
    
    static QString getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp &regExp, 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 &regExp){
    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 &regExp, 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)



  • Thx jazzycamel, that's exactly what I want to achieve :) .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.