07_QT时间编程之QT时钟
QT时间编程之QT时钟
这节课我们做一个计时器
qtime:qt的时间类
qtimer:qt的定时类
代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QTimer timer;
QTime time;
private slots:
void on_startBt_clicked();
void timeout_slot();
void on_stopBt_clicked();
void on_resetBt_clicked();
void on_bitBt_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
static int i;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout_slot()));
time.setHMS(0, 0, 0, 0);
ui->showTime->setText("00:00:00.000");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_startBt_clicked()
{
timer.start(1); //ms
}
void Widget::timeout_slot()
{
time = time.addMSecs(1);
ui->showTime->setText(time.toString("hh:mm:ss.zzz"));
}
void Widget::on_stopBt_clicked()
{
timer.stop();
}
void Widget::on_resetBt_clicked()
{
timer.stop();
time.setHMS(0, 0, 0, 0);
ui->showTime->setText("00:00:00.000");
ui->bitTime->clear();
i=0;
}
void Widget::on_bitBt_clicked()
{
ui->bitTime->append(QString::number(++i)+":\n"+time.toString("hh:mm:ss.zzz"));
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>649</width>
<height>393</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="showTime">
<property name="font">
<font>
<pointsize>29</pointsize>
</font>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="startBt">
<property name="text">
<string>开始</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="stopBt">
<property name="text">
<string>停止</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="resetBt">
<property name="text">
<string>重置</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="bitBt">
<property name="text">
<string>打点</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextBrowser" name="bitTime"/>
</item>
</layout>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>