Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Fitting QGraphicsVideoItem and QGraphicsScene to QGraphicsView



  • Hi, I'm trying to play video on UI like that:

    in mainwindow.cpp:

    QGraphicsScene *scene = new QGraphicsScene(this);
    
    // drag&dropped QGraphicsView on the UI named graphic_view
    ui->graphic_view->setScene(scene);
    
    QGraphicsVideoItem *video_item = new QGraphicsVideoItem();
    scene->addItem(video_item);
    
    QMediaPlayer *media_player = new QMediaPlayer(this);
    media_player ->setVideoOutput(video_item );
    media_player ->setMedia(QUrl("path-to-video"));
    media_player ->play();
    
    

    Video is playing but i couldn't fit video to its parent. Any suggestions or sample to how to do that in my case?

    A gif is here:
    0_1541942323969_aaa.gif



  • @Asperamanca I tried to use but now with a resizeEvent, today i woke up and get on pc then just tried with resizeEvent. So everything ok now :)

    Here is how i did:

    //in constructor
    QGraphicsScene *scene = new QGraphicsScene(this);
    scene ->setSceneRect(10,10,500,350);
    ui->graphicsView_2->setScene(scene);
    ui->graphicsView_2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    
    QGraphicsVideoItem *vi = new QGraphicsVideoItem();
    player = new QMediaPlayer(this);
    player ->setVideoOutput(vi);
    player ->setMedia(QUrl("some rtsp address or local video source path"));
    player ->play();
    
    
    //resize event, you should declare in header protected section
    void MainWindow::resizeEvent(QResizeEvent *)
    {
        QRectF testRect = scene->itemsBoundingRect();
    
        // if i don't multiply with between 0.9 and 0.95 some white spaces raises on the right and bottom
        testRect.setWidth(testRect.width()*0.95);
        testRect.setHeight(testRect.height()*0.95);
        ui->graphicsView_2->fitInView(testRect, Qt::IgnoreAspectRatio);
        ui->graphicsView_2->centerOn(0,0);
    
        ui->graphicsView->raise(); //if mainwindow has a layout(grid or some otherelse)
        // graphicsView is an another layer that i want to paint on second graphicView layer.
    
        QSize test(ui->graphicsView_2->size());
        qDebug() << "grapView2 Sze: " << test;
    }
    

    Video source is playing and it fits itself to entire QGraphicsView, just like i want.

    Thanks for any help and suggestions.


  • Lifetime Qt Champion

    Hi,

    Before diving into that, are you showing any else on that QGraphicsView ? If not, why not use QVideoWidget ?



  • @SGaist I'm trying to draw other qraphicView objects on it, i tried to use QVideoWidget but it's invalidate the transparent layer(to see topic:).

    So i think i need to use other option to show video: QGraphicsView, QGraphicsScene and QGraphicsVideoItem together. I tested to draw another qgraphicsView on it, it keeps its transparent background, so i think i need to do this way to what i am trying to do.



  • any possibility?


  • Lifetime Qt Champion

    IIRC, videos might be shown on a layer above the widgets therefore drawing on top of it might be more complicated. Did you consider using QtQuick ? It could prove simpler for your use case.



  • @SGaist
    0_1542172248606_Capture.PNG

    As you see, i can play an animated QGraphicsScene on a rtsp video like that, the problem is, i can not fit the video into entire parent widget. First message shows how i play rtsp video Mr. @SGaist



  • I'm trying to make a lot of these types examples to achieve and learn Qt's skill. Even what i'm trying to do is wrong i am learning how to fix it and what is the most efficient way. So i want to thank this forum for all replies and helps. The next step is solving this problem, i tried all sizing tricks but never overcome :/



  • Have you tried QGraphicsView::fitInView?



  • @Asperamanca I tried to use but now with a resizeEvent, today i woke up and get on pc then just tried with resizeEvent. So everything ok now :)

    Here is how i did:

    //in constructor
    QGraphicsScene *scene = new QGraphicsScene(this);
    scene ->setSceneRect(10,10,500,350);
    ui->graphicsView_2->setScene(scene);
    ui->graphicsView_2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    
    QGraphicsVideoItem *vi = new QGraphicsVideoItem();
    player = new QMediaPlayer(this);
    player ->setVideoOutput(vi);
    player ->setMedia(QUrl("some rtsp address or local video source path"));
    player ->play();
    
    
    //resize event, you should declare in header protected section
    void MainWindow::resizeEvent(QResizeEvent *)
    {
        QRectF testRect = scene->itemsBoundingRect();
    
        // if i don't multiply with between 0.9 and 0.95 some white spaces raises on the right and bottom
        testRect.setWidth(testRect.width()*0.95);
        testRect.setHeight(testRect.height()*0.95);
        ui->graphicsView_2->fitInView(testRect, Qt::IgnoreAspectRatio);
        ui->graphicsView_2->centerOn(0,0);
    
        ui->graphicsView->raise(); //if mainwindow has a layout(grid or some otherelse)
        // graphicsView is an another layer that i want to paint on second graphicView layer.
    
        QSize test(ui->graphicsView_2->size());
        qDebug() << "grapView2 Sze: " << test;
    }
    

    Video source is playing and it fits itself to entire QGraphicsView, just like i want.

    Thanks for any help and suggestions.


Log in to reply