From 5c64ec3c6713283daaeef9590d5ec63b775fed29 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 11 May 2025 13:52:00 +0200 Subject: [PATCH] use `QMetaObject::invokeMethod` to post to thread (#6203) --- CHANGELOG.md | 1 + src/util/PostToThread.hpp | 25 ++----------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11134546..d6b11eee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ - Dev: Removed authenticated PubSub implementation. (#6158) - Dev: Save settings in `aboutToQuit`. (#6159) - Dev: Bumped deprecation cutoff to Qt 6.4.3. (#6169) +- Dev: Use `QMetaObject::invokeMethod` to run code on a specific thread. (#6203) - Dev: Added a `run-and-kill.sh` script to help debug crash-on-exit bugs. (#6188) - Dev: Refactored event API initialization away from Application and into TwitchIrcServer. (#6198) - Dev: Updated GoogleTest to v1.17.0. (#6180) diff --git a/src/util/PostToThread.hpp b/src/util/PostToThread.hpp index 8021defa..81af2a7d 100644 --- a/src/util/PostToThread.hpp +++ b/src/util/PostToThread.hpp @@ -3,34 +3,13 @@ #include "debug/AssertInGuiThread.hpp" #include +#include namespace chatterino { -// Taken from -// https://stackoverflow.com/questions/21646467/how-to-execute-a-functor-or-a-lambda-in-a-given-thread-in-qt-gcd-style -// Qt 5/4 - preferred, has least allocations static void postToThread(auto &&f, QObject *obj = QCoreApplication::instance()) { - struct Event : public QEvent { - using Fun = typename std::decay_t; - Fun fun; - - Event(decltype(fun) f) - : QEvent(QEvent::None) - , fun(std::forward(f)) - { - } - Event(const Event &) = delete; - Event(Event &&) = delete; - Event &operator=(const Event &) = delete; - Event &operator=(Event &&) = delete; - - ~Event() override - { - fun(); - } - }; - QCoreApplication::postEvent(obj, new Event(std::forward(f))); + QMetaObject::invokeMethod(obj, std::forward(f)); } static void runInGuiThread(auto &&fun)