Prevent restoring invalid/removed IDs of ChecklistModel

The restore function should not add new IDs to the model.
This commit is contained in:
Martchus 2020-04-24 23:06:56 +02:00
parent 6e734159c6
commit 4e974d2258
2 changed files with 14 additions and 3 deletions

View File

@ -10,7 +10,7 @@ set(META_APP_DESCRIPTION
"Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models")
set(META_VERSION_MAJOR 6)
set(META_VERSION_MINOR 0)
set(META_VERSION_PATCH 5)
set(META_VERSION_PATCH 6)
set(META_APP_VERSION ${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH})
project(${META_PROJECT_NAME})

View File

@ -211,8 +211,19 @@ void ChecklistModel::restore(QSettings &settings, const QString &name)
m_items.reserve(rows);
for (int i = 0; i < rows; ++i) {
settings.setArrayIndex(i);
QVariant id = settings.value(QStringLiteral("id"));
QVariant selected = settings.value(QStringLiteral("selected"));
const auto id = settings.value(QStringLiteral("id"));
const auto isIdValid = [&] {
for (const auto &item : currentItems) {
if (item.id() == id) {
return true;
}
}
return false;
}();
if (!isIdValid) {
continue;
}
const auto selected = settings.value(QStringLiteral("selected"));
if (!id.isNull() && !selected.isNull() && selected.canConvert(QMetaType::Bool) && !restoredIds.contains(id)) {
m_items << ChecklistItem(id, labelForId(id), selected.toBool() ? Qt::Checked : Qt::Unchecked);
restoredIds << id;