1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-17 13:29:02 +00:00

Cleans up a little and ensures a safe exit of the timer thread.

This commit is contained in:
Thomas Harte 2020-05-31 23:58:19 -04:00
parent d64b4fbc26
commit a30723c3d4
2 changed files with 29 additions and 16 deletions

View File

@ -5,8 +5,6 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "timer.h" #include "timer.h"
Timer *t;
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
@ -14,16 +12,23 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this); ui->setupUi(this);
createActions(); createActions();
// Start the emulation timer. TODO: not now. // Set up the emulation timer. Bluffer's guide: the QTimer will post an
timer = std::make_unique<QTimer>(this); // event to an event loop. QThread is a thread with an event loop.
QThread *thread = new QThread(this); // My class, Timer, will be wired up to receive the QTimer's events.
timer->setInterval(1); qTimer = std::make_unique<QTimer>(this);
t = new Timer; qTimer->setInterval(1);
t->moveToThread(thread);
connect(timer.get(), SIGNAL(timeout()), t, SLOT(tick())); timerThread = std::make_unique<QThread>(this);
connect(thread, SIGNAL(finished()), t, SLOT(deleteLater()));
thread->start(); timer = std::make_unique<Timer>();
timer->start(); timer->moveToThread(timerThread.get());
connect(qTimer.get(), SIGNAL(timeout()), timer.get(), SLOT(tick()));
// Start the thread and timer.
// TODO: not until there's actually something to display.
timerThread->start();
qTimer->start();
} }
void MainWindow::createActions() { void MainWindow::createActions() {
@ -47,11 +52,15 @@ void MainWindow::createActions() {
void MainWindow::open() { void MainWindow::open() {
QString fileName = QFileDialog::getOpenFileName(this); QString fileName = QFileDialog::getOpenFileName(this);
// if(!fileName.isEmpty()) if(!fileName.isEmpty()) {
// loadFile(fileName); qDebug() << "Should open" << fileName;
}
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
// TODO: stop thread somehow? // Stop the timer by asking its QThread to exit and
// waiting for it to do so.
timerThread->exit();
while(timerThread->isRunning());
} }

View File

@ -3,6 +3,7 @@
#include <QMainWindow> #include <QMainWindow>
#include <memory> #include <memory>
#include "timer.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -19,7 +20,10 @@ class MainWindow : public QMainWindow {
private: private:
std::unique_ptr<Ui::MainWindow> ui; std::unique_ptr<Ui::MainWindow> ui;
std::unique_ptr<QTimer> timer; std::unique_ptr<QTimer> qTimer;
std::unique_ptr<QThread> timerThread;
std::unique_ptr<Timer> timer;
private slots: private slots:
void open(); void open();