PDA

View Full Version : QGraphicsView unwanted margin



hheld
2nd January 2011, 15:48
To experiment a little with QGraphicsView, I wanted to implement a plot widget that plots 2D data points, connecting consecutive points by a line. Both horizontal and vertical scrollbars are set to be always off.

First test was a plot of a sine function, discretized into 50 equidistant data points on the interval [0, 2*pi]. That yields a y-range of [-0.999486, +0.999486]. The underlying SceneRect's size is set to exactly these width and height, resp. Therefore, I expected the plot to reach all the way to the QGraphicsView's boundary, essentially touching its edges. However, and this is what my question is about: there is always a margin of maybe 1 or 2 pixels. Where does this margin come from, and how can it be removed? What else could I have missed and done wrong?

Attached you find two images of the plot. In the first one ('plot_centered.jpg'), the QGraphicsView is aligned horizontally and vertically centered, and the margin is clearly visible on each edge. In the other one ('plot_leftTop.jpg'), it is aligned to the "left and top". The result is that the plot starts exactly at the left and top edge, but the gap next to the other 2 edges is accordingly bigger.

Not sure if it's important, but I'm using Qt 4.7.0 on Linux. Any help or hints are greatly appreciated!

wysota
2nd January 2011, 17:54
If I understand correctly what you mean then the margin comes from the fact that the graphics view widget is really composed of two widgets - the view widget and its viewport and there is a margin between the view and the viewport which is what you might be experiencing. Basically it's the viewport size that is interesting to you and not the view size. You can also manipulate the margin between the view and the viewport.

hheld
2nd January 2011, 20:13
Thanks for your message! Do you mean the actual QGraphicsView widget by "view widget"? How can I control that margin?

I looked at the sizes of the actual widget (QGraphicsView), its viewport, and its frame. Once I removed the frame (frame shape -> NoFrame), all of these 3 have the exact same size. But there is still a visible gap. I have to slightly crop the SceneRect before calling fitInView() to get the desired result.

wysota
2nd January 2011, 22:24
Do you mean the actual QGraphicsView widget by "view widget"?
Yes.


How can I control that margin?
Well... there is QAbstractScrollArea::setViewportMargins(), you can probably do your own calculations in resizeEvent(). Setting QWidget::setContentsMargins() might also be effective. I don't have a ready answer to that question. You can take a look at QAbstractScrollAreaPrivate::layoutChildren() to see how things are calculated.


I have to slightly crop the SceneRect before calling fitInView() to get the desired result.
Always rely on QAbstractScrollArea::maximumViewportSize().

hheld
3rd January 2011, 17:16
Hi wysota,

thanks again for your help. I looked at the code of QAbstractScrollAreaPrivate::layoutChildren() but didn't really find anything that could cause that behavior.

However, I also looked at the implementation of QGraphicsView::fitInView(...). There is a hard-coded margin of 2 in there, I guess that's what I'm seeing. Using google, I then found this bug report (http://bugreports.qt.nokia.com/browse/QTBUG-11945) regarding the issue.

So I guess the solution is to implement my own version of fitInView() without that margin, which shouldn't be that hard to do.

Edit: OK, just tried that, and it's working now as expected (cf. attached screenshot)