Results 1 to 2 of 2

Thread: change rect of parent when child's boundingrect is changed or new child is added

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default change rect of parent when child's boundingrect is changed or new child is added

    Hi,

    I want to implement grid using qgraphicsrectitem in which each cell can has multiple child and grandchildren. i.e. I have outer rectangle R1 with rect as R(0,0,300,300). This rect will have 3 child qgraphicsrectitem as RC1(0,0,300,100), RC2(0,100,300,100), RC3(0,200,300,100)
    when I will add 2 children in RC1 as RGC1(0,0,300,75) and RGC1(0,75,300,75), it should change bounding rect as belows.

    RGC1 - (0,0,300,75)
    RGC2 - (0,75,300,75)
    RC1 - (0,0,300,150),
    RC2 - (0,150,300,100)
    RC3 - (0,250,300,100)
    R - (0,0,300,250)

    This means basically, I want to implement that if child is added or resized all its parents and super parents to get re-shaped so that all children remain inside the bounding rect of parent and will not be out of border.

    Any idea for this

    Manish

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: change rect of parent when child's boundingrect is changed or new child is added

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class GraphicsRectItem : public QGraphicsRectItem
    4. {
    5. public:
    6. explicit GraphicsRectItem(const QString & name, QGraphicsItem * parent = 0)
    7. , text(new QGraphicsSimpleTextItem(this))
    8. {
    9. setData(0, name);
    10. }
    11.  
    12. void setSize(const QRect & r)
    13. {
    14. setRect(r);
    15.  
    16. GraphicsRectItem * parent = dynamic_cast<GraphicsRectItem *>(parentItem());
    17.  
    18. if(parent)
    19. parent->adjust();
    20.  
    21. // Showing rect size in text
    22. text->setText(QString("%5-{%1,%2,%3,%4}").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height()).arg(data(0).toString()));
    23. text->setPos(rect().center());
    24. text->moveBy(-text->boundingRect().width()/2, -text->boundingRect().height()/2);
    25. }
    26.  
    27. private:
    28. void adjust()
    29. {
    30. QRect rect;
    31.  
    32. foreach(const QGraphicsItem * child, childItems())
    33. if(dynamic_cast<const GraphicsRectItem *>(child))
    34. rect = rect.united(child->boundingRect().toRect());
    35.  
    36. setSize(rect);
    37. }
    38.  
    39. };
    40.  
    41. int main(int argc, char *argv[])
    42. {
    43. QApplication app(argc, argv);
    44.  
    45. GraphicsRectItem * R = new GraphicsRectItem("R");
    46.  
    47. GraphicsRectItem * RC1 = new GraphicsRectItem("RC1", R);
    48. GraphicsRectItem * RC2 = new GraphicsRectItem("RC2", R);
    49. GraphicsRectItem * RC3 = new GraphicsRectItem("RC3", R);
    50.  
    51. GraphicsRectItem * RGC1 = new GraphicsRectItem("RGC1", RC1);
    52. GraphicsRectItem * RGC2 = new GraphicsRectItem("RGC2", RC1);
    53.  
    54. RGC1->setSize(QRect(0,0,300,75));
    55. RGC2->setSize(QRect(0,75,300,75));
    56.  
    57. RC2->setSize(QRect(0,150,300,100));
    58. RC3->setSize(QRect(0,250,300,100));
    59.  
    60. scene.addItem(R);
    61.  
    62. view.setScene(&scene);
    63. view.show();
    64.  
    65. return app.exec();
    66. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 7
    Last Post: 17th July 2015, 13:58
  2. Replies: 2
    Last Post: 25th February 2015, 18:18
  3. parent deletion.. what happens to child?
    By mahesh113 in forum Newbie
    Replies: 2
    Last Post: 1st June 2012, 12:13
  4. Parent Child for window
    By febil in forum Qt Programming
    Replies: 6
    Last Post: 1st April 2009, 05:00
  5. QTreeView PARENT & CHILD
    By zeeb100 in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 14:53

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.