我试图在QGraphicsScene
中制作一个弧线但是会出现以下错误:
error: cannot allocate an object of abstract type 'arc'
arcItem = new arc(++id, startP, midP, endP);
我在mousepressevent的cadgraphicsscene中遇到错误
void CadGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
// mousePressEvent in the graphicsScene
if(mouseEvent->button() == Qt::LeftButton)
{
switch (entityMode)
{
case ArcMode:
if (mFirstClick)
{
startP = mouseEvent->scenePos();
mFirstClick = false;
mSecondClick = true;
}
else if (!mFirstClick && mSecondClick)
{
midP = mouseEvent->scenePos();
mFirstClick = false;
mSecondClick = false;
mThirdClick = true;
}
else if (!mSecondClick && mThirdClick)
{
endP = mouseEvent->scenePos();
mThirdClick = false;
mPaintFlag = true;
}
if (mPaintFlag)
{
arcItem = new arc(++id, startP, midP, endP);
itemList.append(arcItem);
mUndoStack->push(new CadCommandAdd(this, arcItem));
setFlags();
}
default:
;
}
}
我的arc类如下所示:
arc.cpp
arc::arc(int i, QPointF point1, QPointF point2, QPointF point3)
{
// assigns id
id = i;
p1 = point1;
p2 = point2;
p3 = point3;
}
int arc::type() const
{
// Enable the use of qgraphicsitem_cast with ellipse item.
return Type;
}
void arc::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QPointF O; // intersection of lines
QPointF B; // end point of horizontal line
QPointF A;
float halfSide = B.x()-O.x();
QRectF rectangle(O.x() - halfSide,
O.y() - halfSide,
O.x() + halfSide,
O.y() + halfSide);
int startAngle = 0;
int spanAngle = (atan2(A.y()-O.y(),A.x()-O.x()) * 180 / M_PI) * 16;
painter->drawArc(rectangle, startAngle, spanAngle);
}
上面的类头文件定义如下:
arc.h
class arc : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
arc(int, QPointF, QPointF, QPointF);
virtual void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget);
enum { Type = UserType + 6 };
int type() const;
int id;
QPointF p1, p2, p3;
}
请帮我解决错误。