use QMetaObject::invokeMethod to post to thread (#6203)

This commit is contained in:
pajlada
2025-05-11 13:52:00 +02:00
committed by GitHub
parent 4c266da24d
commit 5c64ec3c67
2 changed files with 3 additions and 23 deletions
+1
View File
@@ -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)
+2 -23
View File
@@ -3,34 +3,13 @@
#include "debug/AssertInGuiThread.hpp"
#include <QCoreApplication>
#include <QMetaObject>
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<decltype(f)>;
Fun fun;
Event(decltype(fun) f)
: QEvent(QEvent::None)
, fun(std::forward<decltype(fun)>(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<decltype(f)>(f)));
QMetaObject::invokeMethod(obj, std::forward<decltype(f)>(f));
}
static void runInGuiThread(auto &&fun)