使用QGraphicsItem和QGraphicsLineItem继承

时间:2014-11-08 06:49:48

标签: c++ qt inheritance qgraphicsitem

我有一个继承QGraphicsItem的类,这个类需要继承另一个继承QGraphicsLineItem和QGraphicsTextItem的类。当我这样做时它给了我错误类Line没有名为setLine的成员

以下是整个场景解释:

getEntity.h

#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

line.h

//This is my class that needs to inherits gEntity

    #ifndef LINE_H
    #define LINE_H

    #include <QPainter>
    #include <QGraphicsLineItem>

    class Line : public QObject, public QGraphicsLineItem
    {
        Q_OBJECT
    public:
        Line(int, QPointF, QPointF);
        QRectF boundingRect() const;
        virtual void paint(QPainter *painter,
                           const QStyleOptionGraphicsItem *option,
                           QWidget *widget);
        enum { Type = UserType + 2 };
        int type() const;
        int id;

        QPointF start_p, end_p, move_p, check_p;

    private:
        QVector<QPointF> stuff;
    };

    #endif // LINE_H

line.cpp

#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();
    }
}

cadgraphicscene.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?帮忙!

0 个答案:

没有答案