欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > Qt:玩转QPainter序列八

Qt:玩转QPainter序列八

2025/5/13 13:53:52 来源:https://blog.csdn.net/m0_71489826/article/details/141574736  浏览:    关键词:Qt:玩转QPainter序列八

前言

继续

正文

在这里插入图片描述

#if QT_DEPRECATED_SINCE(5, 13) #endif

包裹的部分是被弃用的函数,我以前的文章有说怎么看,这里就不说了。

1.drawTiledPixmap系列函数 绘制平铺的图像

QPainterdrawTiledPixmap 函数用于在绘图设备上平铺绘制 QPixmap 图像。这些函数的目的都是将 QPixmap 图像平铺在指定的区域内。下面详细解释每个版本的用法和参数:

  • void drawTiledPixmap(const QRectF &rect, const QPixmap &pm, const QPointF &offset = QPointF());

    • 参数:
      • rect: 指定要绘制 QPixmap 图像的区域,类型为 QRectF,通常是一个浮点矩形。
      • pm: 要平铺的 QPixmap 对象。
      • offset: 指定平铺图像的偏移量,类型为 QPointF。默认为 (0, 0),即没有偏移。
  • inline void drawTiledPixmap(int x, int y, int w, int h, const QPixmap &, int sx=0, int sy=0);

    • 参数:
      • x, y: 指定平铺区域的左上角位置,类型为整数。
      • w, h: 指定平铺区域的宽度和高度,类型为整数。
      • pm: 要平铺的 QPixmap 对象。
      • sx, sy: 图像的起始点偏移量,类型为整数,默认为 (0, 0)
  • inline void drawTiledPixmap(const QRect &, const QPixmap &, const QPoint & = QPoint());

    • 参数:
      • QRect: 指定要绘制 QPixmap 图像的区域,类型为整数矩形。
      • pm: 要平铺的 QPixmap 对象。
      • offset: 图像的偏移量,类型为 QPoint。默认为 (0, 0),即没有偏移。
注意事项
  • 1.这里的偏移量,第一个参数sx为正时图像向左移动,为负时向右移动;第二个参数sy为正时图像向上移动,为负时向下移动
  • 2.如果用纯色来填充pixmap时,无论你如何调整偏移图像都不会改变,以为图像变化了你也看不出来(纯色).
  • 3.对于你指定的rect,平铺的图像会将你指定的rect填充完

例子

void PaintWidget::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置抗锯齿painter.setRenderHint(QPainter::Antialiasing, true);// 设置粗一点QPen pen;pen.setWidth(3);painter.setPen(pen);// 用大小初始化一个QPixmapQPixmap pixmap(10,10);pixmap.fill(Qt::blue); // 填充蓝色图像用于测试QRect rect(200,0,width(),height());// 使用 drawTiledPixmap(int x, int y, int w, int h, const QPixmap &, int sx=0, int sy=0)int x = 10, y = 10, w = 100, h = 100;// 图像起始点偏移量,设置了也没用对于纯色而言看不出来int sx = 20, sy = 20;painter.drawTiledPixmap(x, y, w, h, pixmap, sx, sy);// 使用 drawTiledPixmap(const QRect &, const QPixmap &, const QPoint & = QPoint())QPixmap pixmap2("D:/all_the_code/qt_code/ts/playQPainter/t1.png");// 图像起始点偏移量,向左上移动QPoint offset(-10,10);painter.drawTiledPixmap(rect, pixmap2, offset);
}

在这里插入图片描述

2.drawPicture系列函数 绘制图片

#ifndef QT_NO_PICTURE
  • 这是一条预处理指令,意思是在编译时,如果未定义 QT_NO_PICTURE,则编译和包含随后的代码。换句话说,如果 QT_NO_PICTURE 被定义了,意味着Qt库被配置为不支持 QPicture,则这些绘制函数将被排除在编译之外。
  1. void drawPicture(const QPointF &p, const QPicture &picture);

    • 这个函数在浮点数坐标 (p.x(), p.y()) 处绘制 QPictureQPointF 是一个表示二维浮点数坐标的类。
  2. inline void drawPicture(int x, int y, const QPicture &picture);

    • 这是一个内联函数,用于在整数坐标 (x, y) 处绘制 QPictureinline 关键字提示编译器尝试将此函数的代码直接嵌入到调用点,以减少函数调用的开销。
  3. inline void drawPicture(const QPoint &p, const QPicture &picture);

    • 另一个内联函数,用于在整数坐标 p 处绘制 QPicture,其中 QPoint 是一个表示二维整数坐标的类。

例子

void PaintWidget::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置抗锯齿painter.setRenderHint(QPainter::Antialiasing, true);// 设置粗一点QPen pen;pen.setWidth(3);painter.setPen(pen);// 创建一个 QPicture 对象并记录绘图操作//QPicture 主要用于记录和回放绘图命令,而不是直接加载和保存图像。QPicture picture;QPainter picPainter(&picture);// 在 picture 中绘制图像picPainter.setBrush(Qt::green);picPainter.drawRect(10, 10, 100, 100);  // 绘制一个绿色矩形picPainter.drawEllipse(50, 50, 50, 50); // 绘制一个椭圆picPainter.end();  // 结束记录// 使用 drawPicture 在 widget 上绘制已记录的图像painter.drawPicture(0, 0, picture);}

在这里插入图片描述

3.drawPixmap系列函数 绘制像素图(Pixmap)

    1. void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect);
    • 功能:将 pixmapsourceRect 区域绘制到目标矩形 targetRect 中。
    • 参数
      • targetRect:目标区域,以浮点矩形 QRectF 表示,指定 pixmap 将被绘制到的区域。
      • pixmap:要绘制的 QPixmap 对象。
      • sourceRect:源区域,以浮点矩形 QRectF 表示,指定 pixmap 中要绘制的部分。
    1. inline void drawPixmap(const QRect &targetRect, const QPixmap &pixmap, const QRect &sourceRect);
    • 功能:与上一个函数类似,但使用整数矩形 QRect 代替浮点矩形。
    1. inline void drawPixmap(int x, int y, int w, int h, const QPixmap &pm, int sx, int sy, int sw, int sh);
    • 功能:将 pm 的某一部分(指定为从点 (sx, sy) 开始,宽 sw,高 sh 的区域)绘制到目标区域 (x, y, w, h)
    • 参数
      • x, y, w, h:目标区域的左上角坐标 (x, y) 及其宽度 w 和高度 h
      • pm:要绘制的 QPixmap 对象。
      • sx, sy, sw, sh:源区域的左上角坐标 (sx, sy) 及其宽度 sw 和高度 sh
    1. inline void drawPixmap(int x, int y, const QPixmap &pm, int sx, int sy, int sw, int sh);
  • 功能:在目标点 (x, y) 处绘制 pm 的某一部分(从 (sx, sy) 开始,宽 sw,高 sh 的区域)。

    1. inline void drawPixmap(const QPointF &p, const QPixmap &pm, const QRectF &sr);
  • 功能:在浮点坐标 p 处绘制 pmsr 区域。

    1. inline void drawPixmap(const QPoint &p, const QPixmap &pm, const QRect &sr);
  • 功能:在整数坐标 p 处绘制 pmsr 区域。

    1. void drawPixmap(const QPointF &p, const QPixmap &pm);
  • 功能:在浮点坐标 p 处绘制完整的 QPixmap 对象 pm

    1. inline void drawPixmap(const QPoint &p, const QPixmap &pm);
  • 功能:在整数坐标 p 处绘制完整的 QPixmap 对象 pm

    1. inline void drawPixmap(int x, int y, const QPixmap &pm);
  • 功能:在整数坐标 (x, y) 处绘制完整的 QPixmap 对象 pm

    1. inline void drawPixmap(const QRect &r, const QPixmap &pm);
  • 功能:将 QPixmap 对象 pm 缩放或拉伸以适应目标矩形 r,并绘制出来。

    1. inline void drawPixmap(int x, int y, int w, int h, const QPixmap &pm);
  • 功能:将 QPixmap 对象 pm 缩放或拉伸以适应目标矩形 (x, y, w, h),并绘制出来。

例子

void PaintWidget::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置抗锯齿painter.setRenderHint(QPainter::Antialiasing, true);// 设置粗一点QPen pen;pen.setWidth(3);painter.setPen(pen);// 加载图像到 QPixmap,替换为你的图片路径QPixmap pixmap("D:/all_the_code/qt_code/ts/playQPainter/t1.png");// 测试1:inline void drawPixmap(int x, int y, const QPixmap &pm);// 在原始大小下绘制图片painter.drawPixmap(10, 10, pixmap);// 测试2:inline void drawPixmap(int x, int y, int w, int h, const QPixmap &pm);// 缩放绘制图片到指定大小painter.drawPixmap(200, 10, 100, 100, pixmap);// 测试3:inline void drawPixmap(int x, int y, int w, int h, const QPixmap &pm, int sx, int sy, int sw, int sh);//绘制图片的一部分,将pm 的某一部分(指定为从点 (sx, sy) 开始,宽sw,高sh的区域)绘制到目标区域(x, y, w, h)painter.drawPixmap(100, 200, 50, 50, pixmap, 30,30,50,50);// 测试4:使用 QPainter 的平移操作绘制图片painter.translate(200, 200);painter.drawPixmap(0, 0, pixmap.scaled(50, 50));// 测试5:以某个角度旋转并绘制图片painter.resetTransform();  // 重置平移变换painter.translate(300, 200);painter.rotate(45);painter.drawPixmap(0, 0, pixmap.scaled(100, 100));// 测试6:inline void drawPixmap(const QRect &r, const QPixmap &pm);// 将 `QPixmap` 对象 `pm` 缩放或拉伸以适应目标矩形 `r`,并绘制出来QRect targetRect(350, 10, 150, 150);painter.drawPixmap(targetRect, pixmap);
}

在这里插入图片描述

4.drawPixmapFragments 绘制像素图片段

  • void drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, PixmapFragmentHints hints = PixmapFragmentHints());用于将一个 QPixmap 对象中的多个片段(或碎片)绘制到指定的目标区域。这些片段由 PixmapFragment 结构体定义,可以在一个绘图操作中同时绘制多个片段。
参数解析:
  1. const PixmapFragment *fragments:

    • PixmapFragment的一个对象,PixmapFragment 是一个类,在序列二说过。
  2. int fragmentCount:

    • 片段的数量,表示 fragments 数组中有多少个 PixmapFragment 需要被绘制。
  3. const QPixmap &pixmap:

    • 被绘制的 QPixmap 对象,所有片段的源图像。
  4. PixmapFragmentHints hints = PixmapFragmentHints():

    • 可选参数,定义一些提示(hints),用于控制片段绘制的行为。默认是空的 PixmapFragmentHints,不提供任何特殊的绘制提示。PixmapFragmentHints 是一个枚举值。

用途:

这个函数非常适合需要从同一个 QPixmap 中绘制多个不同部分的场景,尤其是在游戏开发或复杂UI设计中,它可以用来绘制精灵图(Sprite)、拼图等。通过一次调用 drawPixmapFragments,可以避免多个 drawPixmap 调用带来的开销,提高渲染效率。

例子

void PaintWidget::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置抗锯齿painter.setRenderHint(QPainter::Antialiasing, true);// 设置粗一点QPen pen;pen.setWidth(3);painter.setPen(pen);// 加载图像到 QPixmap,替换为你的图片路径QPixmap pixmap("D:/all_the_code/qt_code/ts/playQPainter/t1.png");// 定义片段,PixmapFragment是一个类,再序列二讲过,这里创建一个对象的数组QPainter::PixmapFragment fragments[3];fragments[0] = QPainter::PixmapFragment::create(QPointF(50, 50), QRectF(0, 0, 50, 50)); // 左上角部分fragments[1] = QPainter::PixmapFragment::create(QPointF(150, 50), QRectF(50, 0, 50, 50)); // 右上角部分fragments[2] = QPainter::PixmapFragment::create(QPointF(100, 150), QRectF(0, 50, 50, 50)); // 左下角部分// 调整片段属性fragments[0].rotation = 45; // 旋转 45 度fragments[1].scaleX = 2.0;  // 横向放大 2 倍fragments[1].scaleY = 0.5;  // 纵向缩小 0.5 倍fragments[2].opacity = 0.5; // 设置半透明// 绘制片段painter.drawPixmapFragments(fragments, 3, pixmap);
}

在这里插入图片描述

5.drawImage系列函数 绘制图像

  1. void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags = Qt::AutoColor);

    • 描述: 将 QImage 对象的 sourceRect 区域绘制到 targetRect 区域。sourceRect 定义了从 QImage 中绘制的区域,targetRect 定义了在目标设备上绘制的区域。
    • flags: 图像转换标志(默认是 Qt::AutoColor),用于控制绘制时的颜色处理,如是否使用透明背景等。
  2. inline void drawImage(const QRect &targetRect, const QImage &image, const QRect &sourceRect, Qt::ImageConversionFlags flags = Qt::AutoColor);

    • 描述: 与上述函数类似,但接受 QRect 对象作为参数。sourceRecttargetRect 分别是源图像和目标区域的矩形。
  3. inline void drawImage(const QPointF &p, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags flags = Qt::AutoColor);

    • 描述: 将 QImage 对象的 sr 区域绘制到从 p 开始的位置。sr 定义了从图像中绘制的区域,p 是目标位置的起点。
  4. inline void drawImage(const QPoint &p, const QImage &image, const QRect &sr, Qt::ImageConversionFlags flags = Qt::AutoColor);

    • 描述: 与上述函数类似,但接受 QPoint 对象作为目标位置。sr 是源图像的矩形区域。
  5. inline void drawImage(const QRectF &r, const QImage &image);

    • 描述: 将整个 QImage 对象绘制到 r 区域。r 定义了目标区域的矩形。如果不指定 sourceRect,则整个图像都会被绘制到目标区域。
  6. inline void drawImage(const QRect &r, const QImage &image);

    • 描述: 与上述函数类似,但接受 QRect 对象作为参数。将整个 QImage 绘制到指定的矩形区域 r
  7. void drawImage(const QPointF &p, const QImage &image);

    • 描述: 将整个 QImage 对象绘制到从 p 开始的位置。如果没有指定目标矩形,图像将以其原始尺寸绘制到 p 的位置。

示例代码

下面是一个示例,展示如何使用这些 drawImage 函数将图像绘制到不同的区域:

void PaintWidget::paintEvent(QPaintEvent* event)
{Q_UNUSED(event);QPainter painter(this);// 设置抗锯齿painter.setRenderHint(QPainter::Antialiasing, true);// 设置粗一点QPen pen;pen.setWidth(3);painter.setPen(pen);// 加载图像到 QPixmap,替换为你的图片路径QImage image;image.load("D:/all_the_code/qt_code/ts/playQPainter/t1.png");// 定义目标区域和源图像区域,目标区域越大,图像也会相应增大QRect targetRect(10, 10, 100, 100);QRect sourceRect(0, 0, 50, 50);// 绘制图像的一部分到指定区域painter.drawImage(targetRect, image, sourceRect);// 定义另一个区域和目标位置QRect targetRect2(220, 10, 200, 200);QRect sourceRect2(0, 0, image.width(), image.height());// 绘制图像的全部内容到另一个指定区域painter.drawImage(targetRect2, image, sourceRect2);// 绘制图像的部分到指定位置QPoint position(10, 220);painter.drawImage(position, image, sourceRect);// 直接绘制图像到指定区域QRectF rect(100, 200, 100, 100);painter.drawImage(rect, image);
}

在这里插入图片描述

注意事项
  1. 目标区域与源区域:

    • targetRect 定义了图像在目标设备上的绘制区域。如果 sourceRecttargetRect 的比例不同,图像可能会被拉伸或缩放。
    • sourceRect 定义了从图像中绘制的区域。如果不指定 sourceRect,整个图像会被绘制。
  2. 图像转换标志:

    • flags 参数可以控制图像的颜色处理。例如,Qt::AutoColor 是默认值,会自动处理颜色转换。如果需要特定的图像处理效果,可以设置其他标志。
  3. 绘制区域的尺寸:

    • 确保 targetRect 足够大以容纳图像。如果目标区域比图像小,图像会被裁剪。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词