How to set transparency effects with setMask (or other)
-
Hi.
I'm trying to set transparency effects, but I can't set a real transparency, texts and graphics in the foreground are removed.
(sample images and code used below.)
I need some suggestions and help.
QPixmap px(this->size()); //Create pixmap with the same size of current widget px.fill(Qt::transparent); //Fill transparent QPainter p(&px); QBrush brush; brush.setStyle(Qt::SolidPattern); //For fill p.setBrush(brush); p.drawRoundedRect(this->rect(), 45.0, 45.0); //Draw filled rounded rectangle on pixmap this->setMask(px.mask()); //The the mask for current widget.
This code is called in the constructor of an object derived from QFrame, and then the foreground parts are drawn.
-
Hi.
I'm trying to set transparency effects, but I can't set a real transparency, texts and graphics in the foreground are removed.
(sample images and code used below.)
I need some suggestions and help.
QPixmap px(this->size()); //Create pixmap with the same size of current widget px.fill(Qt::transparent); //Fill transparent QPainter p(&px); QBrush brush; brush.setStyle(Qt::SolidPattern); //For fill p.setBrush(brush); p.drawRoundedRect(this->rect(), 45.0, 45.0); //Draw filled rounded rectangle on pixmap this->setMask(px.mask()); //The the mask for current widget.
This code is called in the constructor of an object derived from QFrame, and then the foreground parts are drawn.
-
The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.
-
The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.
@KenAppleby-0 I state that my knowledge of Qt is poor.
But in the paintEvent I can't get any result that comes close to the requested one (setMask is much closer).
What I get is a black background or no background change.Perhaps it is due to the fact that in the QWidget parent of the QFrame the background is an image.
-
The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.
@KenAppleby-0 I think I found, the main thing was to turn off these calls buried in the code recesses
setAutoFillBackground( true )
And the management of the palette and the background image needs to be reviewed.
But even if other things malfunction now, partial transparency works.
I've made some changes, the graphic effect is better and the code is substantially definitive (if it can be useful to someone else).
void Panel::paintEvent( QPaintEvent *e ) { Q_UNUSED(e); QPainter p( this ); QRect qrRect; QColor qcRectColor; qrRect = rect(); qrRect.adjust( 1, 1, -1, -1 ); qcRectColor = palette().color( QPalette::Background ); qcRectColor.setAlphaF( 0.7 ); // p.setRenderHint ( QPainter::Antialiasing ); p.setPen( palette().color( QPalette::Background ) ); p.setBrush( qcRectColor ); p.drawRoundedRect( qrRect, 45.0, 45.0); QFrame::paintEvent( e ); }
-