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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With