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 {
    
    	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);
    
    }
    


  • @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);
    	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.



  • @Ruslan-F.

    I also tried this:

    void QTSemiTransparentWidget::paintEvent(QPaintEvent* event) {
    
    	QPainter mainPainter(this);
    	mainPainter.setOpacity(0.5);
    
    }
    

    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);
        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.