PDA

View Full Version : Overlay widgets



joylamb
9th September 2015, 16:38
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);
}