{
Q_OBJECT
public:
explicit StopWatch
(QWidget * parent
= 0) , mRunning(false)
, mStartTime()
, mLabel
(new QLabel("0:0:0:0")) {
hBoxLayout->addWidget(startButton);
hBoxLayout->addWidget(stopButton);
hBoxLayout->addWidget(mLabel);
connect(startButton, SIGNAL(clicked()), SLOT(start()));
connect(stopButton, SIGNAL(clicked()), SLOT(stop()));
startTimer(0);
}
public slots:
void start(void)
{
mRunning = true;
}
void stop(void)
{
mRunning = false;
}
protected:
{
if(mRunning)
{
qint64 ms
= mStartTime.
msecsTo(QDateTime::currentDateTime());
int h = ms / 1000 / 60 / 60;
int m = (ms / 1000 / 60) - (h * 60);
int s = (ms / 1000) - (m * 60);
ms = ms - (s * 1000);
mLabel->setText(diff);
}
}
private:
bool mRunning;
};
class StopWatch : public QWidget
{
Q_OBJECT
public:
explicit StopWatch(QWidget * parent = 0)
: QWidget(parent)
, mRunning(false)
, mStartTime()
, mLabel(new QLabel("0:0:0:0"))
{
QHBoxLayout * hBoxLayout = new QHBoxLayout(this);
QPushButton * startButton = new QPushButton("Start");
QPushButton * stopButton = new QPushButton("Stop");
hBoxLayout->addWidget(startButton);
hBoxLayout->addWidget(stopButton);
hBoxLayout->addWidget(mLabel);
connect(startButton, SIGNAL(clicked()), SLOT(start()));
connect(stopButton, SIGNAL(clicked()), SLOT(stop()));
startTimer(0);
}
public slots:
void start(void)
{
mStartTime = QDateTime::currentDateTime();
mRunning = true;
}
void stop(void)
{
mRunning = false;
}
protected:
void timerEvent(QTimerEvent *)
{
if(mRunning)
{
qint64 ms = mStartTime.msecsTo(QDateTime::currentDateTime());
int h = ms / 1000 / 60 / 60;
int m = (ms / 1000 / 60) - (h * 60);
int s = (ms / 1000) - (m * 60);
ms = ms - (s * 1000);
QString diff = QString("%1:%2:%3:%4").arg(h).arg(m).arg(s).arg(ms);
mLabel->setText(diff);
}
}
private:
bool mRunning;
QDateTime mStartTime;
QLabel * mLabel;
};
To copy to clipboard, switch view to plain text mode
Bookmarks