QT - QInputDialog How to Validate?
Asked Answered
G

2

6

I would like to add some type of validation to my QInputDialog. I use the input of the dialog to create a file system path. So I would like to exclude characters such as @$#%^&*() but keep - and _. I was thinking of applying a regexp pattern but I'm not sure of the workflow.

If its not possible or it makes sense to use something different I'm open to that as well.

This is what I'm currently using:

QString defaultText("whatever");
bool ok;
QString caseInput = QInputDialog::getText(this, tr("Input Text"), tr("New Text:"), QLineEdit::Normal, defaultText, &ok);

if (ok && !caseInput.isEmpty())
{
   // do stuff
}
Glantz answered 8/11, 2013 at 17:56 Comment(0)
H
7

So if you want full control of it, you will want to make your own QDialog, add in a QLabel for the text, and add in a line edit, setup a QValidator, and access the return value afterwards.

Like so:

mydialog.h

#include <QDialog>
#include <QLineEdit>

class MyDialog : public QDialog
{
    Q_OBJECT

public:
    MyDialog(QWidget *parent = 0);
    ~MyDialog();
    QString getNewValue();

signals:
    //void rejected();
    //void accepted();

public slots:


private:
    QLineEdit * le;
};

mydialog.cpp

#include "mydialog.h"
#include <QDialogButtonBox>
#include <QRegExpValidator>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QLabel>

MyDialog::MyDialog(QWidget *parent)
    : QDialog(parent)
{
    le = 0;
    this->setAttribute(Qt::WA_QuitOnClose, false);

    QVBoxLayout * vbox = new QVBoxLayout;

    vbox->addWidget(new QLabel(tr("Type in your text:")));

    le = new QLineEdit();

    // le->setText(tr("Profile"));
    // le->selectAll();
    le->setPlaceholderText(tr("Profile"));

    vbox->addWidget(le);

    QRegExpValidator * v = new QRegExpValidator(QRegExp("[\\w\\d_ \\.]{24}"));
    le->setValidator(v);


    QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
        | QDialogButtonBox::Cancel);
    vbox->addWidget(buttonBox);
    this->setLayout(vbox);

     // connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(accepted()));
     // connect(buttonBox, SIGNAL(rejected()), this, SIGNAL(rejected()));
}

MyDialog::~MyDialog()
{

}

QString MyDialog::getNewValue()
{
        return le->text();
}

Example usage:

MyDialog dialog;
if(dialog.exec() == QDialog::Accepted)
{
    QString retVal = dialog.getNewValue();
    qDebug() << "Dialog value:" << retVal;
}

Another way to achieve almost the same thing:

http://qt-project.org/doc/qt-4.8/qlineedit.html#inputMask-prop http://qt-project.org/doc/qt-4.8/widgets-lineedits.html

If you want to use the stock getText QInputDialog you can set the field for InputMethodHint:

http://qt-project.org/doc/qt-4.8/qinputdialog.html#getText

http://qt-project.org/doc/qt-4.8/qt.html#InputMethodHint-enum

But the QRegExp is the most powerful in my opinion.

Here are some good examples of QRegExp in this class:

http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter-highlighter-cpp.html

 classFormat.setFontWeight(QFont::Bold);
 classFormat.setForeground(Qt::darkMagenta);
 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
 rule.format = classFormat;
 highlightingRules.append(rule);

 singleLineCommentFormat.setForeground(Qt::red);
 rule.pattern = QRegExp("//[^\n]*");
 rule.format = singleLineCommentFormat;
 highlightingRules.append(rule);

 multiLineCommentFormat.setForeground(Qt::red);

 quotationFormat.setForeground(Qt::darkGreen);
 rule.pattern = QRegExp("\".*\"");
 rule.format = quotationFormat;
 highlightingRules.append(rule);

 functionFormat.setFontItalic(true);
 functionFormat.setForeground(Qt::blue);
 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
 rule.format = functionFormat;
 highlightingRules.append(rule);

 commentStartExpression = QRegExp("/\\*");
 commentEndExpression = QRegExp("\\*/");

Hope that helps.

Homes answered 9/11, 2013 at 17:7 Comment(1)
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())) is even cooler. The same with the rejected signal.Bendicta
B
0

Probably the most simplest way to validate text in QInputDialog is using QRegularExpressionValidator. It should be applied for QLineEdit of QInputDialog. But QLineEdit is private and can't be used directly. However, there is a little trick: using QObject::findChild(). Here is a simple example:

QInputDialog dlg(this);
dlg.setWindowTitle(tr("Input Text"));
dlg.setLabelText(tr("New Text:"));
...
dlg.show(); // it's required to create internal QLineEdit
auto le = dlg.findChild<QLineEdit *>();
Q_ASSERT(le);
auto validator = new QRegularExpressionValidator(le);
validator->setRegularExpression(QRegularExpression("<your RegExp pattern>"));
le->setValidator(validator);
if (dlg.exec() == QDialog::Accepted) {
    const auto &text = dlg.textValue().trimmed();
    ...
}
Bartel answered 20/9 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.