Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsScene position and size



  • Hi,
    I have the following ui.
    f626f78c-bdef-490b-990f-b1261a335139-image.png

    The code is as follows:

        m_scene = new QGraphicsScene(this);
        m_chart = new QChart();
        ui->graphicsView->setScene(m_scene);
        m_scene->addItem(m_chart);
    

    I have reimplemented resizeEvent(QResizeEvent *event) in order to change the size of the chart when the window is resized.

    The code in resizeEvent(QResizeEvent *event) is as follows:

    m_scene->setSceneRect(QRect(QPoint(0, 0), ui->graphicsView->size()));
    m_chart->resize(ui->graphicsView->size());
    

    Resizing is working but the issue I have are the scrollbars.

    • Why are they displayed given that the Scene size is the same as the View size?

    • How can I get rid of them?



  • @Bert59
    m_scene-setSeceneRect(QRect(QPoint(0,0), ui->graphicsView->viewport()->rect().size())); should be what you need.



  • @Bert59 said in QGraphicsScene position and size:

    Why are they displayed given that the Scene size is the same as the View size?

    Make the scene a few px smaller.



  • @Bert59
    m_scene-setSeceneRect(QRect(QPoint(0,0), ui->graphicsView->viewport()->rect().size())); should be what you need.



  • Thank you for your support.

    The solution of Pl45m4 is working but the solution of artwaw seems more elegant.

    Kind regards



  • @artwaw
    I'm sure you're right, but I struggle with this conceptually. I have only done a couple of apps with QGraphicsScene/View.

    Why are we setting the scene rect at all, why are we trying to fit it to the view? Why aren't we setting the view size to the scene?



  • @JonB background - for me this concept (viewport, that is) dates back to Borland Pascal 7 (DOS graphic modes) & Turbo Vision 2.0 (text mode windows) so I am sorry if I didn't get things quite Qt way.

    Scene can be, and very often is, larger than viewport. Again, size of the view itself is larger than that of the viewport. Essentially it is laid out in the docs, QGraphicsView visualises the contents of a QGraphicsScene in a scrollable viewport. QGraphicsView::size() returns the size of the view with all the decorators (frames, borders, what's not) while viewport() is the space on which the scene is being visible.

    Imagine image browser written using QGraphicsView and the corresponding scene. Such program often has different modes of displaying the image (for the sake of the example we'll leave fullscreen view on the side here): fit all in window (viewport really), fit only larger images (scale down), fit only smaller images (scale up) and finally do not fit anything.
    The way to go is to render QGraphicsPixmapItem (for raster images) on the scene which is presented in the view. Simple. But.
    One of the properties of the scene is that it only grows, never shrinks on its own. So you have to clip it from time to time (for example when new image loaded is smaller in the view than the previous one, otherwise you get small image with completely unnecessary scrollbars. Now, if you clip it to the size of the view, you will see the scrollbars anyway (and sometimes the image itself will be clipped by lack of the space) since the scene will be larger than the viewport.
    Of course, with scaling single GraphicsItem we can use https://doc.qt.io/qt-5/qgraphicsview.html#fitInView-2 but still we need to take care of clipping the scene.

    All this behaviour I consider completely understandable, especially when you try to render the graphics with mobile viewport (pov), when you have many items to render from which not all need to visible (or rather in the viewport) at the given moment...

    Ok, this is longer than I intended (but I can talk about interesting things for hours, so there you have it).

    I also wrote (for my personal use, at least for now) the image browser with basic functionalities like IrfanView, since there is nothing reliable or free I could find on macOS that would sit right with me. I used QGraphicsView for the display and the journey from the idea to the working (more or less) product has been really enjoyable. So there, that's a bunch of my purely personal impressions.

    Cheers.


Log in to reply