Results 1 to 5 of 5

Thread: QGraphicsView unwanted margin

  1. #1
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question QGraphicsView unwanted margin

    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!
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView unwanted margin

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView unwanted margin

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView unwanted margin

    Quote Originally Posted by hheld View Post
    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView unwanted margin

    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 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)
    Attached Images Attached Images
    Last edited by hheld; 3rd January 2011 at 17:30.

Similar Threads

  1. Avoiding unwanted button signals.
    By Eos Pengwern in forum Newbie
    Replies: 2
    Last Post: 7th January 2010, 15:58
  2. Unwanted padding on QWidget
    By etru1927 in forum Qt Programming
    Replies: 4
    Last Post: 7th August 2008, 07:42
  3. VS2005 proj file has unwanted name
    By Vanir in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2008, 15:53
  4. QMdiArea unwanted actvation
    By fullmetalcoder in forum Qt Programming
    Replies: 7
    Last Post: 12th November 2007, 07:09
  5. How to Set margin
    By suresh in forum Newbie
    Replies: 1
    Last Post: 5th October 2006, 06:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.