Skip to content

Instantly share code, notes, and snippets.

@Razzo78
Last active January 26, 2018 09:32
Show Gist options
  • Save Razzo78/fd60909de1ca0cedd7e61173918958ca to your computer and use it in GitHub Desktop.
Save Razzo78/fd60909de1ca0cedd7e61173918958ca to your computer and use it in GitHub Desktop.
QT - Execute a function from a task thread in the main thread context. This is useful to update UI, without cross thread errors.
void ThreadToolUI::PostToMainThread(const std::function<void()> & fun)
{
QObject signalSource;
QObject::connect(&signalSource, &QObject::destroyed, qApp, [=](QObject*){
fun();
});
}
Task::run<void>([=]() {
qDebug() << "Current therad is task thread";
ThreadToolUI::PostToMainThread([=]() {
qDebug() << "Current therad is main thread";
});
}).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment