Skip to content

Commit

Permalink
Qt: Add QtHost::RunOnUIThread()
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and refractionpcsx2 committed Apr 6, 2022
1 parent 0efe03e commit 821e15f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pcsx2-qt/MainWindow.cpp
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions pcsx2-qt/MainWindow.h
Expand Up @@ -17,6 +17,7 @@

#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <functional>
#include <optional>

#include "Settings/ControllerSettingsDialog.h"
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 8 additions & 0 deletions pcsx2-qt/QtHost.cpp
Expand Up @@ -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));
Expand Down
3 changes: 3 additions & 0 deletions pcsx2-qt/QtHost.h
Expand Up @@ -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 = "");
Expand Down

0 comments on commit 821e15f

Please sign in to comment.