diff --git a/CHANGELOG.md b/CHANGELOG.md index 84361c22..b3b225d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ The **"Breaking Changes"** listed below are changes that have been made in the d - Fix the values for some config fields shuffling their order every save. - Fix some problems with tileset detection when importing maps from AdvanceMap. - Fix certain input fields allowing invalid identifiers, like names starting with numbers. +- Fix crash in the Shortcuts Editor when applying changes after closing certain windows. ## [5.4.1] - 2024-03-21 ### Fixed diff --git a/include/ui/shortcutseditor.h b/include/ui/shortcutseditor.h index 31b0279e..e44c8896 100644 --- a/include/ui/shortcutseditor.h +++ b/include/ui/shortcutseditor.h @@ -35,9 +35,9 @@ signals: private: Ui::ShortcutsEditor *ui; QWidget *main_container; - QMultiMap labels_objects; + QMultiMap> labels_objects; QHash contexts_layouts; - QHash multiKeyEdits_objects; + QHash> multiKeyEdits_objects; void parseObjectList(const QObjectList &objectList); QString getLabel(const QObject *object) const; diff --git a/src/ui/shortcutseditor.cpp b/src/ui/shortcutseditor.cpp index 36bc30d0..d0ef943d 100644 --- a/src/ui/shortcutseditor.cpp +++ b/src/ui/shortcutseditor.cpp @@ -2,6 +2,7 @@ #include "ui_shortcutseditor.h" #include "config.h" #include "multikeyedit.h" +#include "message.h" #include "log.h" #include @@ -46,6 +47,15 @@ void ShortcutsEditor::setShortcutableObjects(const QObjectList &shortcutableObje void ShortcutsEditor::saveShortcuts() { QMultiMap objects_keySequences; for (auto it = multiKeyEdits_objects.cbegin(); it != multiKeyEdits_objects.cend(); ++it) { + if (!it.value()) { + // Some shortcuts cannot be saved. Pointers in the object map can become null if they are + // deleted externally while the shortcuts editor is open. Ideally we should try to restore + // the original object so the shortcut can be saved. Alternatively this could generally be + // prevented by making the shortcuts editor modal. For now, saving these shortcuts is skipped, + // and we warn the user that this happened. + ErrorMessage::show(QStringLiteral("Some shortcuts failed to save. Please close the Shortcuts Editor and retry."), this); + return; + } if (it.key()->keySequences().isEmpty()) objects_keySequences.insert(it.value(), QKeySequence()); for (auto keySequence : it.key()->keySequences()) @@ -60,6 +70,7 @@ void ShortcutsEditor::saveShortcuts() { // Restores default shortcuts but doesn't save until Apply or OK is clicked. void ShortcutsEditor::resetShortcuts() { for (auto it = multiKeyEdits_objects.begin(); it != multiKeyEdits_objects.end(); ++it) { + if (!it.value()) continue; it.key()->blockSignals(true); const auto defaults = shortcutsConfig.defaultShortcuts(it.value()); it.key()->setKeySequences(defaults); @@ -90,6 +101,7 @@ bool ShortcutsEditor::stringPropertyIsNotEmpty(const QObject *object, const char void ShortcutsEditor::populateMainContainer() { for (auto object : labels_objects) { + if (!object) continue; const auto shortcutContext = getShortcutContext(object); if (!contexts_layouts.contains(shortcutContext)) addNewContextGroup(shortcutContext);