Reputation: 2696
I'm trying to draw a rectangle with QPainter. This is going well, but I noticed that if I set a pen and a brush on the painter with the same color, the alpha factor of the pen and the brush are added somehow. This causes the line drawn by the pen to apear less transparent. Is there anyway to prevent this addition of the colors? I would expect that if the pen and the brush are the same color the line drawn by the pen would be "invisible". Below the part of my code to draw the rectangle and an image of the output.
QBrush newbrush = QBrush(painter->brush());
newbrush.setColor(QColor(0, 0, 255, 125));
newbrush.setStyle(Qt::SolidPattern);
painter->setBrush(newbrush);
QPen newpen = QPen(painter->pen());
newpen.setColor(QColor(0, 0, 255, 125));
newpen.setWidth(10);
painter->setPen(newpen);
painter->drawRect(QRect(QPoint(100, 50), QPoint(500, 500)));
Upvotes: 1
Views: 1882
Reputation: 49289
You will need to change the painter composition mode.
QPainter::CompositionMode_SourceOver
This is the default mode. The alpha of the source is used to blend the pixel on top of the destination.
So check out the rest and see which one does it for you.
As mentioned in the comment below, you will need to draw the fill and outline in a sequence in order for the compositing mode to kick in, as it seems like it is ignored then when you are drawing both in a single call. Drawing in sequence is not necessarily a performance overhead, just an extra line and some reordering in the code.
Upvotes: 2
Reputation: 321
When you draw an object with alpha over another object with alpha, you get a "less transparent intersection area", which is represented by a sum of alpha values of these two semi-transparent objects (sorry about my bad english, I'm brazilian).
In your case the border was drawn with alpha 125 over the rectangle also with alpha 125, which gives the border the impression of alpha 250 (125+125).
You can remove the border using painter->setPen( Qt::NoPen );
or draw it separated and outside the rectangle (using painter->fillRect()
for the rectangle and painter->drawRect()
for the border).
Hope it helps.
Upvotes: 5