Results 1 to 4 of 4

Thread: QGraphicsView - How to add item that is composed of more shapes

  1. #1
    Join Date
    Apr 2012
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsView - How to add item that is composed of more shapes

    I have to add one component to QGraphicsView which is composed of more different shapes/items. Each component's view is defined by ordered list of different shapes:
    1. Rectangle (0,0,100,100) -gray
    2. Rectangle (5,5, 70,70) - yellow
    3. Text (15,15) - black
    comp_ex.png

    How can i compose item like that and add it to QGraphicsView?

    I already tried with QGraphicsPathItem but it doesn't allows composition like this and more colors and so on..

  2. #2
    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: QGraphicsView - How to add item that is composed of more shapes

    QGraphicsItemGroup

    Cheers,
    _

  3. #3
    Join Date
    Apr 2012
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView - How to add item that is composed of more shapes

    Answer to my first question was reimplementing all classes derived from QGraphicsPathItem to QGraphicsItem. Then, in overriden paint method from QGraphicsItem class, I added multiple draw methods (drawRect, drawText, etc.) and everything worked fine.

    But now, I have another problem. Next step in my application is adding Pins to Component.

    Quote Originally Posted by anda_skoa View Post
    QGraphicsItemGroup

    Cheers,
    _
    I've used QGraphicsPathItem as suggested in post above, but it also have some weird behaviour. Here is screenshot of QGraphicsItemGroup with 3 added items - 1 component and 2 pins.
    comppin.PNG

    All of them (every item in group) should be moveable as whole composition, but every single item that was added to group should be also selectable.
    Also, all the empty space that is rounded by rectangle line when group is selected shouldn't trigger any move or select action.
    One more problem: it's not possible to select randomly items in group. Once the item is selected, it's necessary to click somewhere out of bounding (imaginary) group rectangle, and then other item can be selected again.

    Here's code:
    Qt Code:
    1. group->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    2. scene->addItem(group);
    3. group->addToGroup(item);
    4. group->addToGroup(pin);
    To copy to clipboard, switch view to plain text mode 


    Added after 36 minutes:


    I'll answer myself:
    All I needed to do is to set parent item to my pin (pin->setParent(item)) and set ItemIsMovable flag to false. Without even creating a QGraphicsItemGroup object.
    Last edited by juracist; 11th May 2014 at 14:15.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView - How to add item that is composed of more shapes

    Quote Originally Posted by juracist View Post
    Answer to my first question was reimplementing all classes derived from QGraphicsPathItem to QGraphicsItem. Then, in overriden paint method from QGraphicsItem class, I added multiple draw methods (drawRect, drawText, etc.) and everything worked fine.
    If you have an item that is composed of other items then you should do exactly that -- use the parent-child relationship to compose the item from simple shapes. Otherwise you will be constantly modifying your implementation and adding more and more code making the class more and more complex while all you need is a proper object composition.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 22nd August 2013, 16:09
  2. Replies: 2
    Last Post: 25th June 2012, 10:54
  3. GUI entirely composed of qgraphicsproxywidgets
    By sabonis in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2012, 11:12
  4. How to fix an Item on QgraphicsView hole.
    By Zikoel in forum Qt Programming
    Replies: 1
    Last Post: 18th September 2011, 19:49
  5. QGraphicsView and item focus
    By Micawber in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2007, 20:36

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