Overlay widget for Phonon VideoWidget

From Qt Wiki
Revision as of 10:57, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


If you need put any widget over VideoWidget with transparent background, you can't do it so:

<br /> m_video = new Phonon::VideoWidget;<br /> ..<br /> QWebView *view = new QWebView(mVideo);<br /> view-&gt;setHtml(&quot;&lt;div style=quot;color:#FFFFFF;font-size:36px;quot;&gt;Hello Qt!&lt;/div&amp;gt;&quot;);

QPalette palette = view-&gt;page()<s>&gt;palette();<br /> palette.setBrush(QPalette::Base, Qt::transparent);<br /> view</s>&gt;setPalette(palette);

view-&gt;setAttribute(Qt::WA_OpaquePaintEvent, false);

m_video-&gt;show();<br /> m_video-&gt;resize(640, 480);

view-&gt;setFixedSize(m_video-&gt;width(), m_video-&gt;height());<br />

You got black background for your widget, It happens because phonon backend might use XRender for video rendering.

Bellow example of workaround, which can help you:

This is subclass of Phonon::VideoWidget with overrided events methods.

class OverlayedVideoWidget : public Phonon::VideoWidget<br />{<br />public:<br /> OverlayedVideoWidget(QWidget *parent = 0) :<br /> Phonon::VideoWidget(parent),<br /> m_overlayWidget(0)<br /> {<br /> }

void setOverlayWidget(QWidget *widget)<br /> {<br /> m_overlayWidget = widget;<br /> }

protected:<br /> void moveEvent(QMoveEvent *event)<br /> {<br /> if (m_overlayWidget)<br /> m_overlayWidget-&gt;move(event-&gt;pos());<br /> }

void resizeEvent(QResizeEvent '''event)<br /> {<br /> if (m_overlayWidget)<br /> m_overlayWidget-&gt;resize(event-&gt;size());<br /> }
<br /> void hideEvent(QHideEvent''')<br /> {<br /> if (m_overlayWidget)<br /> m_overlayWidget-&gt;hide();<br /> }

void showEvent(QShowEvent ''')<br /> {<br /> if (m_overlayWidget)<br /> m_overlayWidget-&gt;show();<br /> }
<br /> void closeEvent(QCloseEvent''')<br /> {<br /> if (m_overlayWidget)<br /> m_overlayWidget-&gt;close();<br /> }

private:<br /> QWidget *m_overlayWidget;<br />};

Usage example:

m_video = new OverlayedVideoWidget;

QWebView *view = new QWebView();<br />view-&gt;setHtml(&quot;&lt;div style=quot;color:#FFFFFF;font-size:36px;quot;&gt;Hello Qt!&lt;/div&amp;gt;&quot;);<br />QPalette palette = view-&gt;page()<s>&gt;palette();<br />palette.setBrush(QPalette::Base, Qt::transparent);
<br />view</s>&gt;setPalette(palette);<br />view-&gt;page()<s>&gt;setPalette(palette);<br />view</s>&gt;setAttribute(Qt::WA_OpaquePaintEvent, false);<br />view-&gt;setAttribute(Qt::WA_TranslucentBackground, true);<br />view-&gt;setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);

m_video-&gt;setOverlayWidget(view);<br />m_video-&gt;show();<br />m_video-&gt;resize(640, 480);