hello,

here are some code, that make it possible, that you can add a GraphicsScene item,
move and resize it on a view.
but when you try to resize it, the item jumps to location 0,0 on view.

so my question, how is it possible that follows old position?
and dont jump by each resize.

thank you

Qt Code:
  1. #include <QApplication>
  2. #include <QtGui>
  3. #include <QGraphicsSceneMouseEvent>
  4. #include <QGraphicsItem>
  5. #include <QGraphicsView>
  6. #include <QGraphicsWidget>
  7. #include <QStyleOptionGraphicsItem>
  8.  
  9. class MyResizeHandle : public QGraphicsRectItem
  10. {
  11. public:
  12. MyResizeHandle(Qt::Alignment alignment, QGraphicsItem* parent = 0)
  13. : QGraphicsRectItem(parent), align(alignment)
  14. {
  15. setRect(0, 0, 5, 5);
  16. setBrush(Qt::black);
  17. }
  18.  
  19. protected:
  20. void mousePressEvent(QGraphicsSceneMouseEvent* event)
  21. {
  22. offset = event->pos();
  23. }
  24.  
  25. void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
  26. {
  27. QGraphicsItem* parent = parentItem();
  28. if (parent && parent->isWidget())
  29. {
  30. QGraphicsWidget* parentWidget = static_cast<QGraphicsWidget*>(parent);
  31. QRectF parentGeometry = parentWidget->geometry();
  32.  
  33. QRectF geometry = boundingRect();
  34. geometry.moveTo(parentWidget->mapToParent(mapToParent(event->pos())) - offset);
  35.  
  36. if (align & Qt::AlignLeft)
  37. parentGeometry.setLeft(geometry.left());
  38. if (align & Qt::AlignRight)
  39. parentGeometry.setRight(geometry.right());
  40. if (align & Qt::AlignTop)
  41. parentGeometry.setTop(geometry.top());
  42. if (align & Qt::AlignBottom)
  43. parentGeometry.setBottom(geometry.bottom());
  44.  
  45. parentWidget->setGeometry(parentGeometry);
  46. }
  47. }
  48.  
  49. private:
  50. QPointF offset;
  51. Qt::Alignment align;
  52. };
  53.  
  54. class MyGraphicsWidget : public QGraphicsWidget
  55. {
  56. public:
  57. MyGraphicsWidget(QGraphicsItem* parent = 0) : QGraphicsWidget(parent)
  58. {
  59. //setFlag(ItemIsMovable);
  60. setFlags(QGraphicsItem::ItemIsSelectable |
  61. QGraphicsItem::ItemIsMovable |
  62. QGraphicsItem::ItemSendsGeometryChanges);
  63.  
  64. createHandle(Qt::AlignTop);
  65. createHandle(Qt::AlignBottom);
  66. createHandle(Qt::AlignLeft);
  67. createHandle(Qt::AlignRight);
  68.  
  69. createHandle(Qt::AlignTop | Qt::AlignLeft);
  70. createHandle(Qt::AlignTop | Qt::AlignRight);
  71. createHandle(Qt::AlignBottom | Qt::AlignLeft);
  72. createHandle(Qt::AlignBottom | Qt::AlignRight);
  73. }
  74.  
  75. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  76. {
  77. Q_UNUSED(widget);
  78. painter->drawRect(option->rect);
  79. }
  80.  
  81. void setGeometry(const QRectF& rect)
  82. {
  83. QGraphicsWidget::setGeometry(rect);
  84.  
  85. QHashIterator<Qt::Alignment, MyResizeHandle*> it(resizeHandles);
  86. while (it.hasNext())
  87. {
  88. it.next();
  89. adjustHandle(it.key(), it.value());
  90. }
  91. }
  92.  
  93. private:
  94. void createHandle(Qt::Alignment align)
  95. {
  96. resizeHandles[align] = new MyResizeHandle(align, this);
  97. }
  98.  
  99. void adjustHandle(Qt::Alignment align, MyResizeHandle* handle)
  100. {
  101. QRectF bounds = boundingRect();
  102. QRectF handleBounds = handle->boundingRect();
  103.  
  104. handleBounds.moveCenter(bounds.center());
  105. if (align & Qt::AlignLeft)
  106. handleBounds.moveLeft(bounds.left());
  107. if (align & Qt::AlignRight)
  108. handleBounds.moveRight(bounds.right());
  109. if (align & Qt::AlignTop)
  110. handleBounds.moveTop(bounds.top());
  111. if (align & Qt::AlignBottom)
  112. handleBounds.moveBottom(bounds.bottom());
  113.  
  114. handle->setPos(handleBounds.topLeft());
  115. }
  116.  
  117. QHash<Qt::Alignment, MyResizeHandle*> resizeHandles;
  118.  
  119.  
  120.  
  121. QVariant itemChange(GraphicsItemChange change,
  122. const QVariant &value)
  123. {
  124. if (change == ItemPositionChange && scene()) {
  125. QPointF newPos = value.toPointF();
  126. if(QApplication::mouseButtons() == Qt::LeftButton &&
  127. qobject_cast<Scene*> (scene())){
  128. Scene* customScene = qobject_cast<Scene*> (scene());
  129. int gridSize = customScene->getGridSize();
  130. qreal xV = round(newPos.x()/gridSize)*gridSize;
  131. qreal yV = round(newPos.y()/gridSize)*gridSize;
  132. return QPointF(xV, yV);
  133. }
  134. else
  135. return newPos;
  136. }
  137. else
  138. return QGraphicsItem::itemChange(change, value);
  139. }
  140. };
To copy to clipboard, switch view to plain text mode