Results 1 to 14 of 14

Thread: Destructor overriding

Threaded View

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

    Default Re: Destructor overriding

    Ok, here is a small framework. Try completing it so that it works properly.

    Qt Code:
    1. #include <QtGui>
    2. #if QT_VERSION >= 0x050000
    3. #include <QtWidgets>
    4. #endif
    5.  
    6. class Arrow;
    7.  
    8. class RotatingItem : public QGraphicsRectItem {
    9. public:
    10. RotatingItem(const QRectF &rect, QGraphicsItem *parent = 0) : QGraphicsRectItem(rect, parent){
    11. setFlag(ItemSendsGeometryChanges, true);
    12.  
    13. }
    14. protected:
    15. void wheelEvent(QGraphicsSceneWheelEvent *event) {
    16. if(event->modifiers() & Qt::ShiftModifier) {
    17. setScale(scale()* (event->delta() > 0 ? 1.1 : 0.9));
    18. } else {
    19. setRotation(rotation()+(event->delta()/15.0));
    20.  
    21. }
    22. }
    23. QVariant itemChange(GraphicsItemChange change, const QVariant &value) {
    24. if(change == ItemPositionHasChanged && scene()) {
    25. foreach(Arrow *a, m_arrowStartsHere) updateStartArrowPosition(a);
    26. foreach(Arrow *a, m_arrowEndsHere) updateEndArrowPosition(a);
    27.  
    28. }
    29. return QGraphicsRectItem::itemChange(change, value);
    30. }
    31. void updateStartArrowPosition(Arrow *);
    32.  
    33. void updateEndArrowPosition(Arrow *);
    34.  
    35. private:
    36. QList<Arrow*> m_arrowStartsHere;
    37. QList<Arrow*> m_arrowEndsHere;
    38.  
    39.  
    40. friend class Arrow;
    41. };
    42.  
    43. class Arrow : public QGraphicsLineItem {
    44. public:
    45. Arrow(RotatingItem *st, RotatingItem *en) :QGraphicsLineItem(st) {
    46. startItem = st;
    47. endItem = en;
    48. QPointF end = mapFromItem(endItem, endItem->boundingRect().center());
    49. setLine(0,0, end.x(), end.y());
    50. startItem->m_arrowStartsHere << this;
    51. endItem->m_arrowEndsHere << this;
    52.  
    53. m_text = new QGraphicsSimpleTextItem(this);
    54. QFont f;
    55. f.setPointSize(8);
    56. m_text->setFont(f);
    57. updateArrowContent();
    58. }
    59. RotatingItem *start() const { return startItem; }
    60. RotatingItem *end() const { return endItem; }
    61. void updateArrowContent() {
    62. m_text->setPos(line().pointAt(0.5));
    63. m_text->setText(QString::number(line().length()));
    64. }
    65.  
    66. protected:
    67. RotatingItem *startItem, *endItem;
    68. };
    69.  
    70. class GraphicsView : public QGraphicsView {
    71. public:
    72. GraphicsView(QWidget *parent = 0) : QGraphicsView(parent){}
    73. protected:
    74. void paintEvent(QPaintEvent *event) {
    75. QGraphicsView::paintEvent(event);
    76. QPainter p(viewport());
    77. p.drawText(viewport()->rect().adjusted(10,10,-10,-10), "Drag item to move\nWheel to rotate\nSHIFT+wheel to scale");
    78. }
    79. };
    80.  
    81. int main(int argc, char **argv) {
    82. QApplication app(argc, argv);
    83. GraphicsView view;
    84. view.setScene(&scene);
    85. RotatingItem *item1 = new RotatingItem(QRect(-50, -50, 100, 100));
    86. RotatingItem *item2 = new RotatingItem(QRect(-50, -50, 100, 100));
    87. scene.addItem(item1);
    88. scene.addItem(item2);
    89. item1->setPos(100,100);
    90. item2->setPos(200,200);
    91. item1->setFlag(QGraphicsItem::ItemIsMovable);
    92. item2->setFlag(QGraphicsItem::ItemIsMovable);
    93. item1->setBrush(Qt::red);
    94. item2->setBrush(Qt::yellow);
    95. Arrow *arr = new Arrow(item1, item2);
    96. QPen p;
    97. p.setWidth(4);
    98. p.setColor(Qt::blue);
    99. arr->setPen(p);
    100. item1->setZValue(10);
    101. item2->setZValue(0);
    102. arr->setZValue(10);
    103. view.setRenderHint(QPainter::Antialiasing);
    104. view.resize(400,400);
    105. view.show();
    106. return app.exec();
    107. }
    108.  
    109.  
    110. void RotatingItem::updateStartArrowPosition(Arrow *arrow)
    111. {
    112. QLineF l = arrow->line();
    113. l.setP2(arrow->end()->mapToItem(arrow, arrow->end()->boundingRect().center()));
    114. arrow->setLine(l);
    115. arrow->updateArrowContent();
    116. }
    117.  
    118. void RotatingItem::updateEndArrowPosition(Arrow *arrow)
    119. {
    120. QLineF l = arrow->line();
    121. l.setP2(mapToItem(arrow, boundingRect().center()));
    122. arrow->setLine(l);
    123. arrow->updateArrowContent();
    124. }
    To copy to clipboard, switch view to plain text mode 

    Problems to solve:
    1. make sure the line sticks to its items when rotation and scale of any of them changes
    2. make sure the length reported by the line is consistent when the line's starting item is scaled
    3. make sure the width of the line doesn't depend on the scale of the red item or that it depends on the scale of both the red and the yellow item (the choice is yours)
    4. make it possible for the yellow item to be over the red item (in terms of Z-value) and the line to be on top of the yellow item (in other words make it possible so that the line is always on top of the other items regardless of the stacking order of the items themselves)
    Last edited by wysota; 1st March 2013 at 11:25.
    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. Overriding Proxies
    By jdgrant in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2011, 01:23
  2. Overriding global new
    By branko in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2010, 16:10
  3. overriding QListWidget advice
    By codebehind in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2010, 22:39
  4. Overriding drawRubberBand()
    By andrew.nguyen in forum Qwt
    Replies: 3
    Last Post: 21st April 2010, 06:58
  5. Overriding shortcuts for QGraphicsItems
    By pherthyl in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2008, 22:47

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.