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