From: Steinar H. Gunderson Date: Sun, 8 Nov 2015 16:01:51 +0000 (+0100) Subject: Do the UI changes always in the main thread. X-Git-Tag: 1.0.0~152 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=7902286a0863f0ae655b14b5c830d81920c55e20;p=nageru Do the UI changes always in the main thread. --- diff --git a/mainwindow.cpp b/mainwindow.cpp index 01baef5..d4e4d62 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -18,6 +18,7 @@ #include "glwidget.h" #include "lrameter.h" #include "mixer.h" +#include "post_to_main_thread.h" #include "ui_display.h" #include "ui_mainwindow.h" #include "vumeter.h" @@ -91,21 +92,23 @@ void MainWindow::mixer_created(Mixer *mixer) } mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float auto_gain_staging_db){ - ui->vu_meter->set_level(level_lufs); - ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs); - - char buf[256]; - snprintf(buf, sizeof(buf), "%.1f", peak_db); - ui->peak_display->setText(buf); - if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit. - ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }"); - } else { - ui->peak_display->setStyleSheet(""); - } - - ui->gainstaging_knob->setValue(lrintf(auto_gain_staging_db * 10.0f)); - snprintf(buf, sizeof(buf), "%+.1f dB", auto_gain_staging_db); - ui->gainstaging_db_display->setText(buf); + post_to_main_thread([=]() { + ui->vu_meter->set_level(level_lufs); + ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs); + + char buf[256]; + snprintf(buf, sizeof(buf), "%.1f", peak_db); + ui->peak_display->setText(buf); + if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit. + ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }"); + } else { + ui->peak_display->setStyleSheet(""); + } + + ui->gainstaging_knob->setValue(lrintf(auto_gain_staging_db * 10.0f)); + snprintf(buf, sizeof(buf), "%+.1f dB", auto_gain_staging_db); + ui->gainstaging_db_display->setText(buf); + }); }); } diff --git a/post_to_main_thread.h b/post_to_main_thread.h new file mode 100644 index 0000000..0462c7b --- /dev/null +++ b/post_to_main_thread.h @@ -0,0 +1,16 @@ +#ifndef _POST_TO_MAIN_THREAD_H +#define _POST_TO_MAIN_THREAD_H 1 + +#include +#include +#include + +// http://stackoverflow.com/questions/21646467/how-to-execute-a-functor-in-a-given-thread-in-qt-gcd-style +template +static inline void post_to_main_thread(F &&fun) +{ + QObject signalSource; + QObject::connect(&signalSource, &QObject::destroyed, qApp, std::move(fun)); +} + +#endif // !defined(_POST_TO_MAIN_THREAD_H)