Qt: Clean up memory model in ConfigController
Vicki Pfau vi@endrift.com
Sat, 23 Jan 2021 20:25:19 -0800
2 files changed,
14 insertions(+),
14 deletions(-)
M
src/platform/qt/ConfigController.cpp
→
src/platform/qt/ConfigController.cpp
@@ -23,7 +23,7 @@ }
void ConfigOption::connect(std::function<void(const QVariant&)> slot, QObject* parent) { m_slots[parent] = slot; - QObject::connect(parent, &QObject::destroyed, [this, slot, parent]() { + QObject::connect(parent, &QObject::destroyed, this, [this, parent]() { m_slots.remove(parent); }); }@@ -37,10 +37,10 @@ QString name = QString("%1.%2").arg(m_name).arg(value.toString());
if (actions) { action = actions->addAction(text, name, function, menu); } else { - action = new Action(function, name, text); + action = new Action(function, name, text, this); } action->setExclusive(); - QObject::connect(action, &QObject::destroyed, [this, action, value]() { + QObject::connect(action, &QObject::destroyed, this, [this, action, value]() { m_actions.removeAll(std::make_pair(action, value)); }); m_actions.append(std::make_pair(action, value));@@ -59,10 +59,10 @@ };
if (actions) { action = actions->addBooleanAction(text, m_name, function, menu); } else { - action = new Action(function, m_name, text); + action = new Action(function, m_name, text, this); } - QObject::connect(action, &QObject::destroyed, [this, action]() { + QObject::connect(action, &QObject::destroyed, this, [this, action]() { m_actions.removeAll(std::make_pair(action, 1)); }); m_actions.append(std::make_pair(action, 1));@@ -103,7 +103,7 @@ {
QString fileName = configDir(); fileName.append(QDir::separator()); fileName.append("qt.ini"); - m_settings = new QSettings(fileName, QSettings::IniFormat, this); + m_settings = std::make_unique<QSettings>(fileName, QSettings::IniFormat); mCoreConfigInit(&m_config, PORT);@@ -150,7 +150,7 @@ return m_optionSet[optionName];
} ConfigOption* newOption = new ConfigOption(optionName, this); m_optionSet[optionName] = newOption; - connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) { + connect(newOption, &ConfigOption::valueChanged, this, [this, key](const QVariant& value) { setOption(key, value); }); return newOption;@@ -292,12 +292,11 @@
QString fileName(configDir()); fileName.append(QDir::separator()); fileName.append("qt.ini"); - QSettings* settings2 = new QSettings(fileName, QSettings::IniFormat, this); + auto settings2 = std::make_unique<QSettings>(fileName, QSettings::IniFormat); for (const auto& key : m_settings->allKeys()) { settings2->setValue(key, m_settings->value(key)); } - delete m_settings; - m_settings = settings2; + m_settings = std::move(settings2); } bool ConfigController::isPortable() {
M
src/platform/qt/ConfigController.h
→
src/platform/qt/ConfigController.h
@@ -7,12 +7,13 @@ #pragma once
#include "Override.h" -#include <QMap> +#include <QHash> #include <QObject> #include <QSettings> #include <QVariant> #include <functional> +#include <memory> #include <mgba/core/config.h> #include <mgba-util/configuration.h>@@ -53,7 +54,7 @@ signals:
void valueChanged(const QVariant& value); private: - QMap<QObject*, std::function<void(const QVariant&)>> m_slots; + QHash<QObject*, std::function<void(const QVariant&)>> m_slots; QList<std::pair<Action*, QVariant>> m_actions; QString m_name; };@@ -110,8 +111,8 @@
mCoreConfig m_config; mCoreOptions m_opts{}; - QMap<QString, ConfigOption*> m_optionSet; - QSettings* m_settings; + QHash<QString, ConfigOption*> m_optionSet; + std::unique_ptr<QSettings> m_settings; static QString s_configDir; };