Overlay widget for Phonon VideoWidget: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:Snippets]]
[[Category:HowTo]]
[[Category:Snippets]]


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


<code><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;);
<code>
m_video = new Phonon::VideoWidget;
…..
QWebView *view = new QWebView(mVideo);
view->setHtml("<div style=quot;color:#FFFFFF;font-size:36px;quot;>Hello Qt!</div>");


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


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


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


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


You got black background for your widget, It happens because phonon backend might use ''XRender'' for video rendering.
You got black background for your widget, It happens because phonon backend might use ''XRender'' for video rendering.
Line 19: Line 28:
This is subclass of ''Phonon::VideoWidget'' with overrided events methods.
This is subclass of ''Phonon::VideoWidget'' with overrided events methods.


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


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


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


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


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


private:<br /> QWidget *m_overlayWidget;<br />};</code>
void showEvent(QShowEvent ''')
{
if (m_overlayWidget)
m_overlayWidget->show();
}


Usage example:<br /><code>m_video = new OverlayedVideoWidget;
void closeEvent(QCloseEvent''')
{
if (m_overlayWidget)
m_overlayWidget->close();
}


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);
private:
<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);
QWidget *m_overlayWidget;
};</code>


m_video-&gt;setOverlayWidget(view);<br />m_video-&gt;show();<br />m_video-&gt;resize(640, 480);</code>
Usage example:
<code>m_video = new OverlayedVideoWidget;
 
QWebView *view = new QWebView();
view->setHtml("<div style=quot;color:#FFFFFF;font-size:36px;quot;>Hello Qt!</div>");
QPalette palette = view->page()->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
 
view->setPalette(palette);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);
view->setAttribute(Qt::WA_TranslucentBackground, true);
view->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
 
m_video->setOverlayWidget(view);
m_video->show();
m_video->resize(640, 480);</code>

Revision as of 11:16, 25 February 2015


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

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

QPalette palette = view->page()->palette();
 palette.setBrush(QPalette::Base, Qt::transparent);
 view->setPalette(palette);

view->setAttribute(Qt::WA_OpaquePaintEvent, false);

m_video->show();
 m_video->resize(640, 480);

view->setFixedSize(m_video->width(), m_video->height());

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
{
public:
 OverlayedVideoWidget(QWidget *parent = 0) :
 Phonon::VideoWidget(parent),
 m_overlayWidget(0)
 {
 }

void setOverlayWidget(QWidget *widget)
 {
 m_overlayWidget = widget;
 }

protected:
 void moveEvent(QMoveEvent *event)
 {
 if (m_overlayWidget)
 m_overlayWidget->move(event->pos());
 }

void resizeEvent(QResizeEvent '''event)
 {
 if (m_overlayWidget)
 m_overlayWidget->resize(event->size());
 }

 void hideEvent(QHideEvent''')
 {
 if (m_overlayWidget)
 m_overlayWidget->hide();
 }

void showEvent(QShowEvent ''')
 {
 if (m_overlayWidget)
 m_overlayWidget->show();
 }

 void closeEvent(QCloseEvent''')
 {
 if (m_overlayWidget)
 m_overlayWidget->close();
 }

private:
 QWidget *m_overlayWidget;
};

Usage example:

m_video = new OverlayedVideoWidget;

QWebView *view = new QWebView();
view->setHtml("<div style=quot;color:#FFFFFF;font-size:36px;quot;>Hello Qt!</div>");
QPalette palette = view->page()->palette();
palette.setBrush(QPalette::Base, Qt::transparent);

view->setPalette(palette);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);
view->setAttribute(Qt::WA_TranslucentBackground, true);
view->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);

m_video->setOverlayWidget(view);
m_video->show();
m_video->resize(640, 480);