Qt 5.10 QGraphicsView cannot scale QGraphicsScene to fullscreen

…衆ロ難τιáo~ 提交于 2019-12-02 07:24:36

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!