yuhong
13th March 2006, 12:19
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?
/////////////////////////////////////////////////////////////////////////
class CircleItem : public QCanvasEllipse
{
public:
CircleItem( int cx, int cy, int r, QCanvas *c ) : QCanvasEllipse( c )
{
this->cx = cx;
this->cy = cy;
this->r = r;
}
~CircleItem()
{
hide();
}
QPointArray areaPoints() const
{
QPointArray res(4);
res.setPoint( 0, QPoint( cx-r, cy-r ) );
res.setPoint( 1, QPoint( cx-r, cy+r ) );
res.setPoint( 2, QPoint( cx+r, cy+r ) );
res.setPoint( 3, QPoint( cx+r, cy-r ) );
return res;
}
protected:
void drawShape( QPainter &p )
{
p.drawEllipse( cx-r, cy-r, 2*r, 2*r );
}
private:
int cx;
int cy;
int r;
};
/////////////////////////////////////////////////////////////////////////
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.
void MyCanvasView::drawCircle-1()
{
QPoint p0 = inverseWorldMatrix().map(points[0]);
QPoint p1 = inverseWorldMatrix().map(points[1]);
int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
QCanvasPolygonalItem* i = new QCanvasEllipse(2*r, 2*r, canvas());;
i->move(p0.x(), p0.y());
i->setBrush( Qt::red ); ***********
i->show();
}
void MyCanvasView::drawCircle-2()
{
QPoint p0 = inverseWorldMatrix().map(points[0]);
QPoint p1 = inverseWorldMatrix().map(points[1]);
int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
QCanvasPolygonalItem* i = new CircleItem(p0.x(), p0.y(), r, canvas());;
i->setPen(ROIPEN); ***********
i->show();
}
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!
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?
/////////////////////////////////////////////////////////////////////////
class CircleItem : public QCanvasEllipse
{
public:
CircleItem( int cx, int cy, int r, QCanvas *c ) : QCanvasEllipse( c )
{
this->cx = cx;
this->cy = cy;
this->r = r;
}
~CircleItem()
{
hide();
}
QPointArray areaPoints() const
{
QPointArray res(4);
res.setPoint( 0, QPoint( cx-r, cy-r ) );
res.setPoint( 1, QPoint( cx-r, cy+r ) );
res.setPoint( 2, QPoint( cx+r, cy+r ) );
res.setPoint( 3, QPoint( cx+r, cy-r ) );
return res;
}
protected:
void drawShape( QPainter &p )
{
p.drawEllipse( cx-r, cy-r, 2*r, 2*r );
}
private:
int cx;
int cy;
int r;
};
/////////////////////////////////////////////////////////////////////////
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.
void MyCanvasView::drawCircle-1()
{
QPoint p0 = inverseWorldMatrix().map(points[0]);
QPoint p1 = inverseWorldMatrix().map(points[1]);
int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
QCanvasPolygonalItem* i = new QCanvasEllipse(2*r, 2*r, canvas());;
i->move(p0.x(), p0.y());
i->setBrush( Qt::red ); ***********
i->show();
}
void MyCanvasView::drawCircle-2()
{
QPoint p0 = inverseWorldMatrix().map(points[0]);
QPoint p1 = inverseWorldMatrix().map(points[1]);
int r = (int) sqrt( pow(p0.x()-p1.x(),2)+pow(p0.y()-p1.y(),2) );
QCanvasPolygonalItem* i = new CircleItem(p0.x(), p0.y(), r, canvas());;
i->setPen(ROIPEN); ***********
i->show();
}
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!