简体   繁体   中英

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:

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

Instances of this class are being painted on a single 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).

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. Any ideas of how to do it? Should I create class derived from QPaintDevice or QPaintEngine ?

I am using 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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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