QVideo Widget should show the first frame after set it.



  • Hi,

    I am working with a QVideoWidget and if I start my programm a video is set in the background. But it does not show the first frame. So it looks like nothing is load. But if I move the window, I can see the already drawn first frame. But it looks like it is in the background. I really dont know whats in front. It is the same with an resizing event, I directly loose the preview frame.

    It is a mediaWidget in the mainwindow, and a QVideowidget with the mediaWidget as parent. So it seems not logic for me.

    I implemented everything normaly.

    ...
    mediaPlayerWidget = new QVideoWidget(_pMainWindow);
    mediaPlayerWidget->setParent(_pMainUi->mediawidget);

    _moviePlayer = new QMediaPlayer(this);
    _moviePlayer->setVideoOutput( mediaPlayerWidget);
    _moviePlayer->setMedia(QUrl::fromLocalFile(moviePath));
    ...
    void Video::play(state)
    {
    if(state()!=QMediaPlayer::PlayingState)
    {
    _moviePlayer->play();
    }
    }


  • Lifetime Qt Champion

    Hi,

    Why are you parenting mediaPlayerWidget like that ? That part looks a bit suspicious.



  • Hi @SGaist

    If I want to use a QVideoWidget I could not put it directly into my Qt Creator. So I put a Widget first and then I created a VideoWidget and set the Widget from the QtCreator as parent.

    Is there a better way? Just creating the videoWidget and set it to the Layout manually? I tested your way:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        videoWidgetTransform     = new QVideoWidget(this);
        videoWidgetTransform->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    
    
        videoWidgetLayout = new QVBoxLayout(this);
        videoWidgetLayout->addWidget(videoWidgetTransform);
        videoWidgetLayout->setMargin(0);
    
        _videoPlayer =  new QMediaPlayer(this);
        _videoPlayer->setVideoOutput(videoWidgetTransform);
        _videoPlayer->setMedia(QUrl::fromLocalFile("C:/Users/Public/Videos/Sample Videos/Wildlife.wmv"));
       connect(_videoPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),_videoPlayer,SLOT(play()));
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
        delete _videoPlayer;
        delete videoWidgetTransform;
        delete videoWidgetLayout;
    }
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
        Q_UNUSED(event)
        QSize s = this->size();
    
    
    
        if(this->height()*16/9>this->size().width() && this->width()*9/16<this->size().height())
        {
            s.setHeight(s.width()*9/16);
            s.setWidth(this->width());
    
        }
        else
        {
            s.setWidth(s.height()*16/9);
            s.setHeight(this->height());
        }
    
        videoWidgetTransform->setGeometry((this->width()-s.width())/2,(this->height()-s.height())/2,s.width(),s.height());
    }
    

    I still have the same problem, taht the first frame is not shown


  • Lifetime Qt Champion

    Why are you not calling the base implementation of resizeEvent ?



  • How do you mean @SGaist ?
    Is below the part of resize Event you mean?

    The widget will be erased and receive a paint event immediately after processing the resize event. No drawing need be (or should be) done inside this handler.
    

    I want to resize the videoWidget by 16:9, thats working properly.


  • Lifetime Qt Champion

    Have a call to QMainWindow::resizeEvent at the end of the method.



  • @SGaist said in QVideo Widget should show the first frame after set it.:

    Have a call to QMainWindow::resizeEvent at the end of the method.

    I dont have another method than resizeEvent(). callinf resizeEvent in resizeEvent would be a loop.
    Where exactly would you implement it?



  • @KonradMD hi, what @SGaist meant is to add QMainWindow::resizeEvent(event); which is not the same as resizeEvent(event).

    resizeEvent() calls your custom overwritten implementation, QMainWindow::resizeEvent calls the base implementation resizeEvent.



  • Unfortunaly it does not work, I still see the frame when I am resizing, but not when I finished resizing.

    Does it work on your systems? Maybe I just have a bad computer ... :/

    void MainWindow::resizeEvent(QResizeEvent *event)
    {
        Q_UNUSED(event)
        QSize s = this->size();
    
    
    
        if(this->height()*16/9>this->size().width() && this->width()*9/16<this->size().height())
        {
            s.setHeight(s.width()*9/16);
            s.setWidth(this->width());
    
        }
        else
        {
            s.setWidth(s.height()*16/9);
            s.setHeight(this->height());
        }
    
        videoWidgetTransform->setGeometry((this->width()-s.width())/2,(this->height()-s.height())/2,s.width(),s.height());
        QMainWindow::resizeEvent(event);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.