/////////////// The GUI class///////////////////
class GUI
{
WorkerThread wt;
Data data;
signals:
dataOut(Data);
}
GUI::GUI()
{
connect(this, SIGNAL(dataOut(Data)), &wt, SLOT(dataIn(Data)));
wt.start();
}
GUI::somewhere()
{
emit dataOut(data);
qDebug
() <<
QThread::currentThreadId() <<
"data emitted";
}
/////////////// The Worker Thread ///////////////////
{
Data data;
void tick();
public slots:
dataIn(Data);
}
WorkerThread::WorkerThread()
{
moveToThread(this);
}
// This is the callback function of the windows media timer.
void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
WorkerThread* wt = (WorkerThread*)dwUser;
wt->tick();
}
// When the thread is started, I create the MM timer and start the event loop.
void RobotControl::run()
{
// Set resolution to the minimum supported by the system.
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
timeBeginPeriod(timerRes);
// Create the callback timer.
timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
// Start the event loop.
exec();
}
WorkerThread::tick()
{
use(data);
qDebug
() <<
QThread::currentThreadId() <<
"worker thread was ticked";
}
WorkerThread::dataIn(Data d)
{
data = d;
qDebug
() <<
QThread::currentThreadId() <<
"data received";
}
/////////////// The GUI class///////////////////
class GUI
{
WorkerThread wt;
Data data;
signals:
dataOut(Data);
}
GUI::GUI()
{
connect(this, SIGNAL(dataOut(Data)), &wt, SLOT(dataIn(Data)));
wt.start();
}
GUI::somewhere()
{
emit dataOut(data);
qDebug() << QThread::currentThreadId() << "data emitted";
}
/////////////// The Worker Thread ///////////////////
class WorkerThread : QThread
{
Data data;
void tick();
public slots:
dataIn(Data);
}
WorkerThread::WorkerThread()
{
moveToThread(this);
}
// This is the callback function of the windows media timer.
void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
WorkerThread* wt = (WorkerThread*)dwUser;
wt->tick();
}
// When the thread is started, I create the MM timer and start the event loop.
void RobotControl::run()
{
// Set resolution to the minimum supported by the system.
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
timeBeginPeriod(timerRes);
// Create the callback timer.
timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
// Start the event loop.
exec();
}
WorkerThread::tick()
{
use(data);
qDebug() << QThread::currentThreadId() << "worker thread was ticked";
}
WorkerThread::dataIn(Data d)
{
data = d;
qDebug() << QThread::currentThreadId() << "data received";
}
To copy to clipboard, switch view to plain text mode
Bookmarks