Displaying a QWidget with Transparent Background over a QMediaPlayer

  • 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 {
    	explicit QTSemiTransparentWidget(QWidget* parent = NULL);
    public slots:
    private slots :
    	virtual void prepareUI();

    And here is the implementation:

    #include "QTSemiTransparentWidget.h"
    QTSemiTransparentWidget::QTSemiTransparentWidget(QWidget* parent) : QWidget(parent) {
    QTSemiTransparentWidget::~QTSemiTransparentWidget() {
    void QTSemiTransparentWidget::prepareUI() {
    	this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    	this->setAttribute(Qt::WA_TranslucentBackground, true);

  • @tmason202 watch at QGraphicsOpacityEffect or reimplement paintEvent and use QPainter::setOpacity

  • Hello!

    This is what I tried:

    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    	transparencyEffect = new QGraphicsOpacityEffect(this);

    In the "prepareUI" function. But the transparency is still not applied to the class object.

    What am I doing wrong?

    Thank you.

  • @Ruslan-F.

    I also tried this:

    void QTSemiTransparentWidget::paintEvent(QPaintEvent* event) {
    	QPainter mainPainter(this);

    But still no transparency.

  • 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.

  • @tmason202 , try this one

    void QTSemiTransparentWidget::paintEvent(QPaintEvent* event) {
        QPainter painter(this);
        QStyleOption opt;
        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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.