1

I am trying to overlay a few buttons over my video player.

I have added a new class called overlay.cpp that subclassed a QWidget for the overlay purpose.

What I did in my code is to overlay button onto the video. In my centralWidget I have added a verticalLayout and morph it into a QWidget. The video was added into this verticalLayout. Upon program is running, the video is playing well. However, what's not working is the overlay of the button. The background doesn't seem to appear transparent even though it was set. I am not sure what is causing it to not appear transparent.

My code is as follows:

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[]){

  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  return a.exec();

}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent), 
  ui(new Ui::MainWindow){
  ui->setupUI(this);
  initializeVideo();
  initializeButton();
}

MainWindow::~MainWindow(){

  delete ui;
}

void MainWindow::initializeVideo(){

  QVideoWidget *v_widget = new QVideoWidget;
  QMediaPlayer *m_player = new QMediaPlayer;

  m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
  m_player->setVideoOutput(v_widget);

  ui->verticalLayout->addWidget(v_widget);

  m_player->player();
  v_widget->show();

}

void MainWindow::initializeButton(){

  QFrame *b_frame = new QFrame;
  QGridLayout *grid = new QGridLayout;
  b_frame->setLayout(grid);

  b_frame->setAttribute(Qt::WA_TranslucentBackground, true);

  QPushButton *buttonStop = new QPushButton;
  buttonStop->setText("STOP");
  grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);

  overlay *overlay_1 = new overlay;
  QGridLayout *gridLayout = new QGridLayout;
  gridLayout->addWidget(b_frame);
  overlay_1->setLayout(gridLayout);

  overlay_1->setParent(ui->verticalWidget);
  overlay_1->show();

  b_frame->show();

}

overlay.cpp

#include "overlay.h"
overlay::overlay(QWidget *parent): QWidget(parent){

  this->setAttribute(Qt::WA_TranslucentBackground, true);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
joy lamb
  • 59
  • 2
  • 9
  • 1
    See the following link for an answer: http://stackoverflow.com/questions/25466030/make-qwidget-transparent – Gombat Sep 08 '15 at 09:04

1 Answers1

3

Move declaration of QVideoWidget *v_widget and QMediaPlayer *m_player to mainwindow.h like this:

private:
    Ui::MainWindow *ui;
    QVideoWidget *v_widget;
    QMediaPlayer *m_player;

In mainwindow.cpp:

void MainWindow::initializeVideo()
{
    v_widget = new QVideoWidget(this);
    m_player = new QMediaPlayer(this);

    m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
    m_player->setVideoOutput(v_widget);

    ui->verticalLayout->addWidget(v_widget);

    m_player->play();
}

void MainWindow::initializeButton()
{
    QGridLayout *grid = new QGridLayout(v_widget);

    QPushButton *buttonStop = new QPushButton(this);
    buttonStop->setText("STOP");
    grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);
}

This will add "STOP" button on top of QVideoWidget.

Specify widget's parent when crating it. new QVideoWidget(this) will create new QVideoWidget as child of current MainWindow widget. If you are creating child of already visible widget you do not need to call show() on it.

  • I've tried your proposed soultion. It does not work. I did not see the button overlaying the button. The button did not appear. – joy lamb Sep 08 '15 at 14:18
  • @joylamb: to show the button you can call `button.raise()` or `videowidget.lower()` – aviit Oct 01 '18 at 16:59