Displaying a QWidget with Transparent Background over a QMediaPlayer
-
wrote on 12 Apr 2015, 22:15 last edited by
Hello,
I am trying to get a transparent QWidget to display over a QMediaPlayer object.
The goal is to have a semi-transparent QWidget that displays playback, volume, etc. controls over a QMediaPlayer widget.
As it stands now the control displays but there is no transparency over the QMediaPlayer object. What do I need to do to make that happen?
Thank you.
Here is the following code for the QWidget I am trying to make semi-transparent:
class QTSemiTransparentWidget : public QWidget { Q_OBJECT public: explicit QTSemiTransparentWidget(QWidget* parent = NULL); ~QTSemiTransparentWidget(); public slots: signals: protected: private: private slots : virtual void prepareUI(); };
And here is the implementation:
#include "QTSemiTransparentWidget.h" QTSemiTransparentWidget::QTSemiTransparentWidget(QWidget* parent) : QWidget(parent) { prepareUI(); } QTSemiTransparentWidget::~QTSemiTransparentWidget() { } void QTSemiTransparentWidget::prepareUI() { this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); this->setAttribute(Qt::WA_NoSystemBackground); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAttribute(Qt::WA_PaintOnScreen); this->setAttribute(Qt::WA_TransparentForMouseEvents); this->setWindowOpacity(0.5); }
-
Hello,
I am trying to get a transparent QWidget to display over a QMediaPlayer object.
The goal is to have a semi-transparent QWidget that displays playback, volume, etc. controls over a QMediaPlayer widget.
As it stands now the control displays but there is no transparency over the QMediaPlayer object. What do I need to do to make that happen?
Thank you.
Here is the following code for the QWidget I am trying to make semi-transparent:
class QTSemiTransparentWidget : public QWidget { Q_OBJECT public: explicit QTSemiTransparentWidget(QWidget* parent = NULL); ~QTSemiTransparentWidget(); public slots: signals: protected: private: private slots : virtual void prepareUI(); };
And here is the implementation:
#include "QTSemiTransparentWidget.h" QTSemiTransparentWidget::QTSemiTransparentWidget(QWidget* parent) : QWidget(parent) { prepareUI(); } QTSemiTransparentWidget::~QTSemiTransparentWidget() { } void QTSemiTransparentWidget::prepareUI() { this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); this->setAttribute(Qt::WA_NoSystemBackground); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAttribute(Qt::WA_PaintOnScreen); this->setAttribute(Qt::WA_TransparentForMouseEvents); this->setWindowOpacity(0.5); }
wrote on 12 Apr 2015, 23:25 last edited by@tmason202 watch at QGraphicsOpacityEffect or reimplement paintEvent and use QPainter::setOpacity
-
wrote on 13 Apr 2015, 00:29 last edited by
Hello!
This is what I tried:
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); transparencyEffect = new QGraphicsOpacityEffect(this); transparencyEffect->setOpacity(0.5); this->setGraphicsEffect(transparencyEffect);
In the "prepareUI" function. But the transparency is still not applied to the class object.
What am I doing wrong?
Thank you.
-
@tmason202 watch at QGraphicsOpacityEffect or reimplement paintEvent and use QPainter::setOpacity
-
wrote on 13 Apr 2015, 10:02 last edited by
Hello,
So, I figured this out; what I ended up doing was creating a QGraphicsView and then have the QMediaPlayer display it's output to a QGraphicsVideoItem which was then attached to the scene.
I ended up getting transparency as I needed but it cost me a little bit of performance. I will be researching what I need to do to bump up performance.
Thank you for the help.
-
I also tried this:
void QTSemiTransparentWidget::paintEvent(QPaintEvent* event) { QPainter mainPainter(this); mainPainter.setOpacity(0.5); }
But still no transparency.
wrote on 15 Apr 2015, 09:16 last edited by Ruslan F.@tmason202 , try this one
void QTSemiTransparentWidget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.setOpacity(0.5); QStyleOption opt; opt.initFrom(this); QStyle* pStyle = QApplication::style(); pStyle->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); }
at least you should draw something in paint event not to just set an opacity.
6/6