1. 程式人生 > >qt下的時鐘程序(簡單美麗,繼承自QWidget的Clock,用timer調用update刷新,然後使用paintEvent作畫就行了,超詳細中文註釋)good

qt下的時鐘程序(簡單美麗,繼承自QWidget的Clock,用timer調用update刷新,然後使用paintEvent作畫就行了,超詳細中文註釋)good

循環 連接 定位 根據 定時器 img cal widget tran

最近抽空又看了下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

qt下的時鐘程序(簡單美麗,繼承自QWidget的Clock,用timer調用update刷新,然後使用paintEvent作畫就行了,超詳細中文註釋)good