#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QTimer>
#include <QThread>
{
Q_OBJECT
public:
explicit Worker
(QObject *parent
= 0);
virtual ~Worker();
signals:
void finished();
void updateProgress(int);
public slots:
void start();
void stop();
void doWork();
private:
};
{
Q_OBJECT
public:
static void sleep(unsigned long secs)
{
}
static void msleep(unsigned long msecs)
{
}
};
#endif // WORKER_H
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QTimer>
#include <QThread>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
virtual ~Worker();
signals:
void finished();
void updateProgress(int);
public slots:
void start();
void stop();
void doWork();
private:
QTimer* m_workerTimer;
};
class MThread : public QThread
{
Q_OBJECT
public:
MThread(QObject *parent) : QThread(parent) {}
static void sleep(unsigned long secs)
{
QThread::sleep(secs);
}
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
};
#endif // WORKER_H
To copy to clipboard, switch view to plain text mode
#include "worker.h"
#include <QThread>
#include <QDebug>
volatile bool stopWork;
{
m_workerTimer
= new QTimer(this);
stopWork = false;
}
Worker::~Worker()
{
delete m_workerTimer;
}
void Worker::start()
{
connect(m_workerTimer, SIGNAL(timeout()), this, SLOT(doWork()));
m_workerTimer->start(100);
}
void Worker::stop()
{
m_workerTimer->stop();
emit finished();
stopWork = true;
}
void Worker::doWork()
{
for (int i = 0; i < 1000000; i++)
{
if (stopWork)
{
break;
}
if (i % 10000 == 0)
{
emit updateProgress((i / 10000) + 1);
qDebug() << ((i / 10000) + 1);
static_cast<MThread
*>
(QThread::currentThread())->msleep
(10);
}
}
emit finished();
}
#include "worker.h"
#include <QThread>
#include <QDebug>
volatile bool stopWork;
Worker::Worker(QObject *parent) : QObject(parent)
{
m_workerTimer = new QTimer(this);
stopWork = false;
}
Worker::~Worker()
{
delete m_workerTimer;
}
void Worker::start()
{
connect(m_workerTimer, SIGNAL(timeout()), this, SLOT(doWork()));
m_workerTimer->start(100);
}
void Worker::stop()
{
m_workerTimer->stop();
emit finished();
stopWork = true;
}
void Worker::doWork()
{
for (int i = 0; i < 1000000; i++)
{
if (stopWork)
{
break;
}
if (i % 10000 == 0)
{
emit updateProgress((i / 10000) + 1);
qDebug() << ((i / 10000) + 1);
static_cast<MThread*>(QThread::currentThread())->msleep(10);
}
}
emit finished();
}
To copy to clipboard, switch view to plain text mode
#include "flashwizard.h"
#include "ui_flashwizard.h"
#include "worker.h"
#include <QThread>
#include <QDebug>
#include <QMessageBox>
FlashWizard
::FlashWizard(QWidget *parent
) : QWizard
(parent
), ui
(new Ui
::FlashWizard){
ui->setupUi(this);
ui->pbReading->reset();
ui->pbWriting->reset();
ui->lblReadingStatus->setText("");
ui->lblReadingStatus->setText("");
ui->lblProgStatus->setText("");
connect(ui->introPage, SIGNAL(p1_updateLabelOne(const QString&)), this, SLOT(updateLabel(const QString&)));
connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageCleanUp(int)));
worker = new Worker;
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
}
FlashWizard::~FlashWizard()
{
delete ui;
}
void FlashWizard::updateWritingProgressBar(const int value)
{
if (ui->pbWriting->maximum() == value)
ui->pbWriting->hide();
ui->pbWriting->setValue(value);
}
void FlashWizard
::updateLabel(const QString &value
) {
ui->lblReadingStatus->setText(value);
}
void FlashWizard::pageCleanUp(int id)
{
if (id == Page_Program)
{
ui->lblProgStatus->setText("Updating ****");
workerThread->start();
qDebug() << "Now using thread: " << worker->thread();
}
ui->pbReading->setVisible(false);
ui->lblUpdating->setText("");
}
void FlashWizard::reject()
{
if (workerThread->isRunning())
{
worker->stop();
}
}
#include "flashwizard.h"
#include "ui_flashwizard.h"
#include "worker.h"
#include <QThread>
#include <QDebug>
#include <QMessageBox>
FlashWizard::FlashWizard(QWidget *parent) : QWizard(parent), ui(new Ui::FlashWizard)
{
ui->setupUi(this);
ui->pbReading->reset();
ui->pbWriting->reset();
ui->lblReadingStatus->setText("");
ui->lblReadingStatus->setText("");
ui->lblProgStatus->setText("");
connect(ui->introPage, SIGNAL(p1_updateLabelOne(const QString&)), this, SLOT(updateLabel(const QString&)));
connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageCleanUp(int)));
worker = new Worker;
workerThread = new QThread;
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
}
FlashWizard::~FlashWizard()
{
delete ui;
}
void FlashWizard::updateWritingProgressBar(const int value)
{
if (ui->pbWriting->maximum() == value)
ui->pbWriting->hide();
ui->pbWriting->setValue(value);
}
void FlashWizard::updateLabel(const QString &value)
{
ui->lblReadingStatus->setText(value);
}
void FlashWizard::pageCleanUp(int id)
{
if (id == Page_Program)
{
ui->lblProgStatus->setText("Updating ****");
workerThread->start();
qDebug() << "Now using thread: " << worker->thread();
}
ui->pbReading->setVisible(false);
ui->lblUpdating->setText("");
}
void FlashWizard::reject()
{
if (workerThread->isRunning())
{
worker->stop();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks