How to get 2 layers using Composition Modes in QPainter?
Solved
General and Desktop
-
Hi,
I would like to draw ( using QPainter and composition modes ) on the one widget 2 layers:
- one, which is top layer - red color
- one, which is bottom layer - blue color with black text in some position
I draw "transparent" ellipse, so I see blue layer.
I can do 2 layers: red and blue, transparent ellipse using only composition modes, but I can't get text to blue layer. There is not shown.
My code:
void someWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing); painter.setPen(Qt::NoPen); QPointF globalpoint = this->mapFromGlobal(QCursor::pos()); painter.setCompositionMode(QPainter::CompositionMode_Source); painter.fillRect(0, 0, width(), height(), Qt::red); painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); painter.setBrush(QColor(0,0,0,0)); painter.drawEllipse(globalpoint, 200,200); painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); painter.fillRect(0, 0, width(), height(), Qt::blue); painter.setPen(Qt::black); painter.drawText(100, 100, "Text"); }
EDIT:
I found solution:
I have to change an order:painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); painter.setPen(Qt::black); painter.drawText(100, 100, "Text"); painter.fillRect(0, 0, width(), height(), Qt::blue);