Reputation: 127
I'm trying to create a custom QGraphicsLineItem
called Edge, but I'm not doing it well I'm new in Qt
.
For now, I can't for example do scene->add(edgeItem)
without error no viable conversion from 'Edge
' to 'QGraphicsItem
*'.
My edge.h
#ifndef EDGE_H
#define EDGE_H
#include <QWidget>
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
class Edge:public QGraphicsLineItem
{
private:
QGraphicsEllipseItem * state1;
QGraphicsEllipseItem * state2;
public:
enum { Type = UserType + 2 };
int type() const override;
Edge(QGraphicsEllipseItem * s1, QGraphicsEllipseItem* s2);
QGraphicsEllipseItem *getState1() const;
void setState1(QGraphicsEllipseItem *value);
QGraphicsEllipseItem *getState2() const;
void setState2(QGraphicsEllipseItem *value);
QList<QPointF> centerPoints();
};
#endif // EDGE_H
My edge.cpp
#include "edge.h"
#include <QGraphicsEllipseItem>
Edge::Edge(QGraphicsEllipseItem* s1, QGraphicsEllipseItem* s2)
{
setState1(s1);
setState2(s2);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
setFlag(QGraphicsItem::ItemIsSelectable);
}
int Edge::type() const
{
return Type;
}
QGraphicsEllipseItem *Edge::getState1() const
{
return state1;
}
void Edge::setState1(QGraphicsEllipseItem *value)
{
state1 = value;
}
QGraphicsEllipseItem *Edge::getState2() const
{
return state2;
}
void Edge::setState2(QGraphicsEllipseItem *value)
{
state2 = value;
}
QList<QPointF> Edge::centerPoints()
{
QPointF point1(state1->rect().x() + state1->rect().width()/2,state1->rect().y()+ state1-> rect().height()/2);
QPointF point2(state2->rect().x()+state2->rect().width()/2,state2->rect().y()+ state2-> rect().height()/2);
QList<QPointF> list;
list.append(point1);
list.append(point2);
return list;
}
Can someone show me how I can modify my Edge to make it a "real" QGraphicsItem
?
Upvotes: 0
Views: 525
Reputation: 126
When creating your Edge class instance, do it this way.
Edge* someinstance = new Edge();
scene->addItem(someinstance);
I assume what you're doing is.
Edge instance;
scene->addItem(instance);
So compiler tries implicitly convert Edge object to QGraphicsItem* object wich is impossible.
Upvotes: 0
Reputation: 776
here is an example of proper inheritance
so .h
class Edge: public QGraphicsLineItem
{
public:
Edge(QGraphicsEllipseItem * s1 = nullptr, QGraphicsEllipseItem* s2= nullptr, QGraphicsItem* parent = nullptr);
//.....
};
and .cpp
Edge::Edge(QGraphicsEllipseItem * s1, QGraphicsEllipseItem* s2, QGraphicsItem* parent)
:QGraphicsLineItem( parent)
{
//.........
}
Upvotes: 2