Qt: Add QtHost::RunOnUIThread()

This commit is contained in:
Connor McLaughlin 2022-04-06 20:49:00 +10:00 committed by refractionpcsx2
parent 0efe03e726
commit 821e15f1ee
4 changed files with 18 additions and 0 deletions

View File

@ -696,6 +696,11 @@ void MainWindow::reportError(const QString& title, const QString& message)
QMessageBox::critical(this, title, message);
}
void MainWindow::runOnUIThread(const std::function<void()>& func)
{
func();
}
bool MainWindow::requestShutdown(bool allow_confirm /* = true */, bool allow_save_to_state /* = true */, bool block_until_done /* = false */)
{
if (!VMManager::HasValidVM())

View File

@ -17,6 +17,7 @@
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <functional>
#include <optional>
#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<void()>& func);
bool requestShutdown(bool allow_confirm = true, bool allow_save_to_state = true, bool block_until_done = false);
void requestExit();

View File

@ -300,6 +300,14 @@ void QtHost::QueueSettingsSave()
s_settings_save_timer->start(SETTINGS_SAVE_DELAY);
}
void QtHost::RunOnUIThread(const std::function<void()>& 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<void()>&, func));
}
std::optional<std::vector<u8>> Host::ReadResourceFile(const char* filename)
{
const std::string path(Path::CombineStdString(EmuFolders::Resources, filename));

View File

@ -42,6 +42,9 @@ namespace QtHost
void UpdateFolders();
void UpdateLogging();
/// Executes a function on the UI thread.
void RunOnUIThread(const std::function<void()>& func, bool block = false);
/// Thread-safe settings access.
SettingsInterface* GetBaseSettingsInterface();
std::string GetBaseStringSettingValue(const char* section, const char* key, const char* default_value = "");