main.h
#ifndef MAIN_H
#define MAIN_H
#include <QtGui>
#ifdef Q_OS_WIN
#include "windows.h"
#endif
class SleeperThread
: public QThread{ Q_OBJECT
public:
SleeperThread
(int interval,
QObject* parent
= 0) : QThread(parent
), m_interval
(interval
) {}protected:
virtual void run()
{
QElapsedTimer timer;
timer.start();
for (int i=0;i<100;++i)
{
msleep(m_interval);
}
QMetaObject::invokeMethod(parent
(),
"showResult",Q_ARG
(int, timer.
elapsed()));
}
private:
int m_interval;
};
{ Q_OBJECT
public:
MainWindow()
{
connect(start_pb,SIGNAL(clicked()),this,SLOT(testSleep()));
connect(beginPeriod_pb,SIGNAL(clicked()),this,SLOT(beginPeriod()));
connect(endPeriod_pb,SIGNAL(clicked()),this,SLOT(endPeriod()));
hl->addWidget(beginPeriod_pb);
hl->addWidget(start_pb);
hl->addWidget(endPeriod_pb);
output_te = new QPlainTextEdit();
output_te->setReadOnly(true);
vl->addLayout(hl);
vl->addWidget(output_te);
w->setLayout(vl);
setCentralWidget(w);
}
public slots:
void testSleep()
{
SleeperThread* st = new SleeperThread(1,this);
connect(st,SIGNAL(result(int)),this,SLOT(showResult(int)));
connect(st,SIGNAL(finished()),st,SLOT(deleteLater()));
st->start();
}
void beginPeriod()
{
output_te->appendPlainText("BeginPeriod!");
#ifdef Q_OS_WIN
if (timeBeginPeriod(1) == TIMERR_NOCANDO)
qWarning() << "Could not start the time period!";
#endif
}
void endPeriod()
{
output_te->appendPlainText("EndPeriod!");
#ifdef Q_OS_WIN
timeEndPeriod(1);
#endif
}
void showResult(int elapsed)
{
output_te
->appendPlainText
(QString::number(elapsed
));
}
private:
QPlainTextEdit* output_te;
};
#endif // MAIN_H
main.cpp:
#include <QApplication>
#include <QtGui>
#include "main.h"
int main(int argc, char** argv)
{
MainWindow mw;
mw.show();
return app.exec();
}
main.h
#ifndef MAIN_H
#define MAIN_H
#include <QtGui>
#ifdef Q_OS_WIN
#include "windows.h"
#endif
class SleeperThread : public QThread
{ Q_OBJECT
public:
SleeperThread(int interval,QObject* parent = 0) : QThread(parent), m_interval(interval) {}
protected:
virtual void run()
{
QElapsedTimer timer;
timer.start();
for (int i=0;i<100;++i)
{
msleep(m_interval);
}
QMetaObject::invokeMethod(parent(),"showResult",Q_ARG(int, timer.elapsed()));
}
private:
int m_interval;
};
class MainWindow : public QMainWindow
{ Q_OBJECT
public:
MainWindow()
{
QPushButton* start_pb = new QPushButton("Test Sleep");
connect(start_pb,SIGNAL(clicked()),this,SLOT(testSleep()));
QPushButton* beginPeriod_pb = new QPushButton("Begin Period");
connect(beginPeriod_pb,SIGNAL(clicked()),this,SLOT(beginPeriod()));
QPushButton* endPeriod_pb = new QPushButton("End Period");
connect(endPeriod_pb,SIGNAL(clicked()),this,SLOT(endPeriod()));
QHBoxLayout* hl = new QHBoxLayout();
hl->addWidget(beginPeriod_pb);
hl->addWidget(start_pb);
hl->addWidget(endPeriod_pb);
output_te = new QPlainTextEdit();
output_te->setReadOnly(true);
QVBoxLayout* vl = new QVBoxLayout();
vl->addLayout(hl);
vl->addWidget(output_te);
QWidget* w = new QWidget();
w->setLayout(vl);
setCentralWidget(w);
}
public slots:
void testSleep()
{
SleeperThread* st = new SleeperThread(1,this);
connect(st,SIGNAL(result(int)),this,SLOT(showResult(int)));
connect(st,SIGNAL(finished()),st,SLOT(deleteLater()));
st->start();
}
void beginPeriod()
{
output_te->appendPlainText("BeginPeriod!");
#ifdef Q_OS_WIN
if (timeBeginPeriod(1) == TIMERR_NOCANDO)
qWarning() << "Could not start the time period!";
#endif
}
void endPeriod()
{
output_te->appendPlainText("EndPeriod!");
#ifdef Q_OS_WIN
timeEndPeriod(1);
#endif
}
void showResult(int elapsed)
{
output_te->appendPlainText(QString::number(elapsed));
}
private:
QPlainTextEdit* output_te;
};
#endif // MAIN_H
main.cpp:
#include <QApplication>
#include <QtGui>
#include "main.h"
int main(int argc, char** argv)
{
QApplication app(argc,argv) ;
MainWindow mw;
mw.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Johannes
Bookmarks