原文: http://zetcode.com/gui/qt5/painting/

在 Qt5 C++ 编程教程的这一部分中,我们将进行绘图。

当我们在 Qt5 中进行绘图时,QPainter类非常有用。 对paintEvent()方法的反应是使用QPainter类完成的。

直线

在第一个示例中,我们将在窗口的客户区域上绘制一些线。

lines.h

  1. #pragma once
  2. #include <QWidget>
  3. class Lines : public QWidget {
  4. public:
  5. Lines(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *event);
  8. void drawLines(QPainter *qp);
  9. };

这是头文件。

lines.cpp

  1. #include <QPainter>
  2. #include "lines.h"
  3. Lines::Lines(QWidget *parent)
  4. : QWidget(parent)
  5. { }
  6. void Lines::paintEvent(QPaintEvent *e) {
  7. Q_UNUSED(e);
  8. QPainter qp(this);
  9. drawLines(&qp);
  10. }
  11. void Lines::drawLines(QPainter *qp) {
  12. QPen pen(Qt::black, 2, Qt::SolidLine);
  13. qp->setPen(pen);
  14. qp->drawLine(20, 40, 250, 40);
  15. pen.setStyle(Qt::DashLine);
  16. qp->setPen(pen);
  17. qp->drawLine(20, 80, 250, 80);
  18. pen.setStyle(Qt::DashDotLine);
  19. qp->setPen(pen);
  20. qp->drawLine(20, 120, 250, 120);
  21. pen.setStyle(Qt::DotLine);
  22. qp->setPen(pen);
  23. qp->drawLine(20, 160, 250, 160);
  24. pen.setStyle(Qt::DashDotDotLine);
  25. qp->setPen(pen);
  26. qp->drawLine(20, 200, 250, 200);
  27. QVector<qreal> dashes;
  28. qreal space = 4;
  29. dashes << 1 << space << 5 << space;
  30. pen.setStyle(Qt::CustomDashLine);
  31. pen.setDashPattern(dashes);
  32. qp->setPen(pen);
  33. qp->drawLine(20, 240, 250, 240);
  34. }

我们在窗口上画了六行; 每条线都有不同的笔样式。

  1. void Lines::paintEvent(QPaintEvent *e) {
  2. Q_UNUSED(e);
  3. QPainter qp(this);
  4. drawLines(&qp);
  5. }

更新小部件时将调用paintEvent()。 在这里我们创建QPainter对象并进行绘制。 由于我们不使用QPaintEvent对象,因此可以通过Q_UNUSED宏来抑制编译器警告。 实际图形委托给drawLines()方法。

  1. QPen pen(Qt::black, 2, Qt::SolidLine);
  2. qp->setPen(pen);

我们创建一个QPen对象。 笔是实心的,2px 粗,是黑色的。 笔用于绘制线条和形状轮廓。 使用setPen()方法将笔设置为画家对象。

  1. qp->drawLine(20, 40, 250, 40);

drawLine()方法画一条线。 四个参数是窗口上两个点的坐标。

  1. pen.setStyle(Qt::DashLine);

QPen行的setStyle()方法设置笔样式-Qt::DashLine

main.cpp

  1. #include <QApplication>
  2. #include "lines.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. Lines window;
  6. window.resize(280, 270);
  7. window.setWindowTitle("Lines");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

Qt5 中的绘图 - 图1

图:直线

颜色

颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。 有效的 RGB 值在 0 到 255 之间。在下面的示例中,我们绘制了九个矩形,其中填充了九种不同的颜色。

colours.h

  1. #pragma once
  2. #include <QWidget>
  3. class Colours : public QWidget {
  4. public:
  5. Colours(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

colours.cpp

  1. #include <QPainter>
  2. #include "colours.h"
  3. Colours::Colours(QWidget *parent)
  4. : QWidget(parent)
  5. { }
  6. void Colours::paintEvent(QPaintEvent *e) {
  7. Q_UNUSED(e);
  8. doPainting();
  9. }
  10. void Colours::doPainting() {
  11. QPainter painter(this);
  12. painter.setPen(QColor("#d4d4d4"));
  13. painter.setBrush(QBrush("#c56c00"));
  14. painter.drawRect(10, 15, 90, 60);
  15. painter.setBrush(QBrush("#1ac500"));
  16. painter.drawRect(130, 15, 90, 60);
  17. painter.setBrush(QBrush("#539e47"));
  18. painter.drawRect(250, 15, 90, 60);
  19. painter.setBrush(QBrush("#004fc5"));
  20. painter.drawRect(10, 105, 90, 60);
  21. painter.setBrush(QBrush("#c50024"));
  22. painter.drawRect(130, 105, 90, 60);
  23. painter.setBrush(QBrush("#9e4757"));
  24. painter.drawRect(250, 105, 90, 60);
  25. painter.setBrush(QBrush("#5f3b00"));
  26. painter.drawRect(10, 195, 90, 60);
  27. painter.setBrush(QBrush("#4c4c4c"));
  28. painter.drawRect(130, 195, 90, 60);
  29. painter.setBrush(QBrush("#785f36"));
  30. painter.drawRect(250, 195, 90, 60);
  31. }

我们绘制九个不同颜色填充的矩形。 矩形的轮廓是灰色的。

  1. painter.setBrush(QBrush("#c56c00"));
  2. painter.drawRect(10, 15, 90, 60);

QBrush类定义QPainter绘制的形状的填充图案。 drawRect()方法绘制一个矩形。 它绘制一个矩形,其左上角位于 x,y 点,并具有给定的宽度和高度。 我们使用十六进制表示法来指定颜色值。

main.cpp

  1. #include <QApplication>
  2. #include "colours.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. Colours window;
  6. window.resize(360, 280);
  7. window.setWindowTitle("Colours");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

Qt5 中的绘图 - 图2

图:颜色

图案

以下编程代码示例与上一个示例相似。 这次我们用各种预定义的图案填充矩形。

patterns.h

  1. #pragma once
  2. #include <QWidget>
  3. class Patterns : public QWidget {
  4. public:
  5. Patterns(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

patterns.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include "patterns.h"
  4. Patterns::Patterns(QWidget *parent)
  5. : QWidget(parent)
  6. { }
  7. void Patterns::paintEvent(QPaintEvent *e) {
  8. Q_UNUSED(e);
  9. doPainting();
  10. }
  11. void Patterns::doPainting() {
  12. QPainter painter(this);
  13. painter.setPen(Qt::NoPen);
  14. painter.setBrush(Qt::HorPattern);
  15. painter.drawRect(10, 15, 90, 60);
  16. painter.setBrush(Qt::VerPattern);
  17. painter.drawRect(130, 15, 90, 60);
  18. painter.setBrush(Qt::CrossPattern);
  19. painter.drawRect(250, 15, 90, 60);
  20. painter.setBrush(Qt::Dense7Pattern);
  21. painter.drawRect(10, 105, 90, 60);
  22. painter.setBrush(Qt::Dense6Pattern);
  23. painter.drawRect(130, 105, 90, 60);
  24. painter.setBrush(Qt::Dense5Pattern);
  25. painter.drawRect(250, 105, 90, 60);
  26. painter.setBrush(Qt::BDiagPattern);
  27. painter.drawRect(10, 195, 90, 60);
  28. painter.setBrush(Qt::FDiagPattern);
  29. painter.drawRect(130, 195, 90, 60);
  30. painter.setBrush(Qt::DiagCrossPattern);
  31. painter.drawRect(250, 195, 90, 60);
  32. }

我们用各种画笔图案绘制了九个矩形。

  1. painter.setBrush(Qt::HorPattern);
  2. painter.drawRect(10, 15, 90, 60);

我们绘制具有特定图案的矩形。 Qt::HorPattern是用于创建水平线条图案的常数。

main.cpp

  1. #include <QDesktopWidget>
  2. #include <QApplication>
  3. #include "patterns.h"
  4. int main(int argc, char *argv[]) {
  5. QApplication app(argc, argv);
  6. Patterns window;
  7. window.resize(350, 280);
  8. window.setWindowTitle("Patterns");
  9. window.show();
  10. return app.exec();
  11. }

这是主文件。

Qt5 中的绘图 - 图3

图:图案

透明矩形

透明性是指能够透视材料的质量。 了解透明度的最简单方法是想象一块玻璃或水。 从技术上讲,光线可以穿过玻璃,这样我们就可以看到玻璃后面的物体。

在计算机图形学中,我们可以使用 alpha 合成来实现透明效果。 Alpha 合成是将图像与背景组合以创建部分透明外观的过程。 合成过程使用 Alpha 通道。

transparent_rectangles.h

  1. #pragma once
  2. #include <QWidget>
  3. class TransparentRectangles : public QWidget {
  4. public:
  5. TransparentRectangles(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *event);
  8. void doPainting();
  9. };

这是头文件。

transparent_rectangles.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include <QPainterPath>
  4. #include "transparent_rectangles.h"
  5. TransparentRectangles::TransparentRectangles(QWidget *parent)
  6. : QWidget(parent)
  7. { }
  8. void TransparentRectangles::paintEvent(QPaintEvent *e) {
  9. Q_UNUSED(e);
  10. doPainting();
  11. }
  12. void TransparentRectangles::doPainting() {
  13. QPainter painter(this);
  14. for (int i=1; i<=11; i++) {
  15. painter.setOpacity(i*0.1);
  16. painter.fillRect(50*i, 20, 40, 40, Qt::darkGray);
  17. }
  18. }

该示例绘制了十个透明度不同的矩形。

  1. painter.setOpacity(i*0.1);

setOpacity()方法设置画家的不透明度。 该值应在 0.0 到 1.0 的范围内,其中 0.0 是完全透明的,而 1.0 是完全不透明的。

main.cpp

  1. #include <QDesktopWidget>
  2. #include <QApplication>
  3. #include "transparent_rectangles.h"
  4. int main(int argc, char *argv[]) {
  5. QApplication app(argc, argv);
  6. TransparentRectangles window;
  7. window.resize(630, 90);
  8. window.setWindowTitle("Transparent rectangles");
  9. window.show();
  10. return app.exec();
  11. }

这是主文件。

Qt5 中的绘图 - 图4

图:透明矩形

甜甜圈

在下面的示例中,我们将创建一个甜甜圈形状。

donut.h

  1. #pragma once
  2. #include <QWidget>
  3. class Donut : public QWidget {
  4. public:
  5. Donut(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

donut.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include "donut.h"
  4. Donut::Donut(QWidget *parent)
  5. : QWidget(parent)
  6. { }
  7. void Donut::paintEvent(QPaintEvent *e) {
  8. Q_UNUSED(e);
  9. doPainting();
  10. }
  11. void Donut::doPainting() {
  12. QPainter painter(this);
  13. painter.setPen(QPen(QBrush("#535353"), 0.5));
  14. painter.setRenderHint(QPainter::Antialiasing);
  15. int h = height();
  16. int w = width();
  17. painter.translate(QPoint(w/2, h/2));
  18. for (qreal rot=0; rot < 360.0; rot+=5.0 ) {
  19. painter.drawEllipse(-125, -40, 250, 80);
  20. painter.rotate(5.0);
  21. }
  22. }

“甜甜圈”是类似于此类食物的高级几何形状。 我们通过绘制 72 个旋转椭圆来创建它。

  1. painter.setRenderHint(QPainter::Antialiasing);

我们将以抗锯齿模式绘制。 渲染将具有更高的质量。

  1. int h = height();
  2. int w = width();
  3. painter.translate(QPoint(w/2, h/2));

这些行将坐标系的起点移到窗口的中间。 默认情况下,它位于 0、0 点。 换句话说,在窗口的左上角。 通过移动坐标系,绘图会容易得多。

  1. for (qreal rot=0; rot < 360.0; rot+=5.0 ) {
  2. painter.drawEllipse(-125, -40, 250, 80);
  3. painter.rotate(5.0);
  4. }

在此循环中,我们绘制了 72 个旋转的椭圆。

main.cpp

  1. #include <QDesktopWidget>
  2. #include <QApplication>
  3. #include "donut.h"
  4. int main(int argc, char *argv[]) {
  5. QApplication app(argc, argv);
  6. Donut window;
  7. window.resize(350, 280);
  8. window.setWindowTitle("Donut");
  9. window.show();
  10. return app.exec();
  11. }

这是主文件。

形状

Qt5 绘图 API 可以绘制各种形状。 以下编程代码示例显示了其中一些。

shapes.h

  1. #pragma once
  2. #include <QWidget>
  3. class Shapes : public QWidget {
  4. public:
  5. Shapes(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

shapes.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include <QPainterPath>
  4. #include "shapes.h"
  5. Shapes::Shapes(QWidget *parent)
  6. : QWidget(parent)
  7. { }
  8. void Shapes::paintEvent(QPaintEvent *e) {
  9. Q_UNUSED(e);
  10. doPainting();
  11. }
  12. void Shapes::doPainting() {
  13. QPainter painter(this);
  14. painter.setRenderHint(QPainter::Antialiasing);
  15. painter.setPen(QPen(QBrush("#888"), 1));
  16. painter.setBrush(QBrush(QColor("#888")));
  17. QPainterPath path1;
  18. path1.moveTo(5, 5);
  19. path1.cubicTo(40, 5, 50, 50, 99, 99);
  20. path1.cubicTo(5, 99, 50, 50, 5, 5);
  21. painter.drawPath(path1);
  22. painter.drawPie(130, 20, 90, 60, 30*16, 120*16);
  23. painter.drawChord(240, 30, 90, 60, 0, 16*180);
  24. painter.drawRoundRect(20, 120, 80, 50);
  25. QPolygon polygon({QPoint(130, 140), QPoint(180, 170), QPoint(180, 140),
  26. QPoint(220, 110), QPoint(140, 100)});
  27. painter.drawPolygon(polygon);
  28. painter.drawRect(250, 110, 60, 60);
  29. QPointF baseline(20, 250);
  30. QFont font("Georgia", 55);
  31. QPainterPath path2;
  32. path2.addText(baseline, font, "Q");
  33. painter.drawPath(path2);
  34. painter.drawEllipse(140, 200, 60, 60);
  35. painter.drawEllipse(240, 200, 90, 60);
  36. }

我们绘制了九种不同的形状。

  1. QPainterPath path1;
  2. path1.moveTo(5, 5);
  3. path1.cubicTo(40, 5, 50, 50, 99, 99);
  4. path1.cubicTo(5, 99, 50, 50, 5, 5);
  5. painter.drawPath(path1);

QPainterPath是用于创建复杂形状的对象。 我们用它来绘制贝塞尔曲线。

  1. painter.drawPie(130, 20, 90, 60, 30*16, 120*16);
  2. painter.drawChord(240, 30, 90, 60, 0, 16*180);
  3. painter.drawRoundRect(20, 120, 80, 50);

这些代码行绘制了一个饼图,一个和弦和一个圆角矩形。

  1. QPolygon polygon({QPoint(130, 140), QPoint(180, 170), QPoint(180, 140),
  2. QPoint(220, 110), QPoint(140, 100)});
  3. painter.drawPolygon(polygon);

在这里,我们使用drawPolygon()方法绘制一个多边形。 多边形由五个点组成。

  1. QPointF baseline(20, 250);
  2. QFont font("Georgia", 55);
  3. QPainterPath path2;
  4. path2.addText(baseline, font, "Q");
  5. painter.drawPath(path2);

Qt5 允许基于字体字符创建路径。

  1. painter.drawEllipse(140, 200, 60, 60);
  2. painter.drawEllipse(240, 200, 90, 60);

drawEllipse()也会绘制一个椭圆和一个圆。 圆是椭圆的特例。 参数是矩形起点的 x 和 y 坐标以及椭圆边界矩形的宽度和高度。

main.cpp

  1. #include <QDesktopWidget>
  2. #include <QApplication>
  3. #include "shapes.h"
  4. int main(int argc, char *argv[]) {
  5. QApplication app(argc, argv);
  6. Shapes window;
  7. window.resize(350, 280);
  8. window.setWindowTitle("Shapes");
  9. window.show();
  10. return app.exec();
  11. }

这是示例的主文件。

Qt5 中的绘图 - 图5

图:形状

渐变

在计算机图形学中,渐变是从浅到深或从一种颜色到另一种颜色的阴影的平滑混合。 在 2D 绘图程序和绘图程序中,渐变用于创建彩色背景和特殊效果以及模拟灯光和阴影。

以下代码示例显示了如何创建线性渐变。

linear_gradients.h

  1. #pragma once
  2. #include <QWidget>
  3. class LinearGradients : public QWidget {
  4. public:
  5. LinearGradients(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

linear_gradients.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include "linear_gradients.h"
  4. LinearGradients::LinearGradients(QWidget *parent)
  5. : QWidget(parent)
  6. { }
  7. void LinearGradients::paintEvent(QPaintEvent *e) {
  8. Q_UNUSED(e);
  9. doPainting();
  10. }
  11. void LinearGradients::doPainting() {
  12. QPainter painter(this);
  13. QLinearGradient grad1(0, 20, 0, 110);
  14. grad1.setColorAt(0.1, Qt::black);
  15. grad1.setColorAt(0.5, Qt::yellow);
  16. grad1.setColorAt(0.9, Qt::black);
  17. painter.fillRect(20, 20, 300, 90, grad1);
  18. QLinearGradient grad2(0, 55, 250, 0);
  19. grad2.setColorAt(0.2, Qt::black);
  20. grad2.setColorAt(0.5, Qt::red);
  21. grad2.setColorAt(0.8, Qt::black);
  22. painter.fillRect(20, 140, 300, 100, grad2);
  23. }

在代码示例中,我们绘制了两个矩形,并用线性渐变填充它们。

  1. QLinearGradient grad1(0, 20, 0, 110);

QLinearGradient构造线性渐变,并在两个点之间作为参数提供插值区域。

  1. grad1.setColorAt(0.1, Qt::black);
  2. grad1.setColorAt(0.5, Qt::yellow);
  3. grad1.setColorAt(0.9, Qt::black);

使用停止点定义渐变中的颜色。 setColorAt()在给定位置以给定颜色创建一个停止点。

  1. painter.fillRect(20, 20, 300, 90, grad1);

我们用渐变填充矩形。

main.cpp

  1. #include <QApplication>
  2. #include "linear_gradients.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. LinearGradients window;
  6. window.resize(350, 260);
  7. window.setWindowTitle("Linear gradients");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

Qt5 中的绘图 - 图6

图:线性渐变

径向渐变

径向渐变是两个圆之间颜色或阴影的混合。

radial_gradient.h

  1. #pragma once
  2. #include <QWidget>
  3. class RadialGradient : public QWidget {
  4. public:
  5. RadialGradient(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *e);
  8. private:
  9. void doPainting();
  10. };

这是头文件。

radial_gradient.cpp

  1. #include <QApplication>
  2. #include <QPainter>
  3. #include "radial_gradient.h"
  4. RadialGradient::RadialGradient(QWidget *parent)
  5. : QWidget(parent)
  6. { }
  7. void RadialGradient::paintEvent(QPaintEvent *e) {
  8. Q_UNUSED(e);
  9. doPainting();
  10. }
  11. void RadialGradient::doPainting() {
  12. QPainter painter(this);
  13. int h = height();
  14. int w = width();
  15. QRadialGradient grad1(w/2, h/2, 80);
  16. grad1.setColorAt(0, QColor("#032E91"));
  17. grad1.setColorAt(0.3, Qt::white);
  18. grad1.setColorAt(1, QColor("#032E91"));
  19. painter.fillRect(0, 0, w, h, grad1);
  20. }

该示例创建了一个径向渐变; 渐变从窗口的中心扩散。

  1. QRadialGradient grad1(w/2, h/2, 80);

QRadialGradient创建一个径向渐变; 它在焦点和围绕它的圆上的端点之间插入颜色。 参数是圆心和半径的坐标。 焦点位于圆的中心。

  1. grad1.setColorAt(0, QColor("#032E91"));
  2. grad1.setColorAt(0.3, Qt::white);
  3. grad1.setColorAt(1, QColor("#032E91"));

setColorAt()方法定义彩色挡块。

  1. painter.fillRect(0, 0, w, h, grad1);

窗口的整个区域都充满了径向渐变。

main.cpp

  1. #include <QApplication>
  2. #include "radial_gradient.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. RadialGradient window;
  6. window.resize(300, 250);
  7. window.setWindowTitle("Radial gradient");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

Qt5 中的绘图 - 图7

图:径向渐变

泡泡

在本 C++ Qt5 教程章节的最后一个示例中,我们创建一个泡泡效果。 该示例显示一个不断增长的居中文本,该文本从某个点逐渐淡出。 这是一种非常常见的效果,您通常可以在网络上的 Flash 动画中看到这种效果。

puff.h

  1. #pragma once
  2. #include <QWidget>
  3. class Puff : public QWidget {
  4. public:
  5. Puff(QWidget *parent = 0);
  6. protected:
  7. void paintEvent(QPaintEvent *event);
  8. void timerEvent(QTimerEvent *event);
  9. private:
  10. int x;
  11. qreal opacity;
  12. int timerId;
  13. void doPainting();
  14. };

在头文件中,我们定义了两个事件处理器:绘图事件处理器和计时器处理器。

puff.cpp

  1. #include <QPainter>
  2. #include <QTimer>
  3. #include <QTextStream>
  4. #include "puff.h"
  5. Puff::Puff(QWidget *parent)
  6. : QWidget(parent) {
  7. x = 1;
  8. opacity = 1.0;
  9. timerId = startTimer(15);
  10. }
  11. void Puff::paintEvent(QPaintEvent *e) {
  12. Q_UNUSED(e);
  13. doPainting();
  14. }
  15. void Puff::doPainting() {
  16. QPainter painter(this);
  17. QTextStream out(stdout);
  18. QString text = "ZetCode";
  19. painter.setPen(QPen(QBrush("#575555"), 1));
  20. QFont font("Courier", x, QFont::DemiBold);
  21. QFontMetrics fm(font);
  22. int textWidth = fm.width(text);
  23. painter.setFont(font);
  24. if (x > 10) {
  25. opacity -= 0.01;
  26. painter.setOpacity(opacity);
  27. }
  28. if (opacity <= 0) {
  29. killTimer(timerId);
  30. out << "timer stopped" << endl;
  31. }
  32. int h = height();
  33. int w = width();
  34. painter.translate(QPoint(w/2, h/2));
  35. painter.drawText(-textWidth/2, 0, text);
  36. }
  37. void Puff::timerEvent(QTimerEvent *e) {
  38. Q_UNUSED(e);
  39. x += 1;
  40. repaint();
  41. }

这是puff.cpp文件。

  1. Puff::Puff(QWidget *parent)
  2. : QWidget(parent) {
  3. x = 1;
  4. opacity = 1.0;
  5. timerId = startTimer(15);
  6. }

在构造器中,我们启动计时器。 每 15ms 会生成一个计时器事件。

  1. void Puff::timerEvent(QTimerEvent *e) {
  2. Q_UNUSED(e);
  3. x += 1;
  4. repaint();
  5. }

timerEvent()内,我们增加字体大小并重新绘制小部件。

  1. if (x > 10) {
  2. opacity -= 0.01;
  3. painter.setOpacity(opacity);
  4. }

如果字体大小大于 10 磅,我们将逐渐降低不透明度; 文字开始消失。

  1. if (opacity <= 0) {
  2. killTimer(timerId);
  3. out << "timer stopped" << endl;
  4. }

如果文字完全消失,我们将杀死计时器。

main.cpp

  1. #include <QApplication>
  2. #include "puff.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. Puff window;
  6. window.resize(350, 280);
  7. window.setWindowTitle("Puff");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

本章是关于 Qt5 中的绘图的。