Hi, everyone,

I have a CircleItem class which inherits QCanvasEllipse. I reimplement areaPoints() and drawShape(). When I'm trying to move CircleItem, it doesn't work.

Why? what else should I do?

Qt Code:
  1. /////////////////////////////////////////////////////////////////////////
  2. class CircleItem : public QCanvasEllipse
  3. {
  4. public:
  5. CircleItem( int cx, int cy, int r, QCanvas *c ) : QCanvasEllipse( c )
  6. {
  7. this->cx = cx;
  8. this->cy = cy;
  9. this->r = r;
  10. }
  11.  
  12. ~CircleItem()
  13. {
  14. hide();
  15. }
  16.  
  17. QPointArray areaPoints() const
  18. {
  19. QPointArray res(4);
  20. res.setPoint( 0, QPoint( cx-r, cy-r ) );
  21. res.setPoint( 1, QPoint( cx-r, cy+r ) );
  22. res.setPoint( 2, QPoint( cx+r, cy+r ) );
  23. res.setPoint( 3, QPoint( cx+r, cy-r ) );
  24. return res;
  25. }
  26.  
  27.  
  28. protected:
  29. void drawShape( QPainter &p )
  30. {
  31. p.drawEllipse( cx-r, cy-r, 2*r, 2*r );
  32. }
  33.  
  34. private:
  35. int cx;
  36. int cy;
  37. int r;
  38. };
  39.  
  40. /////////////////////////////////////////////////////////////////////////
To copy to clipboard, switch view to plain text mode 


Here are two methods that draw a circle on MyCanvasView. The first one uses QCanvasEllipse, it works fine. The second uses my CircleItem, it doesn't work.

Qt Code:
  1. void MyCanvasView::drawCircle-1()
  2. {
  3. QPoint p0 = inverseWorldMatrix().map(points[0]);
  4. QPoint p1 = inverseWorldMatrix().map(points[1]);
  5.  
  6. int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
  7.  
  8. QCanvasPolygonalItem* i = new QCanvasEllipse(2*r, 2*r, canvas());;
  9. i->move(p0.x(), p0.y());
  10. i->setBrush( Qt::red ); ***********
  11. i->show();
  12.  
  13. }
  14.  
  15.  
  16. void MyCanvasView::drawCircle-2()
  17. {
  18. QPoint p0 = inverseWorldMatrix().map(points[0]);
  19. QPoint p1 = inverseWorldMatrix().map(points[1]);
  20.  
  21. int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
  22.  
  23. QCanvasPolygonalItem* i = new CircleItem(p0.x(), p0.y(), r, canvas());;
  24. i->setPen(ROIPEN); ***********
  25. i->show();
  26.  
  27. }
To copy to clipboard, switch view to plain text mode 


The Doc says:
Note: QCanvasEllipse does not use the pen.
In my subclass, I used setPen(), because I need to draw the outline of the circle instead of filling it.

Maybe this is the cause?

Can anyone help?

Thanks!