From 821e15f1eeba75f3cdd196d7296f6ea31c9c854c Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 6 Apr 2022 20:49:00 +1000 Subject: [PATCH] Qt: Add QtHost::RunOnUIThread() --- pcsx2-qt/MainWindow.cpp | 5 +++++ pcsx2-qt/MainWindow.h | 2 ++ pcsx2-qt/QtHost.cpp | 8 ++++++++ pcsx2-qt/QtHost.h | 3 +++ 4 files changed, 18 insertions(+) diff --git a/pcsx2-qt/MainWindow.cpp b/pcsx2-qt/MainWindow.cpp index df8180e6627f5..406498aca0a24 100644 --- a/pcsx2-qt/MainWindow.cpp +++ b/pcsx2-qt/MainWindow.cpp @@ -696,6 +696,11 @@ void MainWindow::reportError(const QString& title, const QString& message) QMessageBox::critical(this, title, message); } +void MainWindow::runOnUIThread(const std::function& func) +{ + func(); +} + bool MainWindow::requestShutdown(bool allow_confirm /* = true */, bool allow_save_to_state /* = true */, bool block_until_done /* = false */) { if (!VMManager::HasValidVM()) diff --git a/pcsx2-qt/MainWindow.h b/pcsx2-qt/MainWindow.h index 3ea65740611f2..0ff57c8cafd32 100644 --- a/pcsx2-qt/MainWindow.h +++ b/pcsx2-qt/MainWindow.h @@ -17,6 +17,7 @@ #include #include +#include #include #include "Settings/ControllerSettingsDialog.h" @@ -55,6 +56,7 @@ public Q_SLOTS: void refreshGameList(bool invalidate_cache); void invalidateSaveStateCache(); void reportError(const QString& title, const QString& message); + void runOnUIThread(const std::function& func); bool requestShutdown(bool allow_confirm = true, bool allow_save_to_state = true, bool block_until_done = false); void requestExit(); diff --git a/pcsx2-qt/QtHost.cpp b/pcsx2-qt/QtHost.cpp index 446cc9b225936..7faca2e94b5d8 100644 --- a/pcsx2-qt/QtHost.cpp +++ b/pcsx2-qt/QtHost.cpp @@ -300,6 +300,14 @@ void QtHost::QueueSettingsSave() s_settings_save_timer->start(SETTINGS_SAVE_DELAY); } +void QtHost::RunOnUIThread(const std::function& func, bool block /*= false*/) +{ + // main window always exists, so it's fine to attach it to that. + QMetaObject::invokeMethod(g_main_window, "runOnUIThread", + block ? Qt::BlockingQueuedConnection : Qt::QueuedConnection, + Q_ARG(const std::function&, func)); +} + std::optional> Host::ReadResourceFile(const char* filename) { const std::string path(Path::CombineStdString(EmuFolders::Resources, filename)); diff --git a/pcsx2-qt/QtHost.h b/pcsx2-qt/QtHost.h index 02e6393dcba29..a84269f61248b 100644 --- a/pcsx2-qt/QtHost.h +++ b/pcsx2-qt/QtHost.h @@ -42,6 +42,9 @@ namespace QtHost void UpdateFolders(); void UpdateLogging(); + /// Executes a function on the UI thread. + void RunOnUIThread(const std::function& func, bool block = false); + /// Thread-safe settings access. SettingsInterface* GetBaseSettingsInterface(); std::string GetBaseStringSettingValue(const char* section, const char* key, const char* default_value = "");