Results 1 to 5 of 5

Thread: QGraphicsItem tears when dragged

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem tears when dragged

    Hello!

    I have a QGraphicsScene in a QGraphicsWidget and in the scene I have two QGraphicsItems. One of the items is instantiated with an addEllipse() in the constructor of the scene, the other one subclasses QGraphicsItem and is added with addItem() also in the constructor of the scene. When I drag the items with the mouse, the addEllipse() instantiated item works perfectly, but the self instantiated item "tears" the screen (see screenshot).

    My graphics widget draws a grid on the background by overriding drawBackground() and this appears to be the source of the problem. Did I not do something right or is this a bug? All relevant code is pasted below.


    sshot.jpg


    Qt Code:
    1. class GraphicsViewWidget : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. GraphicsViewWidget(QWidget *parent = 0);
    7. ~GraphicsViewWidget();
    8.  
    9.  
    10. protected:
    11. void drawBackground(QPainter* painter, const QRectF& rect);
    12. };
    13.  
    14. void GraphicsViewWidget::drawBackground(QPainter *painter, const QRectF &rect)
    15. {
    16. painter->setRenderHint(QPainter::Antialiasing, false);
    17.  
    18. // draw axes
    19. double xmargin = 30/transform().m11(); // pixel
    20. double ymargin = 30/transform().m22(); // pixel
    21. double x = qBound(rect.left() + xmargin, 0.0, rect.right() - xmargin);
    22. double y = qBound(rect.top() - ymargin, 0.0, rect.bottom() + ymargin);
    23. painter->setPen(QPen(QColor(100, 100, 155, 255)));
    24. painter->drawLine(QPointF(x, rect.top()), QPointF(x, rect.bottom()));
    25. painter->drawLine(QPointF(rect.left(), y), QPointF(rect.right(), y));
    26.  
    27. setRenderHint(QPainter::Antialiasing, true);
    28. }
    29.  
    30. class LimpScene : public QGraphicsScene
    31. {
    32. Q_OBJECT
    33.  
    34. ComState comState; // my custom QGraphicsWidget
    35. QGraphicsEllipseItem* com; // will be added by addEllipse()
    36.  
    37. public:
    38. LimpScene(QWidget *parent = 0);
    39. ~LimpScene(){};
    40.  
    41. };
    42.  
    43. LimpScene::LimpScene(QWidget *parent)
    44. : QGraphicsScene(parent)
    45. {
    46. setSceneRect(-100, -100, 200, 200);
    47.  
    48. QPen pen;
    49. pen.setCosmetic(true);
    50. pen.setWidth(2);
    51.  
    52. QBrush magentaBrush(QColor::fromRgb(255,0,255,125));
    53.  
    54. double comRadius = 0.05;
    55. com = addEllipse(-comRadius, -comRadius, 2.0*comRadius, 2.0*comRadius, pen, magentaBrush);
    56. com->setFlags(QGraphicsItem::ItemIsMovable);
    57.  
    58. addItem(&comState);
    59. comState.setFlags(QGraphicsItem::ItemIsMovable);
    60. }
    61.  
    62.  
    63. class ComState : public QGraphicsItem
    64. {
    65. public:
    66. ComState(QGraphicsItem *parent = 0);
    67. ~ComState(){};
    68.  
    69. QRectF boundingRect() const;
    70. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    71. };
    72.  
    73. ComState::ComState(QGraphicsItem *parent)
    74. : QGraphicsItem(parent)
    75. {
    76.  
    77. }
    78.  
    79. QRectF ComState::boundingRect() const
    80. {
    81. return QRectF(-0.1, -0.1, 0.2, 0.2);
    82. }
    83.  
    84. void ComState::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    85. QWidget *widget)
    86. {
    87. QPen pen;
    88. pen.setCosmetic(true);
    89. pen.setWidth(2);
    90.  
    91. QBrush yellowBrush(QColor::fromRgb(255,255,0,125));
    92.  
    93. double comRadius = 0.05;
    94.  
    95. painter->setPen(pen);
    96. painter->setBrush(yellowBrush);
    97. painter->drawEllipse(QPointF(0,0), comRadius, comRadius);
    98. painter->drawLine(QPointF(0, 0), QPointF(2.0*comRadius, 0));
    99. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tears when dragged

    You could try:

    - filling the rect in drawBackground
    - calling the default implementation first then doing your drawing
    - enabling background caching

    Cheers,
    _

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tears when dragged

    Ok I tried these things.

    About filling the rect, I tried to do this in the paint() method of the custom graphics item (painter->eraseRect(boundingRect())). It does erase, but the tear appears the same as before. QGraphicsItem doesn't have a drawBackground() or drawForeground() method I could tinker with. QGraphicsScene does, but they never get called. QGraphicsView does too, but erasing the bounding rect of an item in the scene requires an ugly access to an object the view is not supposed to see, and also it doesn't work.

    Calling the default implementation, I tried calling QGraphicsItem:aint() out of my custom paint method and I get a linker error "undefined reference to ..." and I don't understand why this happens. Calling QGraphicsView::drawBackground() out of my custom view's drawBackground() has no effect.

    Enabling background caching did have a noticeable effect, and yes the tear of the graphics item disappeared!, but now the tear appears all over the place instead when I drag the scene around.

    Interestingly, I discovered that if I do this:

    Qt Code:
    1. void GraphicsViewWidget::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. painter->setRenderHint(QPainter::Antialiasing, false);
    4. setRenderHint(QPainter::Antialiasing, false);
    5.  
    6. drawTheGrid();
    7.  
    8. setRenderHint(QPainter::Antialiasing, true);
    9. }
    To copy to clipboard, switch view to plain text mode 

    then dragging the item works as expected. I have no explanation why this works and I considers myself lucky that I found this out by accident.

    Cheers,
    Cruz

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tears when dragged

    Quote Originally Posted by Cruz View Post
    Ok I tried these things.
    You could also have tried one of the things from my list

    Quote Originally Posted by Cruz View Post
    About filling the rect, I tried to do this in the paint() method of the custom graphics item (painter->eraseRect(boundingRect())). It does erase, but the tear appears the same as before.
    Obviously, the item and therefore its bounding rect has already moved.


    Quote Originally Posted by Cruz View Post
    QGraphicsView does too, but erasing the bounding rect of an item in the scene requires an ugly access to an object the view is not supposed to see, and also it doesn't work.
    My first suggestion was to fill the background rect that needs repainting, you know the one that is passed to drawBackground() for a reason.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tears when dragged

    Ah ok that's the rect you mean. Yap, I cleared it. Nope, it didn't work.


    Added after 6 minutes:


    Calling setViewportUpdateMode(QGraphicsView::FullViewportU pdate) in the constructor of the view fixes the problem too.
    Last edited by Cruz; 22nd June 2014 at 21:45.

Similar Threads

  1. get a pixmap for a dragged QTreeWidgetItem?
    By qlands in forum Qt Programming
    Replies: 7
    Last Post: 18th September 2016, 07:33
  2. Replies: 2
    Last Post: 9th January 2014, 10:26
  3. Replies: 1
    Last Post: 2nd July 2012, 03:54
  4. Replies: 0
    Last Post: 20th August 2010, 23:39
  5. Mouse dragged widget without title
    By Tonal in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2006, 12:28

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.