diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-10 08:09:56 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-10 11:31:09 +0000 |
commit | a83b2c64a9828a1d22347eaf31fd251b2ef647ee (patch) | |
tree | b8d5dae083dddd43c6565514db8e7cee754f91fd /examples/widgets/tools/settingseditor/settingstree.cpp | |
parent | 9ac5273d03c176156596a23d2a88cf95f0b4d6d6 (diff) |
Polish the settingseditor example
The example is meant to show an item delegate with a line edit with
QRegularExpression-based validation depending on type.
Unfortunately, this does not work since QSettings mostly
return QString types.
Fix it to a partially working state by
- Making the expressions match from beginning to end which
was overlooked in the QRegExp->QRegularExpression change.
- Use QCheckBox, QSpinBox for bool/int since it is silly
to have a user edit a bool value by typing 'true'/'false'.
- Move the expressions out to a separate struct to be
able to do some guessing of the type when reading
the QSettings, implement for bool and int.
- Use a fancy Unicode checkmark for displaying bools.
- Fix the garbled display of QByteArray with binary data
by displaying them with hex characters and setting them
read-only.
Change-Id: Iba22dfafc3b813b3fd3d2915ef5210d661049382
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Diffstat (limited to 'examples/widgets/tools/settingseditor/settingstree.cpp')
-rw-r--r-- | examples/widgets/tools/settingseditor/settingstree.cpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/examples/widgets/tools/settingseditor/settingstree.cpp b/examples/widgets/tools/settingseditor/settingstree.cpp index 9132368e4a..04af8ce3b9 100644 --- a/examples/widgets/tools/settingseditor/settingstree.cpp +++ b/examples/widgets/tools/settingseditor/settingstree.cpp @@ -57,9 +57,10 @@ #include <QSettings> SettingsTree::SettingsTree(QWidget *parent) - : QTreeWidget(parent) + : QTreeWidget(parent), + m_typeChecker(new TypeChecker) { - setItemDelegate(new VariantDelegate(this)); + setItemDelegate(new VariantDelegate(m_typeChecker, this)); setHeaderLabels({tr("Setting"), tr("Type"), tr("Value")}); header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); @@ -77,6 +78,8 @@ SettingsTree::SettingsTree(QWidget *parent) connect(&refreshTimer, &QTimer::timeout, this, &SettingsTree::maybeRefresh); } +SettingsTree::~SettingsTree() = default; + void SettingsTree::setSettingsObject(const SettingsPtr &newSettings) { settings = newSettings; @@ -211,6 +214,14 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent) if (value.userType() == QMetaType::UnknownType) { child->setText(1, "Invalid"); } else { + if (value.type() == QVariant::String) { + const QString stringValue = value.toString(); + if (m_typeChecker->boolExp.match(stringValue).hasMatch()) { + value.setValue(stringValue.compare("true", Qt::CaseInsensitive) == 0); + } else if (m_typeChecker->signedIntegerExp.match(stringValue).hasMatch()) + value.setValue(stringValue.toInt()); + } + child->setText(1, value.typeName()); } child->setText(2, VariantDelegate::displayText(value)); |