Skip to content

Commit

Permalink
Don't use QTimer in dispatchToMain (#1237)
Browse files Browse the repository at this point in the history
IB-7904

Signed-off-by: Raul Metsma <raul@metsma.ee>
  • Loading branch information
metsma authored Dec 1, 2023
1 parent 13f107a commit e321e6f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
4 changes: 1 addition & 3 deletions client/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ Application::Application( int &argc, char **argv )
Q_EMIT qApp->TSLLoadingFinished();
qApp->d->ready = true;
if(ex) {
dispatchToMain([ex] {
showWarning(tr("Failed to initalize."), *ex);
});
dispatchToMain(showWarning, tr("Failed to initalize."), *ex);
}
}
);
Expand Down
15 changes: 4 additions & 11 deletions client/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,18 @@ namespace {
template <typename F, class... Args>
inline auto dispatchToMain(F&& function, Args&& ...args) {
QEventLoop l;
QTimer *timer = new QTimer();
timer->moveToThread(qApp->thread());
timer->setSingleShot(true);
if constexpr (std::is_void_v<std::invoke_result_t<F,Args...>>) {
QObject::connect(timer, &QTimer::timeout, timer, [&, function = std::forward<F>(function)] {
QMetaObject::invokeMethod(qApp, [&, function = std::forward<F>(function)] {
std::invoke(function, args...);
l.exit();
timer->deleteLater();
});
QMetaObject::invokeMethod(timer, [timer] { timer->start(0); }, Qt::QueuedConnection);
}, Qt::QueuedConnection);
l.exec();
} else {
std::invoke_result_t<F,Args...> result{};
QObject::connect(timer, &QTimer::timeout, timer, [&, function = std::forward<F>(function)] {
QMetaObject::invokeMethod(qApp, [&, function = std::forward<F>(function)] {
result = std::invoke(function, args...);
l.exit();
timer->deleteLater();
});
QMetaObject::invokeMethod(timer, [timer] { timer->start(0); }, Qt::QueuedConnection);
}, Qt::QueuedConnection);
l.exec();
return result;
}
Expand Down

0 comments on commit e321e6f

Please sign in to comment.