Put transparent QWidget on top of QMediaView in QT5 on Ubuntu

后端 未结 3 1281
渐次进展
渐次进展 2021-01-02 05:57

Goal

I want the background of my QT5 based GUI to be a video file that is playing. I also want to be able to style my GUI components with transparen

3条回答
  •  星月不相逢
    2021-01-02 06:20

    I know this is an old question and you managed to solve your problem by converting parts of your application to QML/QtQuick 2.2 but I ended up bumping into it by google search and someone else with the same problem might also find this. I found a solution that worked for me and has been tested on Windows with QT 5.3.

    What I did was use a QGraphicsView to display the video. Here's my code (playerScreen is the QGraphicsView):

    QGraphicsVideoItem *item = new QGraphicsVideoItem;
    item->setSize(ui->playerScreen->size());
    player.reset(new QMediaPlayer());
    player->setVideoOutput(item);
    QGraphicsScene *scene = new QGraphicsScene(0, 0, ui->playerScreen->size().width(), ui->playerScreen->size().height());
    ui->playerScreen->setScene(scene);
    ui->playerScreen->scene()->addItem(item);
    

    I disabled the scroll bars on playerScreen otherwise there would be horizontal and vertical scrollbars.

    I have a QWidget on top of the playerScreen in which I draw with QPainter. That QWidget is on top of the graphics view.

    Then, when I play the video, I call ui->playerScreen-show(). I do this only on play because I have another screen on top (for another stuff related to my project) and I need to call show/hide when the video is being used/not used :)

    Let me know if you need more information on my code.

提交回复
热议问题