Results 1 to 8 of 8

Thread: How to create a QGraphicsItem compound?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to create a QGraphicsItem compound?

    Hello!

    I need to create a slightly complex item for a graphics scene. In particular, imagine a circle that is flying across the screen. The circle has a position, of course, and there is an arrow attached to it, which indicates its velocity. I would like to drag the circle with the mouse to change its position, and I would like to be able to drag the tip of the arrow in order to change the velocity. What is the best strategy to create this item?

    I could for example just subclass QGraphicsEllipseItem which gives me the circle, and hack the rest into it by drawing the error in the paint() method, and checking in the mouse event methods if the mouse is on the arrow or on the circle. This way seems a bit hack and I'm sure there is a cleaner way.

    I could also subclass QGraphicsEllipseItem and QGraphicsLineItem and add the circle and the arrow as separate items to the scene and connect the arrow to the circle using a parent child relationship. I don't now how I would go about keeping things on one object in the end, one object that has a position and a velocity. Do I somehow find the parent of the arrow and write the velocity into the parent?

    Then there are also item groups, but I don't see any advantage of using them at the moment.

    Can anyone give pointers please?


    Thanks
    Cruz

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to create a QGraphicsItem compound?

    Do I somehow find the parent of the arrow and write the velocity into the parent?
    It's seems as a good approach.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a QGraphicsItem compound?

    Quote Originally Posted by Santosh Reddy View Post
    It's seems as a good approach.
    Except that the child wouldn't know the type of the parent other than that it's a QGraphicsItem, which means I would have to perform an ugly cast to be able to access the necessary fields.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to create a QGraphicsItem compound?

    You could use QGraphicsItem::setData() and QGraphicsItem::data() to store any custom data
    Qt Code:
    1. void QGraphicsItem::setData(int key, const QVariant & value)
    2. QVariant QGraphicsItem::data(int key) const
    To copy to clipboard, switch view to plain text mode 

    That way you can avoid casting
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a QGraphicsItem compound?

    Quote Originally Posted by Santosh Reddy View Post
    You could use QGraphicsItem::setData() and QGraphicsItem::data() to store any custom data
    Qt Code:
    1. void QGraphicsItem::setData(int key, const QVariant & value)
    2. QVariant QGraphicsItem::data(int key) const
    To copy to clipboard, switch view to plain text mode 

    That way you can avoid casting
    True, but then I always have to access the velocity of the object from the data() field and I lose the ability to use the object as a vector with standard mathematical operations. Somehow I feel it would be best if the circle and its arrow were in one object. I just don't know how to route the mouse events from the arrow tip to the circle.


    Added after 9 minutes:


    Also, I would have to notify the parent somehow that the data have changed. How do I do that?
    Last edited by Cruz; 22nd April 2015 at 17:03.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a QGraphicsItem compound?

    Quote Originally Posted by Cruz View Post
    Except that the child wouldn't know the type of the parent other than that it's a QGraphicsItem, which means I would have to perform an ugly cast to be able to access the necessary fields.
    Nothing keeps you from using your own constructor on the arrow item that takes a pointer to your master item as its parent.

    Qt Code:
    1. class ArrowItem : public QGraphicsItem // or whatever you want to use as a base class
    2. {
    3. public:
    4. explicit ArrowItem(CircleItem *parent = 0)
    5. : QGraphicsItem(parent)
    6. , m_circle(parent)
    7. {
    8. }
    9.  
    10. private;
    11. Circleitem *const m_circle;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a QGraphicsItem compound?

    Yes this is a good suggestion. I ended up subclassing them from QObject so that I can connect them with signals and slots, which is very similar to what you suggested really.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a QGraphicsItem compound?

    Well, you have a one-on-one relation ship between the circle item and the arrow, so the arrow could just call an update method on the parent.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 23rd March 2013, 13:50
  2. converting X-Window compound strings
    By Wurgl in forum General Programming
    Replies: 1
    Last Post: 10th March 2013, 08:32
  3. Creating compound paths
    By bassPenguin in forum Qt Programming
    Replies: 3
    Last Post: 28th March 2012, 11:57
  4. Replies: 12
    Last Post: 24th June 2011, 09:22
  5. Replies: 7
    Last Post: 29th November 2010, 19:20

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.