Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fitting QGraphicsVideoItem and QGraphicsScene to QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

Fitting QGraphicsVideoItem and QGraphicsScene to QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R_IrudezuR Offline
    R_IrudezuR Offline
    R_Irudezu
    wrote on last edited by R_Irudezu
    #1

    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

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • A Asperamanca

      Have you tried QGraphicsView::fitInView?

      R_IrudezuR Offline
      R_IrudezuR Offline
      R_Irudezu
      wrote on last edited by R_Irudezu
      #9

      @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.

      Keizoku wa chikaranari.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        R_IrudezuR 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          R_IrudezuR Offline
          R_IrudezuR Offline
          R_Irudezu
          wrote on last edited by
          #3

          @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.

          Keizoku wa chikaranari.

          1 Reply Last reply
          0
          • R_IrudezuR Offline
            R_IrudezuR Offline
            R_Irudezu
            wrote on last edited by
            #4

            any possibility?

            Keizoku wa chikaranari.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R_IrudezuR 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                R_IrudezuR Offline
                R_IrudezuR Offline
                R_Irudezu
                wrote on last edited by R_Irudezu
                #6

                @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

                Keizoku wa chikaranari.

                1 Reply Last reply
                0
                • R_IrudezuR Offline
                  R_IrudezuR Offline
                  R_Irudezu
                  wrote on last edited by R_Irudezu
                  #7

                  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 :/

                  Keizoku wa chikaranari.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #8

                    Have you tried QGraphicsView::fitInView?

                    R_IrudezuR 1 Reply Last reply
                    2
                    • A Asperamanca

                      Have you tried QGraphicsView::fitInView?

                      R_IrudezuR Offline
                      R_IrudezuR Offline
                      R_Irudezu
                      wrote on last edited by R_Irudezu
                      #9

                      @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.

                      Keizoku wa chikaranari.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved