I have used this code to implement resizing in my QGraphicsRectItem and QGraphicsEllipseItem, since both Rect and Ellipse have implemented function SetRect(qreal x, qreal y, qreal width, qreal height) it was not a problem, but with QGraphicsPolygonItem the nearest function is setPolygon() but it only takes a QPolygonf& Polygon as parameter which doesn't work with my code of sizeGripItem

mainwindow.cpp
Qt Code:
  1. class RectResizer : public SizeGripItem::Resizer // resize class for rectagle
  2. {
  3. public:
  4. virtual void operator()(QGraphicsItem* item, const QRectF& rect)
  5. {
  6. QGraphicsRectItem* rectItem =
  7. dynamic_cast<QGraphicsRectItem*>(item);
  8.  
  9. if (rectItem)
  10. {
  11. rectItem->setRect(rect);
  12. }
  13. }
  14. };
  15.  
  16. class EllipseResizer : public SizeGripItem::Resizer //resize class for ellipse
  17. {
  18. public:
  19. virtual void operator()(QGraphicsItem* item, const QRectF& rect)
  20. {
  21. QGraphicsEllipseItem* ellipseItem =
  22. dynamic_cast<QGraphicsEllipseItem*>(item);
  23.  
  24. if (ellipseItem)
  25. {
  26. ellipseItem->setRect(rect);
  27. }
  28. }
  29. };
  30.  
  31. class PolygonResizer : public SizeGripItem::Resizer // resize class for polygon
  32. {
  33. public:
  34. virtual void operator()(QGraphicsItem* item, const QRectF& rect)
  35. {
  36. QGraphicsPolygonItem* polygonItem =
  37. dynamic_cast<QGraphicsPolygonItem*>(item);
  38.  
  39. if (polygonItem)
  40. {
  41. polygonItem->setPolygon(rect);
  42. } //^ doesn't work when i attempt to resize shape, it changes to rectangle
  43. }
  44. };
To copy to clipboard, switch view to plain text mode 

sizegripitem.cpp
Qt Code:
  1. #include "sizegripitem.h"
  2. #include <QBrush>
  3.  
  4. SizeGripItem::HandleItem::HandleItem(int positionFlags, SizeGripItem* parent)
  5. : QGraphicsRectItem(-4, -4, 8, 8, parent),
  6. positionFlags_(positionFlags),
  7. parent_(parent)
  8. {
  9. setBrush(QBrush(Qt::lightGray));
  10. setFlag(ItemIsMovable);
  11. setFlag(ItemSendsGeometryChanges);
  12. }
  13.  
  14. int SizeGripItem::HandleItem::positionFlags() const
  15. {
  16. return positionFlags_;
  17. }
  18.  
  19. QVariant SizeGripItem::HandleItem::itemChange(GraphicsItemChange change,
  20. const QVariant &value)
  21. {
  22. QVariant retVal = value;
  23.  
  24. if (change == ItemPositionChange)
  25. {
  26. retVal = restrictPosition(value.toPointF());
  27. }
  28. else if (change == ItemPositionHasChanged)
  29. {
  30. QPointF pos = value.toPointF();
  31.  
  32. switch (positionFlags_)
  33. {
  34. case TopLeft:
  35. parent_->setTopLeft(pos);
  36. break;
  37. case Top:
  38. parent_->setTop(pos.y());
  39. break;
  40. case TopRight:
  41. parent_->setTopRight(pos);
  42. break;
  43. case Right:
  44. parent_->setRight(pos.x());
  45. break;
  46. case BottomRight:
  47. parent_->setBottomRight(pos);
  48. break;
  49. case Bottom:
  50. parent_->setBottom(pos.y());
  51. break;
  52. case BottomLeft:
  53. parent_->setBottomLeft(pos);
  54. break;
  55. case Left:
  56. parent_->setLeft(pos.x());
  57. break;
  58. }
  59. }
  60.  
  61. return retVal;
  62. }
  63.  
  64. QPointF SizeGripItem::HandleItem::restrictPosition(const QPointF& newPos)
  65. {
  66. QPointF retVal = pos();
  67.  
  68. if (positionFlags_ & Top || positionFlags_ & Bottom)
  69. retVal.setY(newPos.y());
  70.  
  71. if (positionFlags_ & Left || positionFlags_ & Right)
  72. retVal.setX(newPos.x());
  73.  
  74. if (positionFlags_ & Top && retVal.y() > parent_->rect_.bottom())
  75. retVal.setY(parent_->rect_.bottom());
  76. else if (positionFlags_ & Bottom && retVal.y() < parent_->rect_.top())
  77. retVal.setY(parent_->rect_.top());
  78.  
  79. if (positionFlags_ & Left && retVal.x() > parent_->rect_.right())
  80. retVal.setX(parent_->rect_.right());
  81. else if (positionFlags_ & Right && retVal.x() < parent_->rect_.left())
  82. retVal.setX(parent_->rect_.left());
  83.  
  84. return retVal;
  85. }
  86.  
  87. SizeGripItem::SizeGripItem(Resizer* resizer, QGraphicsItem* parent)
  88. : QGraphicsItem(parent),
  89. resizer_(resizer)
  90. {
  91. if (parentItem())
  92. rect_ = parentItem()->boundingRect();
  93.  
  94. handleItems_.append(new HandleItem(TopLeft, this));
  95. handleItems_.append(new HandleItem(Top, this));
  96. handleItems_.append(new HandleItem(TopRight, this));
  97. handleItems_.append(new HandleItem(Right, this));
  98. handleItems_.append(new HandleItem(BottomRight, this));
  99. handleItems_.append(new HandleItem(Bottom, this));
  100. handleItems_.append(new HandleItem(BottomLeft, this));
  101. handleItems_.append(new HandleItem(Left, this));
  102. updateHandleItemPositions();
  103. }
  104.  
  105. SizeGripItem::~SizeGripItem()
  106. {
  107. if (resizer_)
  108. delete resizer_;
  109. }
  110.  
  111. QRectF SizeGripItem::boundingRect() const
  112. {
  113. return rect_;
  114. }
  115.  
  116. void SizeGripItem::paint(QPainter* painter,
  117. const QStyleOptionGraphicsItem* option,
  118. QWidget* widget)
  119. {
  120. }
  121.  
  122. #define IMPL_SET_FN(TYPE, POS) \
  123. void SizeGripItem::set ## POS (TYPE v) \
  124. { \
  125. rect_.set ## POS (v); \
  126. doResize(); \
  127. }
  128.  
  129. IMPL_SET_FN(qreal, Top)
  130. IMPL_SET_FN(qreal, Right)
  131. IMPL_SET_FN(qreal, Bottom)
  132. IMPL_SET_FN(qreal, Left)
  133. IMPL_SET_FN(const QPointF&, TopLeft)
  134. IMPL_SET_FN(const QPointF&, TopRight)
  135. IMPL_SET_FN(const QPointF&, BottomRight)
  136. IMPL_SET_FN(const QPointF&, BottomLeft)
  137.  
  138. void SizeGripItem::doResize()
  139. {
  140. if (resizer_)
  141. {
  142. (*resizer_)(parentItem(), rect_);
  143. updateHandleItemPositions();
  144. }
  145. }
  146.  
  147. void SizeGripItem::updateHandleItemPositions()
  148. {
  149. foreach (HandleItem* item, handleItems_)
  150. {
  151. item->setFlag(ItemSendsGeometryChanges, false);
  152.  
  153. switch (item->positionFlags())
  154. {
  155. case TopLeft:
  156. item->setPos(rect_.topLeft());
  157. break;
  158. case Top:
  159. item->setPos(rect_.left() + rect_.width() / 2 - 1,
  160. rect_.top());
  161. break;
  162. case TopRight:
  163. item->setPos(rect_.topRight());
  164. break;
  165. case Right:
  166. item->setPos(rect_.right(),
  167. rect_.top() + rect_.height() / 2 - 1);
  168. break;
  169. case BottomRight:
  170. item->setPos(rect_.bottomRight());
  171. break;
  172. case Bottom:
  173. item->setPos(rect_.left() + rect_.width() / 2 - 1,
  174. rect_.bottom());
  175. break;
  176. case BottomLeft:
  177. item->setPos(rect_.bottomLeft());
  178. break;
  179. case Left:
  180. item->setPos(rect_.left(),
  181. rect_.top() + rect_.height() / 2 - 1);
  182. break;
  183. }
  184.  
  185. item->setFlag(ItemSendsGeometryChanges, true);
  186. }
  187. }
To copy to clipboard, switch view to plain text mode 

sizegripitem.h
Qt Code:
  1. #ifndef SIZEGRIPITEM_H
  2. #define SIZEGRIPITEM_H
  3.  
  4.  
  5. #include <QGraphicsItem>
  6. #include <QGraphicsRectItem>
  7.  
  8. class SizeGripItem : public QGraphicsItem
  9. {
  10. private:
  11. enum
  12. {
  13. Top = 0x1,
  14. Bottom = 0x2,
  15. Left = 0x4,
  16. TopLeft = Top | Left,
  17. BottomLeft = Bottom | Left,
  18. Right = 0x8,
  19. TopRight = Top | Right,
  20. BottomRight = Bottom | Right
  21. };
  22.  
  23. class HandleItem : public QGraphicsRectItem
  24. {
  25. public:
  26. HandleItem(int positionFlags, SizeGripItem* parent);
  27. int positionFlags() const;
  28.  
  29. protected:
  30. virtual QVariant itemChange(GraphicsItemChange change,
  31. const QVariant &value);
  32.  
  33. private:
  34. QPointF restrictPosition(const QPointF& newPos);
  35.  
  36. int positionFlags_;
  37. SizeGripItem* parent_;
  38. };
  39.  
  40. public:
  41. class Resizer
  42. {
  43. public:
  44. virtual void operator()(QGraphicsItem* item,
  45. const QRectF& rect) = 0;
  46.  
  47. };
  48.  
  49. SizeGripItem(Resizer* resizer = 0, QGraphicsItem* parent = 0);
  50. virtual ~SizeGripItem();
  51. virtual QRectF boundingRect() const;
  52. virtual void paint(QPainter* painter,
  53. const QStyleOptionGraphicsItem* option,
  54. QWidget* widget = 0);
  55. void setTopLeft(const QPointF& pos);
  56. void setTop(qreal y);
  57. void setTopRight(const QPointF& pos);
  58. void setRight(qreal x);
  59. void setBottomRight(const QPointF& pos);
  60. void setBottom(qreal y);
  61. void setBottomLeft(const QPointF& pos);
  62. void setLeft(qreal x);
  63.  
  64. private:
  65. void doResize();
  66. void updateHandleItemPositions();
  67.  
  68. QList<HandleItem*> handleItems_;
  69. QRectF rect_;
  70. Resizer* resizer_;
  71. };
  72.  
  73. #endif // SIZEGRIPITEM_H
To copy to clipboard, switch view to plain text mode 

I can't really think of a way to resize a polygon since the library misses on a lot of functions Rect and Ellipse do have implemented. Thanks for any help or suggestions on how to proceed with resizing a polygon item