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

在 Qt4 C++ 编程教程的这一部分中,我们将做一些绘图。

当我们在 Qt4 中进行绘图时,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) : QWidget(parent)
  4. { }
  5. void Lines::paintEvent(QPaintEvent *e) {
  6. Q_UNUSED(e);
  7. QPainter qp(this);
  8. drawLines(&qp);
  9. }
  10. void Lines::drawLines(QPainter *qp) {
  11. QPen pen(Qt::black, 2, Qt::SolidLine);
  12. qp->setPen(pen);
  13. qp->drawLine(20, 40, 250, 40);
  14. pen.setStyle(Qt::DashLine);
  15. qp->setPen(pen);
  16. qp->drawLine(20, 80, 250, 80);
  17. pen.setStyle(Qt::DashDotLine);
  18. qp->setPen(pen);
  19. qp->drawLine(20, 120, 250, 120);
  20. pen.setStyle(Qt::DotLine);
  21. qp->setPen(pen);
  22. qp->drawLine(20, 160, 250, 160);
  23. pen.setStyle(Qt::DashDotDotLine);
  24. qp->setPen(pen);
  25. qp->drawLine(20, 200, 250, 200);
  26. QVector<qreal> dashes;
  27. qreal space = 4;
  28. dashes << 1 << space << 5 << space;
  29. pen.setStyle(Qt::CustomDashLine);
  30. pen.setDashPattern(dashes);
  31. qp->setPen(pen);
  32. qp->drawLine(20, 240, 250, 240);
  33. }

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

  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. }

这是主文件。

Qt4 中的绘图 - 图1

图:直线

色彩

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

colours.h

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

这是头文件。

colours.cpp

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

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

  1. qp.setBrush(QBrush("#c56c00"));
  2. qp.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. }

这是主文件。

Qt4 中的绘图 - 图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 *);
  8. void drawRectangles(QPainter &);
  9. };

头文件。

patterns.cpp

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

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

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

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

main.cpp

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

这是主文件。

Qt4 中的绘图 - 图3

图:图案

甜甜圈

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

donut.h

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

这是头文件。

donut.cpp

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

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

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

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

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

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

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

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

main.cpp

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

这是主文件。

形状

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

shapes.h

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

这是头文件。

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. QPainter painter(this);
  11. painter.setRenderHint(QPainter::Antialiasing);
  12. painter.setPen(QPen(QBrush("#888"), 1));
  13. painter.setBrush(QBrush(QColor("#888")));
  14. QPainterPath path1;
  15. path1.moveTo(5, 5);
  16. path1.cubicTo(40, 5, 50, 50, 99, 99);
  17. path1.cubicTo(5, 99, 50, 50, 5, 5);
  18. painter.drawPath(path1);
  19. painter.drawPie(130, 20, 90, 60, 30*16, 120*16);
  20. painter.drawChord(240, 30, 90, 60, 0, 16*180);
  21. painter.drawRoundRect(20, 120, 80, 50);
  22. QPolygon polygon({QPoint(130, 140), QPoint(180, 170), QPoint(180, 140),
  23. QPoint(220, 110), QPoint(140, 100)});
  24. painter.drawPolygon(polygon);
  25. painter.drawRect(250, 110, 60, 60);
  26. QPointF baseline(20, 250);
  27. QFont font("Georgia", 55);
  28. QPainterPath path2;
  29. path2.addText(baseline, font, "Q");
  30. painter.drawPath(path2);
  31. painter.drawEllipse(140, 200, 60, 60);
  32. painter.drawEllipse(240, 200, 90, 60);
  33. }

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

  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);

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

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

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

main.cpp

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

这是示例的主文件。

Qt4 中的绘图 - 图4

图:形状

渐变

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

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

gradients.h

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

这是头文件。

gradients.cpp

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

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

  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 "gradients.h"
  3. int main(int argc, char *argv[]) {
  4. QApplication app(argc, argv);
  5. Gradient window;
  6. window.resize(350, 260);
  7. window.setWindowTitle("Gradients");
  8. window.show();
  9. return app.exec();
  10. }

这是主文件。

Qt4 中的绘图 - 图5

图:渐变

径向渐变

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

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. };

这是头文件。

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. QPainter painter(this);
  10. int h = height();
  11. int w = width();
  12. QRadialGradient grad1(w/2, h/2, 80);
  13. grad1.setColorAt(0, QColor("#032E91"));
  14. grad1.setColorAt(0.3, Qt::white);
  15. grad1.setColorAt(1, QColor("#032E91"));
  16. painter.fillRect(0, 0, w, h, grad1);
  17. }

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

  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. }

这是主文件。

Qt4 中的绘图 - 图6

图:径向渐变

泡泡

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

puff.h

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

这是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. }

这是主文件。

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