简体   繁体   English

限制通过pixmap绘制qpainter

[英]Restrict drawing of qpainter over pixmap

I am using QGraphicsView and Scene over which two QGraphicsPixmap item are set. 我正在使用QGraphicsView和Scene,在其上设置了两个QGraphicsPixmap项。 One is showing some image, another one is having transparent pixmap which is used to show marking. 一个正在显示一些图像,另一个正在显示用于显示标记的透明像素图。

I am using qpainter to draw over a transparent qpixmap. 我正在使用qpainter绘制透明的qpixmap。

I am using drawline between two points with qpen having rounded point with some pen size. 我在两点之间使用画线,qpen具有一些笔大小的圆形点。

Problem is: 问题是:

If i load some png image, with some part of image being transparent, I want to disable marking (on marking pixmap) over transparent region of image. 如果我加载一些png图像,而图像的某些部分是透明的,则我想在图像的透明区域上禁用标记(在标记像素图上)。 Is there any way to automatically restrict area of marking of qpainter? 有什么方法可以自动限制qpainter的标记区域?

It would be easiest to combine your two pixmaps into a single QGraphicsPixmapItem . 将两个像素图组合到单个QGraphicsPixmapItem中将是最简单的。 Then you could simply use the correct QPainter::CompositionMode , which would need to be 然后,您可以简单地使用正确的QPainter::CompositionMode ,这需要

QPainter::CompositionMode_SourceAtop

The source pixel is blended on top of the destination, with the alpha of the source pixel reduced by the alpha of the destination pixel. 源像素在目标顶部混合,源像素的alpha减小目标像素的alpha。

eg: 例如:

QPixmap markingPixmap(sourceImage.size());
markingPixmap.fill(Qt::transparent);

{ // scope for painter1
    QPainter painter1(&markingPixmap);
    painter1.setPen(...);
    painter1.drawLine(...);
}

QPainter painter(&sourceImage);
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.drawPixmap(0, 0, markingPixmap);

(Code untested!) (代码未经测试!)


Or you could even use a QBitmap , see QPainter::drawPixmap() : 或者甚至可以使用QBitmap ,请参见QPainter::drawPixmap()

If pixmap is a QBitmap it is drawn with the bits that are "set" using the pens color. 如果pixmap是QBitmap,则使用笔颜色使用“置位”的位进行绘制。 If backgroundMode is Qt::OpaqueMode, the "unset" bits are drawn using the color of the background brush; 如果backgroundMode为Qt :: OpaqueMode,则使用背景笔刷的颜色绘制“未设置”位; if backgroundMode is Qt::TransparentMode, the "unset" bits are transparent. 如果backgroundMode是Qt :: TransparentMode,则“未设置”位是透明的。 Drawing bitmaps with gradient or texture colors is not supported. 不支持使用渐变或纹理颜色绘制位图。

(You would need to try if this respects the CompositionMode.) (如果这符合CompositionMode,则需要尝试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM