QPixmap with transparent background
-
I have a widget whose ctor contains:
setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_NoSystemBackground); setAttribute(Qt::WA_WState_ExplicitShowHide);
it has a null paintEvent handler
void EditStars::paintEvent([[maybe_unused]] QPaintEvent* event) { event.ignore(); }
and the mouseMoveEvent and a few other places do this:
drawOnPixmap(); imageView->setOverlayPixmap(pixmap); imageView->update();
The drawOnPixmap() code does:
QRect rcClient{ rect() }; size_t width = rcClient.width(), height = rcClient.height(); QPainter painter(&pixmap); //QPalette palette{ QGuiApplication::palette() }; QBrush brush{ Qt::transparent }; painter.setBrush(brush); painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.eraseRect(rcClient); painter.fillRect(rcClient, brush);
the paintEvent handler of imageView does most of its other works then does:
painter.setBackgroundMode(Qt::TransparentMode); painter.drawPixmap(0, 0, *pOverlayPixmap);
but the overlay pixmap isn't displayed with a transparent background so I can't see through it to the underlying widget :(
How to get this to work?
Thanks
D. -
I'm not building the pixmap from a png, it's all done with QPainter. - that may make all the difference.
I am doing this on Windows
D.
-
I even tried moving the pixmap from being a class instance variable to a local variable
in drawOnPixmap, omitting the eraseRect() call and adding the callsimageView->setOverlayPixmap(pixmap); imageView->update();
to the end of mf.
but that didn't work either :(
-
Hi
what type is imageView. ?
Some widgets like QStacked and QScrollArea have a background of their own. -
Hi
what type is imageView. ?
Some widgets like QStacked and QScrollArea have a background of their own.@mrjj It is a direct sub-class of QWidget. In the paint mf of ImageView, I'm just overlaying the pixmap that was set by setOverlayPixmap ...
I tried without the use of the overlay pixmap - instead using the paint mf of the overlaying widget to draw over the underlying image but had the same problem :(
David -
@mrjj It is a direct sub-class of QWidget. In the paint mf of ImageView, I'm just overlaying the pixmap that was set by setOverlayPixmap ...
I tried without the use of the overlay pixmap - instead using the paint mf of the overlaying widget to draw over the underlying image but had the same problem :(
David -
I'm not building the pixmap from a png, it's all done with QPainter. - that may make all the difference.
I am doing this on Windows
D.
-
@Perdrix said in QPixmap with transparent background:
painter.fillRect(rcClient, brush);
No - see code posted above - I used painter.fillRect using a transparent brush
@Perdrix Perhaps filling a rect with transparent brush means painting a transparent rect onto the canvas, which actually is a no-op.
-
The default mode for painter is to blend the painted shape with existing pixels using the alpha channel, so as @candy76041820 said - filling a rectangle with transparency does nothing.
To replace the contents of that rectangle instead of blending you have to change composition mode to
QPainter::CompositionMode_Source
, or, simpler, as @mrjj suggested usefill()
method of QPixmap.