Results 1 to 6 of 6

Thread: paint Rect sometimes missing one border

  1. #1
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default paint Rect sometimes missing one border

    I have a QGraphicsRectItem. In its paint() method:

    Qt Code:
    1. painter->setPen(QPen(Qt::red, 1, Qt::SolidLine));
    2. painter->setBrush(QBrush(Qt::gray, Qt::Dense4Pattern));
    3. painter->drawRect(draw_rect);
    To copy to clipboard, switch view to plain text mode 




    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() - adjust/2, m_rect.topLeft().ry() - adjust/2,
    5. m_rect.width() + adjust, m_rect.height() + adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 


    Most of the time, the rectangle is painted correctly. But after resize, sometimes it will miss the red line boarder, for example the left-side edge is not painted.

    I tried with following: doesn't work:

    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() - adjust, m_rect.topLeft().ry() - adjust,
    5. m_rect.width() + adjust, m_rect.height() + adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any one know this problem?
    Last edited by stella1016; 19th January 2011 at 10:57.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: paint Rect sometimes missing one border

    I am not sure I follow on your code but if m_rect is supposed to be a sub-rect , and adjust is the marigne to the original rect, then I would expect the following:
    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() + adjust, m_rect.topLeft().ry() + adjust,
    5. m_rect.width() - adjust, m_rect.height() - adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Otherwise it looks you want to draw a rect which is larger by 'adjust' to each direction of a given rect, if that is what you want, then I see nothing wrong in the code.

    If you are scaling down (large to small) you might have a problem with the pen width, in that case you should set the pen width to 0.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: paint Rect sometimes missing one border

    Quote Originally Posted by high_flyer View Post
    I am not sure I follow on your code but if m_rect is supposed to be a sub-rect , and adjust is the marigne to the original rect, then I would expect the following:
    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() + adjust, m_rect.topLeft().ry() + adjust,
    5. m_rect.width() - adjust, m_rect.height() - adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Otherwise it looks you want to draw a rect which is larger by 'adjust' to each direction of a given rect, if that is what you want, then I see nothing wrong in the code.

    If you are scaling down (large to small) you might have a problem with the pen width, in that case you should set the pen width to 0.
    Yes, I add the adjust of 1.0, since the pen width is 1. The actual QGraphicsRectItem has the m_rect set as its rectangle.

    I tested it also with setting pen, and set adjust with half-size of pen width:
    Qt Code:
    1. //in boundingRect()
    2. qreal adjust = pen().widthF();
    3. QRectF result(m_rect.topLeft().rx() - adjust/2.0, m_rect.topLeft().ry() - adjust/2.0,
    4. m_rect.width() + adjust, m_rect.height() + adjust);
    To copy to clipboard, switch view to plain text mode 


    This is one of the example of qt documentation of boundingRect like follows:
    Qt Code:
    1. QRectF CircleItem::boundingRect() const
    2. {
    3. qreal penWidth = 1;
    4. return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
    5. diameter + penWidth, diameter + penWidth);
    6. }
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:




    The problem can hanppen in any of these rectangle and any side during resize. But after again resize one pixel larger or smaller, the border shows again. I am not sure in painting, whether the floating point is rounded.
    Last edited by stella1016; 19th January 2011 at 14:55.

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: paint Rect sometimes missing one border

    Quote Originally Posted by stella1016 View Post
    Most of the time, the rectangle is painted correctly. But after resize, sometimes it will miss the red line boarder, for example the left-side edge is not painted.
    Resize is the problem. When you resize graphics item and it has impact on boundingRect you have to call: QGraphicsItem::prepareGeometryChange (not inside boundingRect), see also QGraphicsItem::boundingRect.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: paint Rect sometimes missing one border

    More or less it should look like this:
    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. static const qreal Adjust = 0.5;
    4.  
    5. return m_rect.adjusted(-Adjust, -Adjust, Adjust, Adjust);
    6. }
    7.  
    8. QRectF Myclass::setSize(const QSizeF &newSize)
    9. {
    10. if (m_rect.size()==newSize) {
    11. return;
    12. }
    13. m_rect.setSize(newSize);
    14.  
    15. prepareGeometryChange();
    16. update();
    17. }
    18.  
    19. QRectF Myclass::setTopLeft(const QPointF &newPosition)
    20. {
    21. if (m_rect.topLeft()==newPosition) {
    22. return;
    23. }
    24. m_rect.setTopLeft(newPosition);
    25.  
    26. prepareGeometryChange();
    27. update();
    28. }
    To copy to clipboard, switch view to plain text mode 
    I don't now how did you do this class so most probably it will look a bit differently.

  6. #6
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paint Rect sometimes missing one border

    My suspicion is that this is due to a round off error. Adjust is a real. In the bounding rectangle calculation you divide this by 2, which is an integer. This causes the result also to be of type integer (thus, causing rounding errors). You can avoid this by dividing by 2.0.

    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() - adjust/2, m_rect.topLeft().ry() - adjust/2,
    5. m_rect.width() + adjust, m_rect.height() + adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Should be:

    Qt Code:
    1. QRectF Myclass::boundingRect() const
    2. {
    3. qreal adjust = 1.0;
    4. QRectF result(m_rect.topLeft().rx() - adjust/2.0, m_rect.topLeft().ry() - adjust/2.0,
    5. m_rect.width() + adjust, m_rect.height() + adjust);
    6.  
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QGraphicsItem and the selection rect
    By Markus in forum Qt Programming
    Replies: 0
    Last Post: 9th February 2010, 22:21
  2. How to draw the below round rect?
    By tszzp in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2010, 08:58
  3. Qcombobox item rect?
    By shud in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2009, 12:07
  4. Paint Rect visible on Drag to crop image coordinate
    By patrik08 in forum Qt Programming
    Replies: 8
    Last Post: 17th March 2007, 09:04
  5. QListWidgetItem Text Rect
    By js67257 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2006, 12:02

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.