本系列所有文章可以在這里查看[http://blog.csdn.net/cloud_castle/article/category/2123873](http://blog.csdn.net/cloud_castle/article/category/2123873)
一直想找個成套的例子給大家看看,一來可以由淺入深,習得復雜中的緣由,簡單中的精妙。二來呢一直覺得比較性的學習是最有效率的~
無奈Qt中這種例子并不多,好在今天又碰到一個。這三個Clock Example基本同出一派,但也都有些值得玩味的地方,先從第一個例子開始吧~
介紹是這樣寫的:
The Analog Clock example shows how to draw the contents of a custom widget.
This example also demonstrates how the transformation and scaling features of QPainter can be used to make drawing custom widgets easier.
也就是說,從這個例子里,我們應該能了解到一些scaling和transformation在繪圖中的應用~
先看analogclock.h:
~~~
#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H
#include <QWidget>
//! [0]
class AnalogClock : public QWidget
{
Q_OBJECT
public:
AnalogClock(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
};
//! [0]
#endif
~~~
沒什么特別好說的,就是一點,為什么事件通常被protected繼承?
第一,子類通常應當可以響應父類可以響應的事件,因此不應該使用private。
第二,事件函數由特定的事件觸發,而不應當被實例對象所調用。w->mouseMoveInEvent(QMouseEvent *e);這種用法一定很奇怪吧。因此不應該使用public。
analogclock.cpp:
~~~
#include <QtWidgets>
#include "analogclock.h"
//! [0] //! [1] // 話說我一直不懂! [0]這些表明什么,有了解的網友希望告訴我一下。。。
AnalogClock::AnalogClock(QWidget *parent)
//! [0] //! [2]
: QWidget(parent)
//! [2] //! [3]
{
//! [3] //! [4]
QTimer *timer = new QTimer(this);
//! [4] //! [5]
connect(timer, SIGNAL(timeout()), this, SLOT(update())); // 1秒信號槽
//! [5] //! [6]
timer->start(1000);
//! [6]
setWindowTitle(tr("Analog Clock"));
resize(200, 200);
//! [7]
}
//! [1] //! [7]
//! [8] //! [9]
void AnalogClock::paintEvent(QPaintEvent *)
//! [8] //! [10]
{
static const QPoint hourHand[3] = { // 時針的坐標數組。一個尖尖向下的三角形的三個點的坐標
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = { // const能理解,static能理解嗎?每秒一次的update(),每次創建這個數組麻煩不?麻煩,那就使用靜態變量就好了
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -70)
};
QColor hourColor(127, 0, 127); // 時針顏色的RGB值
QColor minuteColor(0, 127, 127, 191); // 分針擁有75%的透明度
int side = qMin(width(), height()); // 這里取的是窗口長寬的較小值
QTime time = QTime::currentTime(); // 取當前時間
//! [10]
//! [11]
QPainter painter(this);
//! [11] //! [12]
painter.setRenderHint(QPainter::Antialiasing); // 渲染屬性
//! [12] //! [13]
painter.translate(width() / 2, height() / 2); // 坐標變換,中點坐標變為為(0, 0)
//! [13] //! [14]
painter.scale(side / 200.0, side / 200.0); // 縮放為窗口長寬的較小值的 1/200。也就是1。那有什么意義呢?為了用戶縮放窗口的時候也能鋪滿整個窗口嘛
//! [9] //! [14]
//! [15]
painter.setPen(Qt::NoPen); // 不繪制圖像邊界
//! [15] //! [16]
painter.setBrush(hourColor);
//! [16]
//! [17] //! [18]
painter.save(); // 保存設置
//! [17] //! [19]
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); // 以每小時30度旋轉坐標系
painter.drawConvexPolygon(hourHand, 3); // 畫時針
painter.restore(); // 重載剛才保存的設置,也就是把旋轉的坐標系重置回來了
//! [18] //! [19]
//! [20]
painter.setPen(hourColor);
//! [20] //! [21]
for (int i = 0; i < 12; ++i) { // 這個for循環就是畫時針線咯
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0); // 坐標系旋轉了30 * 12 度,就不用重置了
}
//! [21]
//! [22]
painter.setPen(Qt::NoPen);
//! [22] //! [23]
painter.setBrush(minuteColor);
//! [24]
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3); // 畫分針
painter.restore();
//! [23] //! [24]
//! [25]
painter.setPen(minuteColor);
//! [25] //! [26]
//! [27]
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0) // 如果 j 是5的整數倍就不畫了
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
//! [27]
}
//! [26]
~~~
ok,這個例子就到這里,有意思的還在后面~
- 前言
- 1——Fortune Server/Client
- 2——Multicast Sender/Receiverz
- 3——Broadcast Sender/Receiver
- 4——Blocking Fortune Client
- 5——Threaded Fortune Server
- 5(總結)——Fortune例程的各個實現區別
- 6——Loopback Example
- 7——Analog Clock Example
- 8——Shaped Clock Example
- 9——Analog Clock Window Example
- 10——Qt Quick Particles Examples - Emitters
- 11——Qt Quick Particles Examples - Affectors
- 12——Qt Quick Particles Examples - CustomParticles
- 13——Qt Quick Particles Examples - Image Particles
- 14——Qt Quick Particles Examples - System
- 15——Chapter 1: Creating a New Type
- 16——Chapter 2: Connecting to C++ Methods and Signals
- 17——Chapter 3: Adding Property Bindings
- 18——Chapter 4: Using Custom Property Types
- 19——Chapter 5: Using List Property Types
- 20——Chapter 6: Writing an Extension Plugin
- 21——Extending QML - Adding Types Example
- 22——Extending QML - Object and List Property Types Example
- 23——Extending QML - Inheritance and Coercion Example
- 24——Extending QML - Default Property Example
- 25——Extending QML - Methods Example
- 26——Extending QML - Grouped Properties Example
- 27——Extending QML - Attached Properties Example
- 28——Extending QML - Signal Support Example
- 29——Extending QML - Property Value Source Example
- 30——Extending QML - Binding Example
- 31——StocQt
- 32——Qt Quick Examples - Threading
- 33——Qt Quick Examples - Window and Screen
- 34——Concentric Circles Example
- 35——Music Player
- 36——Wiggly Example
- 37——Vector Deformation