C++ 使用QGraphicsSitem和QGraphicsLineItem进行继承
我有一个继承QGraphicsItem的类,这个类需要被另一个继承QGraphicsLineItem和QGraphicsTextItem的类继承。当我这样做时,它会给我一个错误类Line没有名为setLine的成员 下面是整个场景的说明: getEntity.hC++ 使用QGraphicsSitem和QGraphicsLineItem进行继承,c++,qt,inheritance,qgraphicsitem,C++,Qt,Inheritance,Qgraphicsitem,我有一个继承QGraphicsItem的类,这个类需要被另一个继承QGraphicsLineItem和QGraphicsTextItem的类继承。当我这样做时,它会给我一个错误类Line没有名为setLine的成员 下面是整个场景的说明: getEntity.h #ifndef getEntity_H #define getEntity_H #include <QGraphicsItem> class getEntity :public QObject , public QGra
#ifndef getEntity_H
#define getEntity_H
#include <QGraphicsItem>
class getEntity :public QObject , public QGraphicsItem
{
public:
getEntity(QObject* parent=0) : QObject(parent) {}
virtual ~getEntity() {}
virtual getEntity* my_clone() { return 0; }
};
#endif // getEntity_H
cadgraphicsene.cpp
//Here it returns pointing to setLine method, when I inherit getEntity in Line class
if (mPaintFlag)
{
lineItem = new Line(++id, start_p, end_p);
lineItem->setLine(start_p.x(), start_p.y(), end_p.x(), end_p.y());
Line类如何正确继承getEntity?帮帮忙 为什么您要继承QObject?我使用的是信号和插槽的概念,而不是您所展示的代码。不能从QObject继承乘法。我一点也不明白你想用这段代码实现什么,但看起来你有一个设计问题。多重继承是一个非常复杂的工具。也许如果你准确地描述了你想要实现的目标,有人会指出一个更明智的方法。我想在我的图形视图中有剪切、复制和粘贴操作。我的所有实体都有不同的类,继承自Graphics Item、QGraphicsLineItem和GraphicsText Item。我正在使用getEntity类克隆所有实体的属性。要使用信号和插槽,放置
Q_对象
(就像您在类行
)就足够了。
#include "line.h"
Line::Line(int i, QPointF p1, QPointF p2)
{
// assigns id
id = i;
// set values of start point and end point of line
start_p = p1;
end_p = p2;
}
int Line::type() const
{
// Enable the use of qgraphicsitem_cast with line item.
return Type;
}
QRectF Line::boundingRect() const
{
qreal extra = 1.0;
// bounding rectangle for line
return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
line().p2().y() - line().p1().y()))
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
void Line::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
// draws/paints the path of line
QPen paintpen;
painter->setRenderHint(QPainter::Antialiasing);
paintpen.setWidth(1);
if (isSelected())
{
// sets brush for end points
painter->setBrush(Qt::SolidPattern);
paintpen.setColor(Qt::red);
painter->setPen(paintpen);
painter->drawEllipse(start_p, 2, 2);
painter->drawEllipse(end_p, 2, 2);
// sets pen for line path
paintpen.setStyle(Qt::DashLine);
paintpen.setColor(Qt::black);
painter->setPen(paintpen);
painter->drawLine(start_p, end_p);
}
else
{ painter->save();
painter->setBrush(Qt::SolidPattern);
paintpen.setColor(Qt::black);
painter->setPen(paintpen);
painter->drawEllipse(start_p, 2, 2);
painter->drawEllipse(end_p, 2, 2);
painter->drawLine(start_p, end_p);
painter->restore();
}
}
//Here it returns pointing to setLine method, when I inherit getEntity in Line class
if (mPaintFlag)
{
lineItem = new Line(++id, start_p, end_p);
lineItem->setLine(start_p.x(), start_p.y(), end_p.x(), end_p.y());