1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <QtGui>
#include <QtWidgets> // pour Qt5
#include <QtCore>
#include <QApplication>
class Measure : public QObject
{
Q_OBJECT
int timerid;
public :
Measure():timerid(-1)
{}
protected :
void RunMeasure()
{
newMeasure(QString::number(rand()));
}
void timerEvent(QTimerEvent * ev)
{
if(ev->timerId() == timerid)
RunMeasure();
}
signals :
void newMeasure(QString);
public slots :
void start()
{
if(timerid == -1)
timerid =startTimer(0);
}
void stop()
{
if(timerid != -1)
{
killTimer(timerid);
timerid = -1;
}
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QThread thread;
thread.start();
Measure mm;
mm.moveToThread(&thread);
QWidget w;
{
QVBoxLayout * layout = new QVBoxLayout(&w);
QPushButton * start = new QPushButton("Start");
QPushButton * stop = new QPushButton("Stop");
QLabel * label = new QLabel;
layout->addWidget(start);
layout->addWidget(stop);
layout->addWidget(label);
QObject::connect(start,SIGNAL(clicked()),&mm,SLOT(start()));
QObject::connect(stop,SIGNAL(clicked()),&mm,SLOT(stop()));
QObject::connect(&mm,SIGNAL(newMeasure(QString)),label,SLOT(setText(QString)));
}
w.show();
a.exec();
return 0;
} |