QT widgets Video as background?

919 Views Asked by At

I have to develop a UI for my new program and I want to play an animated wallpaper (mp4) as my background. Do somebody know how I can to that?

I tried to solve it with the qvideowidget and qmediaplayer but if I do it this way I cannot insert buttons or the like over the video so that the video would be my background.

Subsequently I converted my video in single images and tried to refresh a pixmap of a qlabel every iteration but my program crashed so this doesn't work neither.

1

There are 1 best solutions below

0
CoolStack On

With OpenCV, you can gain what you want.

Start timer and get pixmap whenever time clocks.

Re-implement paintEvent() function of QWidget.

For example...

VideoCapture m_capture ;
m_capture.open(videoFilePath.toStdString()) ;


void QWidget::playMovie()
{
    m_timer.start(30,this) ;
    m_StartTime = QTime::currentTime() ;
}

QWidget::paintEvent(QPaintEvent* evemt)
{
    QPainter painter(this) ;
    painter.drawPixmap( 0, 0, m_snapshot.scaled(width(),height()) ;
}

QWidget::timerEvent(QTimerEvent* event)
{
    if( event->timerId() != m_timer.timerId() ) return ;
    int elapsed = m_StartTime.msecsTo(QTime::currentTime()) ;
    if( m_capture.isOpened() )
    {
        m_capture.set(CV_CAP_PROP_POS_MSEC,elapsed ) ;
        cv::Mat img ;
        m_capture.read(img) ;
        cvtColor(img , img ,CV_BGR2RGB);
        QImage dest((const uchar *) img.data, img.cols, img.rows, img.step, 
            QImage::Format_RGB888);
        dest.bits();
        m_snapshot = QPixmap::fromImage(dest) ;
        repaint() ;
    }
}