From 3d52692536e541909f05e178305222b2f69d29c6 Mon Sep 17 00:00:00 2001 From: offtkp Date: Sat, 27 Jan 2024 19:01:05 +0200 Subject: [PATCH] Threading shenanigans --- include/panda_qt/main_window.hpp | 1 + src/panda_qt/cheats_window.cpp | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/panda_qt/main_window.hpp b/include/panda_qt/main_window.hpp index d5eccc93..f7757d73 100644 --- a/include/panda_qt/main_window.hpp +++ b/include/panda_qt/main_window.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/src/panda_qt/cheats_window.cpp b/src/panda_qt/cheats_window.cpp index c6628125..74be1e94 100644 --- a/src/panda_qt/cheats_window.cpp +++ b/src/panda_qt/cheats_window.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "cheats.hpp" #include "emulator.hpp" @@ -23,6 +25,19 @@ struct CheatMetadata { bool enabled = true; }; +void dispatchToMainThread(std::function callback) +{ + QTimer* timer = new QTimer(); + timer->moveToThread(qApp->thread()); + timer->setSingleShot(true); + QObject::connect(timer, &QTimer::timeout, [=]() + { + callback(); + timer->deleteLater(); + }); + QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection, Q_ARG(int, 0)); +} + class CheatEntryWidget : public QWidget { public: CheatEntryWidget(Emulator* emu, CheatMetadata metadata, QListWidget* parent); @@ -181,16 +196,22 @@ void CheatEditDialog::accepted() { } mainWindow->editCheat(cheatEntry.GetMetadata().handle, bytes, [this](u32 handle) { - CheatMetadata metadata = cheatEntry.GetMetadata(); - metadata.handle = handle; - cheatEntry.SetMetadata(metadata); - cheatEntry.Update(); + dispatchToMainThread([this, handle]() { + if (handle == badCheatHandle) { + cheatEntry.Remove(); + return; + } else { + CheatMetadata metadata = cheatEntry.GetMetadata(); + metadata.handle = handle; + cheatEntry.SetMetadata(metadata); + cheatEntry.Update(); + } + }); }); } void CheatEditDialog::rejected() { bool isEditing = cheatEntry.GetMetadata().handle != badCheatHandle; - if (!isEditing) { // Was adding a cheat but pressed cancel cheatEntry.Remove();