Results 1 to 5 of 5

Thread: QGraphicsItem inheritence

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsItem inheritence

    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:

    Qt Code:
    1. class Node : public QGraphicsItem
    2. {
    3. public:
    4. Node(GraphWidget *graphWidget);
    5.  
    6. void addEdge(Edge *edge);
    7. QList<Edge *> edges() const;
    8.  
    9. enum { Type = UserType + 1 };
    10. int type() const { return Type; }
    11.  
    12. void calculateForces();
    13. bool advance();
    14.  
    15. virtual QRectF boundingRect() const;
    16. QPainterPath shape() const;
    17. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    18.  
    19. protected:
    20. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    21.  
    22. virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    23. virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    24.  
    25. QList<Edge *> edgeList;
    26. QPointF newPos;
    27. GraphWidget *graph;
    28.  
    29. private:
    30.  
    31. };
    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
    Attached Images Attached Images

Similar Threads

  1. Replies: 2
    Last Post: 25th March 2011, 09:18
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. Replies: 1
    Last Post: 25th February 2009, 00:34
  4. QGraphicsItem does not update
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2009, 14:44
  5. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 16:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.