Results 1 to 2 of 2

Thread: Change QGraphicsScene Rect by dragging QGraphicItems

  1. #1
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Change QGraphicsScene Rect by dragging QGraphicItems

    Hello everyone,

    I've a QGraphicsView and a QGraphicsScene, within this scene are QGraphicsitems.
    It sounds quiet trivial but I couldn't manage to change the "size" of the scene by dragging an item at the border of the view (->viewport() ).
    I've set the size of the scene because I want that the Items are inserted where a click happens. Otherwise it would insert the first item in the middle...

    I tried to override the QGraphicsItem::mouseMoveEvent function and then I tried to check if the scene intersects the item (that the item is at the border and not the whole item is shown... , the view should then add some scrollbars to it (like the default behavior) and should expand the whole thing.


    thank you for your help

    airglide

  2. #2
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change QGraphicsScene Rect by dragging QGraphicItems

    I've solved the problem more or less, I've to optimize it but the code looks like this:
    The class Pixmap inherits from QGraphicsPixmapItem (QGraphicsItem)
    Qt Code:
    1. void PixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. QRectF theRect = QRectF(scenePos(), boundingRect().size());
    4. QRectF intersected = QRectF(theRect.intersected(scene()->sceneRect()));
    5. if(intersected != theRect)
    6. {
    7. //the item is outside of sceneRect
    8. if(theRect.top() != intersected.top())
    9. {
    10. //is at bottom
    11. if(theRect.right() != intersected.right())
    12. {
    13. //is right
    14. qDebug() << "top-right";
    15. QRectF mRect = scene()->sceneRect();
    16. mRect.adjust(0,-50,50,0);
    17. scene()->setSceneRect(mRect);
    18. }else{
    19. if(theRect.left() != intersected.left())
    20. {
    21. //is left
    22. qDebug() << "top-left";
    23. QRectF mRect = scene()->sceneRect();
    24. mRect.adjust(-50,-50,0,0);
    25. scene()->setSceneRect(mRect);
    26. }else{
    27. //only top
    28. qDebug() << "top";
    29. QRectF mRect = scene()->sceneRect();
    30. mRect.adjust(0,-50,0,0);
    31. scene()->setSceneRect(mRect);
    32. }
    33. }
    34. }else{
    35.  
    36. if(theRect.bottom() != intersected.bottom())
    37. {
    38. //is at bottom
    39. if(theRect.right() != intersected.right())
    40. {
    41. //is right
    42. qDebug() << "bottom-right";
    43. QRectF mRect = scene()->sceneRect();
    44. mRect.adjust(0,0,50,50);
    45. scene()->setSceneRect(mRect);
    46. }else{
    47. if(theRect.left() != intersected.left())
    48. {
    49. //is left
    50. qDebug() << "bottom-left";
    51. QRectF mRect = scene()->sceneRect();
    52. mRect.adjust(-50,0,0,50);
    53. scene()->setSceneRect(mRect);
    54. }else{
    55. //only bottom
    56. qDebug() << "bottom";
    57. QRectF mRect = scene()->sceneRect();
    58. mRect.adjust(0,0,0,50);
    59. scene()->setSceneRect(mRect);
    60. }
    61. }
    62. }else{
    63. if(theRect.right() != intersected.right())
    64. {
    65. qDebug() << "right";
    66. //is right
    67. QRectF mRect = scene()->sceneRect();
    68. mRect.adjust(0,0,50,0);
    69. scene()->setSceneRect(mRect);
    70. }else{
    71. if(theRect.left() != intersected.left())
    72. {
    73. qDebug() << "left";
    74. //is left
    75. QRectF mRect = scene()->sceneRect();
    76. mRect.adjust(-50,0,0,0);
    77. scene()->setSceneRect(mRect);
    78. }}
    79.  
    80. }
    81. }
    82. }else{
    83. qDebug() << "no intersection";
    84. }
    85. QGraphicsPixmapItem::mouseMoveEvent(event);
    86. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QDrag - Change the image display while dragging
    By chkong in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2011, 21:30
  2. Replies: 2
    Last Post: 1st October 2011, 09:11
  3. Replies: 7
    Last Post: 12th March 2010, 12:40
  4. Replies: 4
    Last Post: 11th July 2007, 04:21
  5. Moving multiple QGraphicItems
    By StefanHirche in forum Newbie
    Replies: 2
    Last Post: 11th January 2007, 21:27

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.