Move timer callback to separate thread

This commit is contained in:
BlackMark 2020-07-08 22:37:02 +02:00
parent d1d2d4ae0a
commit 2253a25011
2 changed files with 14 additions and 3 deletions

View File

@ -64,9 +64,13 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
qDebug(ltr("Tray icon initialized"));
}
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &MainWindow::updateState);
m_timer->start(1000);
m_timerThread = new QThread(this);
m_timer = new QTimer(nullptr);
m_timer->setInterval(1000);
m_timer->moveToThread(m_timerThread);
m_timerThread->connect(m_timer, &QTimer::timeout, this, &MainWindow::updateState, Qt::DirectConnection);
m_timer->connect(m_timerThread, SIGNAL(started()), SLOT(start()));
m_timerThread->start();
qDebug(ltr("Tray icon update timer started"));
@ -77,6 +81,11 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
MainWindow::~MainWindow()
{
qDebug(ltr("Destroying main window"));
m_timerThread->quit();
if(!m_timerThread->wait()) {
qCritical(ltr("Timer thread did not terminate cleanly"));
}
}
void MainWindow::loadIcons()

View File

@ -9,6 +9,7 @@
#include <QMainWindow>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QThread>
#include <QTimer>
#include "monitor.hpp"
@ -39,6 +40,7 @@ class MainWindow : public QMainWindow {
QMenu* m_trayIconMenu = nullptr;
QSystemTrayIcon* m_trayIcon = nullptr;
QThread* m_timerThread = nullptr;
QTimer* m_timer = nullptr;
float m_brightness = 0.5f;