QGraphicsView fitInView margins
Asked Answered
H

3

5

Why this:

graphics_view->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);

doesn’t work as expected ? It isn't fitting the scene rect correctly, showing margins around it.

Hawkshaw answered 28/10, 2013 at 16:54 Comment(1)
Add a minimal code example, and a screenshot, and how it differs from what you expect.Christabelle
H
2

I figured out what the problem was, it is described in this question. The main cause of the problem is that you shouldn't call fitInView before the form is shown.

Hawkshaw answered 29/10, 2013 at 15:31 Comment(1)
actually it depends on exactly what you mean by just margins, I suppose since you also asked the question it could've been these huge ones in the question you linked. Regardless, you can call fitInView all you want before the form is shown.Trent
T
4

The cause is this: https://bugreports.qt.io/browse/QTBUG-42331 - please vote on it on the qt bug tracker to up its priority.

In short, fitInView has hardcoded margins and this can cause all kinds of havoc - the least of which is that now you lose a few pixels of display area and might also force unnecessary rescaling.

You can fix the problem by reimplementing fitInView, based on the existing implementation but removing it's ridiculous margins. An example of that is available here in python, based on the original C++ version:

https://github.com/nevion/pyqimageview/blob/master/qimageview/widget.py#L276

Trent answered 5/5, 2015 at 11:17 Comment(0)
H
2

I figured out what the problem was, it is described in this question. The main cause of the problem is that you shouldn't call fitInView before the form is shown.

Hawkshaw answered 29/10, 2013 at 15:31 Comment(1)
actually it depends on exactly what you mean by just margins, I suppose since you also asked the question it could've been these huge ones in the question you linked. Regardless, you can call fitInView all you want before the form is shown.Trent
C
1

You can fix it reimplementing the original fitInView method, but without including the margins. Check my C++ solution at: https://mcmap.net/q/1936043/-unwanted-margin-inside-qgraphicsview-with-scrollbars

Catherincatherina answered 26/2, 2017 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.