Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGraphicsView fitInView margins

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.

like image 810
Tarantula Avatar asked Oct 28 '13 16:10

Tarantula


3 Answers

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

like image 87
Jason Newton Avatar answered Nov 19 '22 02:11

Jason Newton


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.

like image 29
Tarantula Avatar answered Nov 19 '22 04:11

Tarantula


You can fix it reimplementing the original fitInView method, but without including the margins. Check my C++ solution at: https://stackoverflow.com/a/42474510/6050364

like image 1
Adriel Jr Avatar answered Nov 19 '22 04:11

Adriel Jr