Skip to content
Advertisement

Qt 5.10 QGraphicsView cannot scale QGraphicsScene to fullscreen

I am experimenting with Qt 5.10 QMultimedia on Ubuntu 16.04 to play Gstreamer recorded videos. The meat of the application is shown below. I cannot seem to get the video to scale to the full Qt screen after I call showMaximized().

The small video shown in the middle of the screen is shown below ( I want it to scale to the entire window ):

enter image description here

The Code:

#include "mainwindow.h"

#include <QVideoWidget>
#include <QGraphicsView>
#include <QtMultimedia>
#include <QGraphicsVideoItem>
#include <unistd.h>
#include <QTimer>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{

    QGraphicsView * graphicsView = new QGraphicsView;
    QGraphicsScene * scene = new QGraphicsScene;
    QGraphicsVideoItem *item = new QGraphicsVideoItem;

    //graphicsView->setGeometry(0,0,640,480); // One size shows up... attempt #1
    graphicsView->setGeometry(0,0,640*2,480*2); // This does nothing to video size?

    graphicsView->setScene(scene);
    graphicsView->scene()->addItem(item);
    graphicsView->setRenderHints( QPainter::Antialiasing );

    scene->setSceneRect(scene->itemsBoundingRect());
    graphicsView->setSceneRect(scene->sceneRect());

    // Media Player
    player = new QMediaPlayer;
    player->setVideoOutput(item);
    player->setMedia(QUrl::fromLocalFile("/tmp/test.mkv"));
    player->play();

    qDebug() << "Started Playing";

    // Show full screen
    //graphicsView->showFullScreen();
    graphicsView->showMaximized();

}

Advertisement

Answer

In QGraphicsView 2 types of coordinates are handled, the first is the physical coordinate of the pixels, and another is the one that is handled with the items, that is, the coordinate system of the QGraphicsView as a widget is different from the coordinate system of the scene. For you to understand, I will use the following example: let’s say you have a camera and you are recording a movie, the real world has a coordinate system, but also the camera has another coordinate system that does not match but there is a certain transformation that relates them, example an actor approaches the camera, his physical height has not changed but the height that looks from the camera if it has changed. In the previous example, the camera is the QGraphicView, the world is the QGraphicScene, and the actors and elements are the items. So even if I buy a camera with more resolution the actor will not grow, and that’s what you’ve done. One thing that can be done is to adjust the focus of the camera so that only the actor can see, that’s the same as using fitInView(), but that’s what you should do when the scene changes in size so that it adjusts itself, so we must use the sceneRectChanged signal of QGraphicScene :

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QGraphicsView * graphicsView = new QGraphicsView;
    setCentralWidget(graphicsView);
    QGraphicsScene * scene = new QGraphicsScene(graphicsView);
    QGraphicsVideoItem *item = new QGraphicsVideoItem;

    // Media Player
    player = new QMediaPlayer;
    player->setVideoOutput(item);
    player->setMedia(QUrl::fromLocalFile("/tmp/test.mp4"));
    player->play();

    graphicsView->setScene(scene);
    graphicsView->scene()->addItem(item);
    graphicsView->setRenderHints( QPainter::Antialiasing );

    connect(scene, &QGraphicsScene::sceneRectChanged,
            [graphicsView, item](const QRectF &)
    {
        graphicsView->fitInView(item);;
    });

    showMaximized();
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement