Results 1 to 7 of 7

Thread: Container item implementation

  1. #1
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Container item implementation

    Hi, I am working in Train Traffic Controller software project. My responsibility in this project is to develop the visual railroad GUI.

    We are implementing the project with Qt. By now I am using QGraphicsLinearLayout to hold my items. I am using the layout because I do not want to calculate coordinates of each item. So far I wrote item classes to add the layout. For instance SwitchItem class symbolizes railroad switch in real world. Each item class is responsible for its own painting and events. So far so good.
    Now I need a composite item that can contain two or more item. This class is going to be responsible for painting the items contained in it. I need this class because I have to put two or more items inside same layout cell. If I don' t put them in same cell I can' t use layout. See the image below.

    BlockSegmentItem and SignalItem inside same cell.

    Here is my compositeitem implementation.

    Qt Code:
    1. #include "compositeitem.h"
    2.  
    3. CompositeItem::CompositeItem(QString id,QList<FieldItem *> _children)
    4. {
    5. children = _children;
    6. }
    7.  
    8. CompositeItem::~CompositeItem()
    9. {
    10. }
    11.  
    12. QRectF CompositeItem::boundingRect() const
    13. {
    14. //Not carefully thinked about it
    15. return QRectF(QPointF(-50,-150),QSizeF(250,250));
    16. }
    17.  
    18. void CompositeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    19. {
    20. FieldItem *child;
    21. foreach(child,children)
    22. {
    23. child->paint(painter,option,widget);
    24. }
    25. }
    26.  
    27. QSizeF CompositeItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
    28. {
    29. QSizeF itsSize(0,0);
    30. FieldItem *child;
    31. foreach(child,children)
    32. {
    33. // if its size empty set first child size to itsSize
    34. if(itsSize.isEmpty())
    35. itsSize = child->sizeHint(Qt::PreferredSize);
    36. else
    37. {
    38. QSizeF childSize = child->sizeHint(Qt::PreferredSize);
    39. if(itsSize.width() < childSize.width())
    40. itsSize.setWidth(childSize.width());
    41. itsSize.setHeight(itsSize.height() + childSize.height());
    42. }
    43. }
    44. return itsSize;
    45. }
    46.  
    47. void CompositeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    48. {
    49. qDebug()<<"Test";
    50. }
    To copy to clipboard, switch view to plain text mode 

    This code works good with painting but when it comes to item events it is problematic. QGraphicsScene treats the composite item like a single item which is right for layout but not for events. Because each item has its own event implementation.(e.g. SignalItem has its special context menu event.)

    I have to handle item events seperately. Also I need a composite item implementation for the layout. How can I overcome this dilemma?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Container item implementation

    Check out this function:
    http://doc.qt.nokia.com/4.6/qgraphic...eneEventFilter

    This way, you can handle the events of the children in your composite item and make a distinction between them

  3. #3
    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: Container item implementation

    Instead of your class use QGraphicsWidget, place a layout inside it and place subitems into the layout. Then you can put the graphics widget into your main layout.
    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.


  4. #4
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Container item implementation

    Due to advice I changed boundingRect code.
    I think this is a neat idea to unite child items boundingRects.

    Qt Code:
    1. QRectF CompositeItem::boundingRect() const
    2. {
    3. FieldItem *child;
    4. QRectF rect(0,0,0,0);
    5. foreach(child,children)
    6. {
    7. rect = rect.united(child->boundingRect());
    8. }
    9. return rect;
    10. }
    To copy to clipboard, switch view to plain text mode 
    But this time my composite item can' t catch events like contextmenuevent and mousepress event.
    What may cause this?

  5. #5
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Container item implementation

    @wysota

    If I've understood you corretly. You say "Change your code like this"

    Qt Code:
    1. class CompositeItem : public QGraphicsLinearLayout
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. CompositeItem:CompositeItem(QString id, QList<FieldItem *> _children)
    2. {
    3. children = _children;
    4. FieldItem *child;
    5. foreach(child,children)
    6. {
    7. addItem(child);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Am I right?

  6. #6
    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: Container item implementation

    The overall reason is that there are no child items in your item, you have a single item which is just drawing many items. You implemented boundingRect()... but did you also implement shape()? I don't really see the point of what you are doing as QGraphicsWidget and/or QGraphicsItemGroup already provide everything you want and need.
    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.


  7. #7
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Container item implementation

    I am using layouts and as far as I know QGraphicsItemGroup can not be added to a layout. That' s why I am using QGraphicsWidget.
    Last edited by onurozcelik; 12th May 2010 at 12:00. Reason: spelling corrections

Similar Threads

  1. Container plugins
    By Benne Gesserit in forum Qt Tools
    Replies: 4
    Last Post: 20th November 2008, 23:43
  2. Scrollbar in the container
    By anju123 in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 07:44
  3. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 11:20
  4. Problem with Container Extension
    By tarod in forum Qt Tools
    Replies: 2
    Last Post: 12th November 2007, 16:20
  5. Container Extensions
    By mariok in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 11:06

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.