Example: migrate settingseditor example to use QRegularExpression
Update the settingseditor example to use the new QRegularExpression class in place of the deprecated QRegExp. Change-Id: I07e34bf916bdde161c4253fca70b853061cd589b Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
parent
a1cf6e5281
commit
57a710467b
@ -56,14 +56,14 @@ VariantDelegate::VariantDelegate(QObject *parent)
|
|||||||
: QItemDelegate(parent)
|
: QItemDelegate(parent)
|
||||||
{
|
{
|
||||||
boolExp.setPattern("true|false");
|
boolExp.setPattern("true|false");
|
||||||
boolExp.setCaseSensitivity(Qt::CaseInsensitive);
|
boolExp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
|
||||||
|
|
||||||
byteArrayExp.setPattern("[\\x00-\\xff]*");
|
byteArrayExp.setPattern("[\\x00-\\xff]*");
|
||||||
charExp.setPattern(".");
|
charExp.setPattern(".");
|
||||||
colorExp.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)");
|
colorExp.setPattern("^\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)$");
|
||||||
doubleExp.setPattern("");
|
doubleExp.setPattern("");
|
||||||
pointExp.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)");
|
pointExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*)\\)$");
|
||||||
rectExp.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)");
|
rectExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)$");
|
||||||
signedIntegerExp.setPattern("-?[0-9]*");
|
signedIntegerExp.setPattern("-?[0-9]*");
|
||||||
sizeExp = pointExp;
|
sizeExp = pointExp;
|
||||||
unsignedIntegerExp.setPattern("[0-9]*");
|
unsignedIntegerExp.setPattern("[0-9]*");
|
||||||
@ -104,7 +104,7 @@ QWidget *VariantDelegate::createEditor(QWidget *parent,
|
|||||||
QLineEdit *lineEdit = new QLineEdit(parent);
|
QLineEdit *lineEdit = new QLineEdit(parent);
|
||||||
lineEdit->setFrame(false);
|
lineEdit->setFrame(false);
|
||||||
|
|
||||||
QRegExp regExp;
|
QRegularExpression regExp;
|
||||||
|
|
||||||
switch (originalValue.type()) {
|
switch (originalValue.type()) {
|
||||||
case QVariant::Bool:
|
case QVariant::Bool:
|
||||||
@ -152,8 +152,8 @@ QWidget *VariantDelegate::createEditor(QWidget *parent,
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!regExp.isEmpty()) {
|
if (regExp.isValid()) {
|
||||||
QValidator *validator = new QRegExpValidator(regExp, lineEdit);
|
QValidator *validator = new QRegularExpressionValidator(regExp, lineEdit);
|
||||||
lineEdit->setValidator(validator);
|
lineEdit->setValidator(validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,17 +185,18 @@ void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|||||||
|
|
||||||
QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
||||||
QVariant value;
|
QVariant value;
|
||||||
|
QRegularExpressionMatch match;
|
||||||
|
|
||||||
switch (originalValue.type()) {
|
switch (originalValue.type()) {
|
||||||
case QVariant::Char:
|
case QVariant::Char:
|
||||||
value = text.at(0);
|
value = text.at(0);
|
||||||
break;
|
break;
|
||||||
case QVariant::Color:
|
case QVariant::Color:
|
||||||
colorExp.exactMatch(text);
|
match = colorExp.match(text);
|
||||||
value = QColor(qMin(colorExp.cap(1).toInt(), 255),
|
value = QColor(qMin(match.captured(1).toInt(), 255),
|
||||||
qMin(colorExp.cap(2).toInt(), 255),
|
qMin(match.captured(2).toInt(), 255),
|
||||||
qMin(colorExp.cap(3).toInt(), 255),
|
qMin(match.captured(3).toInt(), 255),
|
||||||
qMin(colorExp.cap(4).toInt(), 255));
|
qMin(match.captured(4).toInt(), 255));
|
||||||
break;
|
break;
|
||||||
case QVariant::Date:
|
case QVariant::Date:
|
||||||
{
|
{
|
||||||
@ -214,17 +215,17 @@ void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case QVariant::Point:
|
case QVariant::Point:
|
||||||
pointExp.exactMatch(text);
|
match = pointExp.match(text);
|
||||||
value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
|
value = QPoint(match.captured(1).toInt(), match.captured(2).toInt());
|
||||||
break;
|
break;
|
||||||
case QVariant::Rect:
|
case QVariant::Rect:
|
||||||
rectExp.exactMatch(text);
|
match = rectExp.match(text);
|
||||||
value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
|
value = QRect(match.captured(1).toInt(), match.captured(2).toInt(),
|
||||||
rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
|
match.captured(3).toInt(), match.captured(4).toInt());
|
||||||
break;
|
break;
|
||||||
case QVariant::Size:
|
case QVariant::Size:
|
||||||
sizeExp.exactMatch(text);
|
match = sizeExp.match(text);
|
||||||
value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
|
value = QSize(match.captured(1).toInt(), match.captured(2).toInt());
|
||||||
break;
|
break;
|
||||||
case QVariant::StringList:
|
case QVariant::StringList:
|
||||||
value = text.split(',');
|
value = text.split(',');
|
||||||
|
@ -52,7 +52,7 @@
|
|||||||
#define VARIANTDELEGATE_H
|
#define VARIANTDELEGATE_H
|
||||||
|
|
||||||
#include <QItemDelegate>
|
#include <QItemDelegate>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
class VariantDelegate : public QItemDelegate
|
class VariantDelegate : public QItemDelegate
|
||||||
{
|
{
|
||||||
@ -73,19 +73,19 @@ public:
|
|||||||
static QString displayText(const QVariant &value);
|
static QString displayText(const QVariant &value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable QRegExp boolExp;
|
mutable QRegularExpression boolExp;
|
||||||
mutable QRegExp byteArrayExp;
|
mutable QRegularExpression byteArrayExp;
|
||||||
mutable QRegExp charExp;
|
mutable QRegularExpression charExp;
|
||||||
mutable QRegExp colorExp;
|
mutable QRegularExpression colorExp;
|
||||||
mutable QRegExp dateExp;
|
mutable QRegularExpression dateExp;
|
||||||
mutable QRegExp dateTimeExp;
|
mutable QRegularExpression dateTimeExp;
|
||||||
mutable QRegExp doubleExp;
|
mutable QRegularExpression doubleExp;
|
||||||
mutable QRegExp pointExp;
|
mutable QRegularExpression pointExp;
|
||||||
mutable QRegExp rectExp;
|
mutable QRegularExpression rectExp;
|
||||||
mutable QRegExp signedIntegerExp;
|
mutable QRegularExpression signedIntegerExp;
|
||||||
mutable QRegExp sizeExp;
|
mutable QRegularExpression sizeExp;
|
||||||
mutable QRegExp timeExp;
|
mutable QRegularExpression timeExp;
|
||||||
mutable QRegExp unsignedIntegerExp;
|
mutable QRegularExpression unsignedIntegerExp;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user