From a83b2c64a9828a1d22347eaf31fd251b2ef647ee Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 10 Jul 2020 08:09:56 +0200 Subject: 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 --- examples/widgets/tools/settingseditor/settingstree.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'examples/widgets/tools/settingseditor/settingstree.cpp') 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 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)); -- cgit v1.2.3