简体   繁体   English

使用QPainter确定在绘制时QImage的哪些像素被更改

[英]Determine which pixels of QImage were changed while painting on it with QPainter

I have class Paintable that is able to paint itself with QPainter provided as an argument: 我有类Paintable ,可以使用QPainter作为参数绘制自己:

class Paintable
{
public:
    virtual void paint(QPainter*) = 0;
};

Instances of this class are being painted on a single QImage: 这个类的实例正在一个QImage上绘制:

QImage paint(const std::vector<Paintable*>& paintables) {
    QImage result;
    QPainter p(&result);
    for(int i = 0; i < paintables.size(); ++i) {
        paintables[i]->paint(&p);
    }
    return result;
}

What I want to achieve is that function paint could also form a matrix of size equal to result image size in which each cell contains a pointer to Paintable which has drawn corresponding pixel in result image (something like z-buffer). 我想要实现的是,函数paint也可以形成一个大小等于result图像大小的矩阵,其中每个单元格包含一个指向Paintable的指针,该指针在result图像中绘制了相应的像素(类似于z-buffer)。

It could be achieved easily if draw methods of QPainter somehow let me know of which pixels of QPaintDevice were changed during last draw operation. 如果QPainter绘制方法以某种方式让我知道QPaintDevice的哪些像素在最后的绘制操作期间被改变,则可以很容易地实现。 Any ideas of how to do it? 有什么想法怎么做? Should I create class derived from QPaintDevice or QPaintEngine ? 我应该创建从QPaintDeviceQPaintEngine派生的类吗?

I am using Qt 4.6.4. 我使用的是Qt 4.6.4。

Thanks. 谢谢。

Perhaps instead of having all your Paintables paint onto the same QImage, have each one paint onto a temporary blank QImage -- ie a QImage with all pixels set to RGBA=(0,0,0,0). 也许不是将所有的Paintables绘制到同一个QImage上,而是将每个绘制到一个临时的空白QImage上 - 即所有像素设置为RGBA =(0,0,0,0)的QImage。 That way, after a given Paintable's paint() method returns, you know that any pixels in the QImage that are now non-transparent must have been painted by that Paintable object. 这样,在给定的Paintable的paint()方法返回之后,您知道QImage中现在不透明的任何像素都必须由该Paintable对象绘制。 You could then update your own z-buffer like data-structure based on that information, and then drawImage() the QImage over to a separate "accumulation QImage" (assuming you also want the composited result), clear the temporary QImage again, and repeat as necessary. 然后,您可以根据该信息更新您自己的z-buffer数据结构,然后将QImage drawImage()转换为单独的“accum QImage”(假设您还需要合成结果),再次清除临时QImage,以及必要时重复。

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

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