3

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.

Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36

1 Answers1

3

Perhaps instead of having all your Paintables paint onto the same QImage, have each one paint onto a temporary blank QImage -- i.e. 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.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • This is the first idea that has came to me too. But I guess such solution would give a huge overhead. For high resolution result image analysing each temporary image might take much time. But if QPainter or QImage could provide just a little bit more information about painting with/on them, there would be no overhead. – Oleg Andriyanov Mar 13 '14 at 07:40
  • If you can assign each Paintable its own unique color, and guarantee that it will only paint in that one color, then you could have them all paint to the same QImage and use a QColor->Paintable map at the end to get your results from the pixels... dunno if that's a reasonable restriction though. – Jeremy Friesner Mar 13 '14 at 15:34
  • ... also if you did need multiple colors, you could do two passes; one for single-color-per-Paintable "z-buffer painting" (as described in the previous comment) and then a second pass to a second QImage with the actual colors enabled. – Jeremy Friesner Mar 13 '14 at 16:35