Hi!
I am developping my own QGraphicsItem that should paint a partly transparent QImage. This is easy, it can just be realised via
void GraphicsHairstyleItem
::paint(QPainter *painter,
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawImage(m_imgrect, m_img_warped);
}
void GraphicsHairstyleItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawImage(m_imgrect, m_img_warped);
}
To copy to clipboard, switch view to plain text mode
OK. But now I qould like to be able to realise something like "erasing", or like the rubber-tool in Photoshop. Therefor I inted to have a second QImage called the "cutmask" that is transparent everywhere except for parts of the image that should be erased.
Following the documentation on Compositing modes, if the image to be shown is the destination and the cutmask acts as the source (but I'm not a 100% sure about this), then QPainter::CompositionMode_DestinationOut should do the right thing.
This means that only portions of the image where the cutmask is transparent are shown.
So my code looks like
void GraphicsHairstyleItem
::paint(QPainter *painter,
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawImage(m_imgrect, m_img_warped);
painter
->setCompositionMode
(QPainter::CompositionMode_DestinationOut);
painter->drawImage(m_imgrect, m_img_cutmask);
}
void GraphicsHairstyleItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawImage(m_imgrect, m_img_warped);
painter->setCompositionMode(QPainter::CompositionMode_DestinationOut);
painter->drawImage(m_imgrect, m_img_cutmask);
}
To copy to clipboard, switch view to plain text mode
see the attachments for my cutmask image "cut.png".
But in this realisation (and also in many others), the white spot is just drawn on top of the image, instead of erasing it out there.
Thank you very much for your help in advance,
Olli
Bookmarks