Compare commits

...

2 Commits

Author SHA1 Message Date
eb18ab9f1e Change window close event to hide window 2020-07-08 22:42:24 +02:00
2253a25011 Move timer callback to separate thread 2020-07-08 22:37:02 +02:00
2 changed files with 24 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include <cmath>
#include <QCloseEvent>
#include <QGroupBox>
#include <QSlider>
#include <QtDebug>
@ -52,7 +53,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
m_visibility = !m_visibility;
});
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(tr("Quit"), this, &QWidget::close);
m_trayIconMenu->addAction(tr("Quit"), this, &QCoreApplication::quit);
qDebug(ltr("Tray icon context menu initialized"));
@ -64,9 +65,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 +82,17 @@ 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::closeEvent(QCloseEvent* event)
{
m_showHideAction->activate(QAction::Trigger);
event->ignore();
}
void MainWindow::loadIcons()

View File

@ -9,6 +9,7 @@
#include <QMainWindow>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QThread>
#include <QTimer>
#include "monitor.hpp"
@ -24,6 +25,8 @@ class MainWindow : public QMainWindow {
~MainWindow();
private:
void closeEvent(QCloseEvent* event) override;
void loadIcons();
void updateState();
@ -39,6 +42,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;