Put transparent QWidget on top of QMediaView in QT5 on Ubuntu

后端 未结 3 1252
渐次进展
渐次进展 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:06

    Disclaimer: I have not tried this solution. It works on Qt 4.8 to put transparant widgets over anything that is OpenGL rendered. I am just assuming the media play is using hardware acceleration.

    The black pixels are Qt's backbuffer where software rendering is performed. The video bypasses this backbuffer, and when a 'regular' qt widget is drawn, it draws on top of the backbuffer which is still in it's initial state (black).

    You could make the button (or a widget containing the button) a separate, transparant window.

    setWindowFlags(Qt::FramelessWindowHint | Qt::SplashScreen);
    setAttribute(Qt::WA_TranslucentBackground);
    

    You would need to position it manually though, so it follows your window. This means the button needs to install an eventfilter on the window, and listen to any move or resize events. It then recalculates its own position, relative to the window, and updates it position.

    0 讨论(0)
  • 2021-01-02 06:12

    qwidget4.1

    void setAutoFillBackground(bool enabled) //you have that method, try it.

    Always search in the forum before, cause there are a lot of answers. :) not that i mind, its faster for you.

    A qt widget with fully transparent background

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题