Results 1 to 2 of 2

Thread: what's the better practice?

  1. #1
    Join Date
    Mar 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default what's the better practice?

    I'm working on an example in the QT Documentation,Diagram Scene Example.

    In the example, the DiagramItem class is designed for the items to be drawn in to the scene. Different kinds of items(Conditional,Process,Input/Output,Text) exists and these are enumerated in the DiagramItem class and are distinct with switch... But I don't know if it's the best way.Wouldn't it be better if the Conditional,Process,IO,Text items inherits from the DiagramItem class.
    here's the code and i want to hear you:
    Qt Code:
    1. #ifndef DIAGRAMITEM_H
    2. #define DIAGRAMITEM_H
    3.  
    4. #include <QGraphicsPixmapItem>
    5. #include <QList>
    6.  
    7. class QPixmap;
    8. class QTextEdit;
    9. class QMenu;
    10. class QPainter;
    11. class QWidget;
    12. class QPolygonF;
    13. class Arrow;
    14.  
    15. class DiagramItem : public QGraphicsPolygonItem
    16. {
    17. public:
    18. enum { Type = UserType + 15 };
    19. enum DiagramType { Step, Conditional, StartEnd, Io };
    20.  
    21. DiagramItem(DiagramType diagramType, QMenu *contextMenu,
    22. QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    23.  
    24. void removeArrow(Arrow *arrow);
    25. void removeArrows();
    26. DiagramType diagramType() const
    27. { return myDiagramType; }
    28. QPolygonF polygon() const
    29. { return myPolygon; }
    30. void addArrow(Arrow *arrow);
    31. QPixmap image() const;
    32. int type() const
    33. { return Type;}
    34.  
    35. protected:
    36. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
    37. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    38.  
    39. private:
    40. DiagramType myDiagramType;
    41. QPolygonF myPolygon;
    42. QMenu *myContextMenu;
    43. QList<Arrow *> arrows;
    44. };
    45.  
    46. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "diagramitem.h"
    4. #include "arrow.h"
    5.  
    6. DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
    7. QGraphicsItem *parent, QGraphicsScene *scene)
    8. : QGraphicsPolygonItem(parent, scene)
    9. {
    10. myDiagramType = diagramType;
    11. myContextMenu = contextMenu;
    12.  
    13. switch (myDiagramType) {
    14. case StartEnd:
    15. path.moveTo(200, 50);
    16. path.arcTo(150, 0, 50, 50, 0, 90);
    17. path.arcTo(50, 0, 50, 50, 90, 90);
    18. path.arcTo(50, 50, 50, 50, 180, 90);
    19. path.arcTo(150, 50, 50, 50, 270, 90);
    20. path.lineTo(200, 25);
    21. myPolygon = path.toFillPolygon();
    22. break;
    23. case Conditional:
    24. myPolygon << QPointF(-100, 0) << QPointF(0, 100)
    25. << QPointF(100, 0) << QPointF(0, -100)
    26. << QPointF(-100, 0);
    27. break;
    28. case Step:
    29. myPolygon << QPointF(-100, -100) << QPointF(100, -100)
    30. << QPointF(100, 100) << QPointF(-100, 100)
    31. << QPointF(-100, -100);
    32. break;
    33. default:
    34. myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
    35. << QPointF(120, 80) << QPointF(70, -80)
    36. << QPointF(-120, -80);
    37. break;
    38. }
    39. setPolygon(myPolygon);
    40. setFlag(QGraphicsItem::ItemIsMovable, true);
    41. setFlag(QGraphicsItem::ItemIsSelectable, true);
    42. }
    43.  
    44. void DiagramItem::removeArrow(Arrow *arrow)
    45. {
    46. int index = arrows.indexOf(arrow);
    47.  
    48. if (index != -1)
    49. arrows.removeAt(index);
    50. }
    51.  
    52. void DiagramItem::removeArrows()
    53. {
    54. foreach (Arrow *arrow, arrows) {
    55. arrow->startItem()->removeArrow(arrow);
    56. arrow->endItem()->removeArrow(arrow);
    57. scene()->removeItem(arrow);
    58. delete arrow;
    59. }
    60. }
    61.  
    62. void DiagramItem::addArrow(Arrow *arrow)
    63. {
    64. arrows.append(arrow);
    65. }
    66.  
    67. QPixmap DiagramItem::image() const
    68. {
    69. QPixmap pixmap(250, 250);
    70. pixmap.fill(Qt::transparent);
    71. QPainter painter(&pixmap);
    72. painter.setPen(QPen(Qt::black, 8));
    73. painter.translate(125, 125);
    74. painter.drawPolyline(myPolygon);
    75.  
    76. return pixmap;
    77. }
    78.  
    79. void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    80. {
    81. scene()->clearSelection();
    82. setSelected(true);
    83. myContextMenu->exec(event->screenPos());
    84. }
    85.  
    86. QVariant DiagramItem::itemChange(GraphicsItemChange change,
    87. const QVariant &value)
    88. {
    89. if (change == QGraphicsItem::ItemPositionChange) {
    90. foreach (Arrow *arrow, arrows) {
    91. arrow->updatePosition();
    92. }
    93. }
    94.  
    95. return value;
    96. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: what's the better practice?

    In general it depends. In most cases it'd be better to make each type a separate item class inheriting a common base.

  3. The following user says thank you to wysota for this useful post:

    xyzt (22nd March 2008)

Similar Threads

  1. Best Practice - delete pointer QAbstractButton in QMessageBox
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 12:43
  2. Best Practice on single source Qt Desktop/Qtopia Core/QPE application
    By izico in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 31st March 2007, 07:08
  3. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.