I need to subclass an existing QGraphicsItem and change the painting
and size. I started modifying the elasticnodes example and came up with
these changes:
{
public:
Node(GraphWidget *graphWidget);
void addEdge(Edge *edge);
QList<Edge *> edges() const;
enum { Type = UserType + 1 };
int type() const { return Type; }
void calculateForces();
bool advance();
virtual QRectF boundingRect
() const;
protected:
QList<Edge *> edgeList;
GraphWidget *graph;
private:
};
class Node : public QGraphicsItem
{
public:
Node(GraphWidget *graphWidget);
void addEdge(Edge *edge);
QList<Edge *> edges() const;
enum { Type = UserType + 1 };
int type() const { return Type; }
void calculateForces();
bool advance();
virtual QRectF boundingRect() const;
QPainterPath shape() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
QList<Edge *> edgeList;
QPointF newPos;
GraphWidget *graph;
private:
};
To copy to clipboard, switch view to plain text mode
So for my derived class, I need to make boundingRect(), paint(), and mousePressEvent() virtual, because I want my class to paint a pixmap
and catch it's own mousePressEvents.
Correct?
So I subclassed Node, loaded a rectangular image in the constructor, implemented boundingRect(), paint(), and mousePressEvent(). Compiles and runs ok.
Just one problem - I can't drag the rectangular image by the mouse!
Some qDebug statements in the relevant methods shows that the derived class
is receiving mousePressEvents, but only in the exact area where a Node (ball) would be (around 0,0 in my pixmap). It looks like I need to make another method in Node virtual to override the default area where a Node accepts mouse events.
I hope this is clear. The attached jpg shows a closeup.
The white circle is some sort of "ghost" node that will accept a mouse press
and allow me to drag the pixmap. I can't drag the pixmap anywhere else.
thanks,
-baj
Bookmarks