Results 1 to 5 of 5

Thread: Typical Design Issue

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Typical Design Issue

    Hello forum,


    I would like to discuss some design issues which may not related directly to Qt, but in some way it is because i am using the graphics view network to get around with this.

    I have a class ArrowGraphicsItem (Subclass of QGraphicsItem) as follows:

    Qt Code:
    1. class ArrowGraphicsItem : public QGraphicsItem
    2. {
    3. public:
    4.  
    5. /**
    6.   * \param the source graphics item for the arrow
    7.   * \param the destination graphics item for the arrow, by default it is ZERO
    8.   */
    9. ArrowGraphicsItem(QGraphicsItem *sourceItem,
    10. QGraphicsItem *destinationItem = 0);
    11.  
    12. ~ArrowGraphicsItem();
    13.  
    14. enum { Type = UserType + UserTypesArrowGraphicsItem };
    15.  
    16. /**
    17.   * Retruns the type of this class.Necessary for all QGraphicsItem subclasses
    18.   * \return The type of this class
    19.   */
    20. int type() const;
    21.  
    22. /**
    23.   * The bounding rect for the arrow graphics item
    24.   * \return The bounding rect
    25.   */
    26. QRectF boundingRect() const;
    27.  
    28. ................................................................
    29.  
    30. ................................................................
    31.  
    32. protected:
    33.  
    34. virtual QPainterPath shape() const = 0;
    35.  
    36. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    To copy to clipboard, switch view to plain text mode 


    I have several subclasses which only over-rides the shape() function in their respective subclasses and paint functional definition is always grabbed from default implementation in the ArrowGraphicsItem.


    Is that a good design technique ?




    Regards
    Sajjad

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

    Default Re: Typical Design Issue

    If you paint all items the same way, it would seem all of them have the same shape. What is the point of reimplementing shape() then?
    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.


  3. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Typical Design Issue

    Yes the shape will be different in each of the sub-classes

  4. #4
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Typical Design Issue

    to wysota: Not necessarily. I also sometime use such a design for similar (and simple) items. Each item implements only the boundingRect() and shape() method.
    e.g.:
    Qt Code:
    1. void ArrowGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setPen(getPen());
    4. painter->strokePath(shape());
    5. // some other stuff (fill path, outline, etc.)
    6. }
    7.  
    8. QPainterPath ThinArrowGraphicsItem::shape() const
    9. {
    10. // define thin shape here ...
    11. }
    12.  
    13. QPainterPath WideArrowGraphicsItem::shape() const
    14. {
    15. // define wide shape here ...
    16. }
    To copy to clipboard, switch view to plain text mode 
    Is there any problem? I don't think it's so ugly design, is it?

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

    Default Re: Typical Design Issue

    Quote Originally Posted by sajis997 View Post
    Yes the shape will be different in each of the sub-classes
    Quote Originally Posted by Piskvorkar View Post
    to wysota: Not necessarily. I also sometime use such a design for similar (and simple) items. Each item implements only the boundingRect() and shape() method.
    e.g.:
    Qt Code:
    1. void ArrowGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setPen(getPen());
    4. painter->strokePath(shape());
    5. // some other stuff (fill path, outline, etc.)
    6. }
    7.  
    8. QPainterPath ThinArrowGraphicsItem::shape() const
    9. {
    10. // define thin shape here ...
    11. }
    12.  
    13. QPainterPath WideArrowGraphicsItem::shape() const
    14. {
    15. // define wide shape here ...
    16. }
    To copy to clipboard, switch view to plain text mode 
    Is there any problem? I don't think it's so ugly design, is it?
    How about just having one class with a setShape() method then? What's the point of subclassing?
    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. Non-typical and Non-linear wizard
    By kornicameister in forum Qt Programming
    Replies: 4
    Last Post: 30th December 2010, 15:26
  2. Replies: 3
    Last Post: 5th October 2008, 23:41
  3. QSystemTrayIcon as "main window" design issue
    By nooky59 in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 13:15
  4. Dialog and code design issue
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2006, 17:54
  5. A Design Issue...
    By nupul in forum Qt Programming
    Replies: 6
    Last Post: 4th May 2006, 17:41

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.