Luckychap 68 Posting Pro in Training
#include <QtGui>

class MaskedLabel : public QLabel
{
protected:
	void resizeEvent(QResizeEvent* event)
	{
		QLabel::resizeEvent(event);
	
		QPixmap pixmap(size());
		pixmap.fill(Qt::transparent);
		QPainter::setRedirected(this, &pixmap);
		QPaintEvent pe(rect());
		paintEvent(&pe);
		QPainter::restoreRedirected(this);
		setMask(pixmap.mask());
	}
};

int main(int argc, char* argv[])
{
	QApplication a(argc, argv);
	QLabel* label = new MaskedLabel();
	label->setText("Qt Centre!");
	QFont font = label->font();
	font.setPointSize(72);
	label->setFont(font);
	label->show();
	return a.exec();
}

Hi,

I tired this approach to create Picture In Picture(PIP) window on QWebView widget. And my goal is to create hole on QwebView so that video playing in the background is visible through PIP window.

When I first create this Masked Label its showing me the video playing in background, all is good at this point. But when I try to change the height/width of the Masked Label using setGeometry() method of QLabel, its is not showing me transparent window. But when I only(not changing the height/widht) move it around on the screen using same setGeometry(), it just work fine.

Any idea OR approach to achieve my goals.

Thanks.

Dani AI

Generated

For : the symptom you describe (works when first shown, fails after a geometry resize) usually comes down to two separate issues — how you shape the overlay widget, and what actually draws the video under the hole.

A reliable approach is to make the PIP a top-level, frameless, translucent window and explicitly shape it rather than relying on redirected painting hacks. On the PIP widget call something like:

setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground);

Then update the shape every time the widget size changes. Two safe ways to update the visible area:

  • Create a region that subtracts the hole and call setMask() with that region.
  • Render the widget into a QPixmap and build a mask from the transparent pixels.

Example patterns (keep these in a updateMask() called from resizeEvent):

QRegion r(rect());
r = r.subtracted(QRegion(holeRect));
setMask(r);
QPixmap pm(size());
pm.fill(Qt::transparent);
render(&pm);                      // QWidget::render is simpler and safer than setRedirected
setMask(pm.createMaskFromColor(Qt::transparent));

Important notes and troubleshooting:

  • Use updateMask() from resizeEvent() or immediately after any setGeometry() to ensure the mask matches the new size.
  • setMask uses a 1-bit shape; use WA_TranslucentBackground if you need alpha blending instead of a hard shape.
  • If the underlying video is drawn by a native plugin or overlay (native child window), stacking order can prevent a hole from showing the video. In that case render the video into a composited surface (for example use Qt multimedia/QGraphicsVideoItem) or avoid native-window plugins.

See Qt docs for the APIs mentioned: QWidget::setMask, QWidget::render, and QGraphicsVideoItem for composited video alternatives.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.