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
+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)