您的位置:首页 > 编程语言 > Qt开发

qt下的时钟程序(简单美丽,继承自QWidget的Clock,用timer调用update刷新,然后使用paintEvent作画就行了,超详细中文注释)good

2017-06-14 18:40 501 查看
最近抽空又看了下qt,发现用它来实现一些东西真的很容易
比如下面这个例子,绘制了个圆形的时钟,
但代码却清晰易懂[例子源自奇趣科技提供的例子]
因为清晰,所以就只写注释了,吼吼
其实也就这么几行代码
头文件

//clock.h

#ifndef CLOCK_H
#define CLOCK_H

#include <QWidget>

class Clock : public QWidget
{
//对于具有signal,slot机制的类需要声明
Q_OBJECT

public:
Clock(QWidget *parent = 0);

protected:
//重绘用的事件处理函式
void paintEvent(QPaintEvent *event);
};

#endif // CLOCK_H

cpp文件

1 #include "clock.h"
2
3
4 #include <QtGui>
5
6 #include "clock.h"
7
8 Clock::Clock(QWidget *parent): QWidget(parent)
9 {
10 //声明一个定时器
11 QTimer *timer = new QTimer(this);
12 //连接信号与槽
13 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
14 timer->start(1000);
15 //设置窗体名称与大小
16 setWindowTitle(tr("Clock"));
17 resize(200, 200);
18
19 }
20
21
22 void Clock::paintEvent(QPaintEvent *)
23
24 {
25 //下面三个数组用来定义表针的三个顶点,以便后面的填充
26 static const QPoint hourHand[3] = {
27 QPoint(3, 8),
28 QPoint(-3, 8),
29 QPoint(0, -40)
30 };
31 static const QPoint minuteHand[3] = {
32 QPoint(3, 8),
33 QPoint(-3, 8),
34 QPoint(0, -70)
35 };
36 static const QPoint secondHand[3] = {
37 QPoint(3, 8),
38 QPoint(-3, 8),
39 QPoint(0, -90)
40 };
41
42 //填充表针的颜色
43 QColor hourColor(127, 0, 127);
44 QColor minuteColor(0, 127, 127, 191);
45 QColor secondColor(127, 127,0,120);
46 //绘制的范围
47 int side = qMin(width(), height());
48 //获取当前的时间
49 QTime time = QTime::currentTime();
50 //声明用来绘图用的“画家”
51 QPainter painter(this);
52
53 painter.setRenderHint(QPainter::Antialiasing);
54 //重新定位坐标起始点点
55 painter.translate(width() / 2, height() / 2);
56 //设定花布的边界
57 painter.scale(side / 200.0, side / 200.0);
58 //填充时针,不需要边线所以NoPen
59 painter.setPen(Qt::NoPen);
60 //画刷颜色设定
61 painter.setBrush(hourColor);
62 //保存“画家”的状态
63 painter.save();
64 //将“画家”(的”视角“)根据时间参数转移
65 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
66 //填充时针的区域
67 painter.drawConvexPolygon(hourHand, 3);
68 //恢复填充前“画家”的状态
69 painter.restore();
70
71 //下面画表示小时的刻度,此时要用到画笔(因为要划线)
72 painter.setPen(hourColor);
73 //十二个刻度,循环下就好了
74 for (int i = 0; i < 12; ++i) {
75 //没次都是这样,先画跳线,再转个角
76 painter.drawLine(88, 0, 96, 0);
77 painter.rotate(30.0);
78 }
79
80 //后面的跟前面的类似,分别绘制了分针和秒针,及相应的刻度,我就不废话了
81
82 painter.setPen(Qt::NoPen);
83
84 painter.setBrush(minuteColor);
85
86
87 painter.save();
88 painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
89 painter.drawConvexPolygon(minuteHand, 3);
90 painter.restore();
91
92 painter.setPen(minuteColor);
93
94 for (int j = 0; j < 60; ++j) {
95 if ((j % 5) != 0)
96 painter.drawLine(92, 0, 96, 0);
97 painter.rotate(6.0);
98 }
99
100
101 painter.setPen(Qt::NoPen);
102
103 painter.setBrush(secondColor);
104
105 painter.save();
106 painter.rotate(6.0*time.second());
107 painter.drawConvexPolygon(secondHand,3);
108 painter.restore();
109
110 }
111
112

main文件

#include <QApplication>

#include "clock.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//声明下,再show出来就可以了
Clock clock;
clock.show();
return app.exec();
}

pro文件

HEADERS = clock.h
SOURCES = clock.cpp \
main.cpp

下面是运行时的截图,开发环境为qtcreator

在奇趣提供的例子中还将其做成了控件,有时间在写点关于那个例子的东西。


http://www.cnblogs.com/pingf/archive/2009/08/06/1540374.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐