Solved 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:
-
@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.
-
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?
-
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.
-
-
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.