Now I build graphic application, but I have issue with draw the dimension line, and dimension can move when I move mouse, as software: Auto Cad or solidwork software, as picture below:
https://i.stack.imgur.com/BXCBj.png

, I want to adjust the position of dimension line,Now, my method is use handle Item to move the dimension,
https://i.stack.imgur.com/34VfQ.png

This is my code:

Qt Code:
  1. void DPS_HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  2. {
  3. // QCursor* cursor = nullptr;
  4.  
  5. switch (handlePosition) {
  6.  
  7. case dimension:
  8. {
  9. setCursor(Qt::SizeHorCursor);
  10.  
  11. DPS_ResizeableHandleRect * rectItem = dynamic_cast<DPS_ResizeableHandleRect *>( parentItem());
  12. if(rectItem){
  13. QRectF boundingFrameRect = rectItem->selectorFrameBounds();
  14.  
  15. boundingFrameRect.setRight(event->pos().x() + 5);
  16.  
  17. rectItem->setSelectorFrameBounds(boundingFrameRect);
  18. }
  19. }
  20. break;
  21. }
  22.  
  23. }
  24.  
  25. void DPS_ResizeableHandleRect::drawHandlesDimension()
  26. {
  27. //Populate handles in list
  28. if(handleListCenter.count() == 0){
  29. // handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::LeftCenter));
  30. handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::dimension));
  31. }
  32.  
  33. //Set up pen
  34. QPen mPen;
  35. mPen.setWidth(1);
  36. mPen.setColor(Qt::darkGreen);
  37.  
  38. //Right Center handle
  39. QPointF centerRightCorner = selectorFrameBounds().topRight() + QPointF(-4.5, selectorFrameBounds().height()/2.0 - 4.5);
  40. centerRightHandleRect = QRectF(centerRightCorner,QSize(9,9));
  41. handleListCenter[0]->setRect(centerRightHandleRect);
  42. if(!handleListCenter.isEmpty() && (!handlesAddedToScene)){
  43. handleListCenter[0]->setPen(mPen);
  44. handleListCenter[0]->setBrush(QBrush(Qt::darkGreen));
  45. ownerItem->scene()->addItem(handleListCenter[0]);
  46. handleListCenter[0]->setParentItem(ownerItem);
  47. }
  48. handlesAddedToScene = true;
  49.  
  50. }
  51.  
  52. void DPS_ResizeableHandleRect::drawHandlesDimensionIfNecessary()
  53. {
  54. if(!ownerItem){
  55. qWarning() << "ResizableHandleRect : No ownerItem set. Not drawing any\
  56. handle. Please call setOwnerItem on your ResizableHandleRect subclass";
  57. return;
  58. }
  59.  
  60.  
  61.  
  62. if(ownerItem->isSelected()){
  63. drawHandlesDimension();
  64. handlesAddedToScene = true;
  65. }else{
  66.  
  67. //Remove the handles
  68. foreach (DPS_HandleItem * handleItem, handleListCenter) {
  69. ownerItem->scene()->removeItem(handleItem);
  70. }
  71. qDeleteAll(handleListCenter);
  72. handleListCenter.clear();
  73. handlesAddedToScene = false;
  74. }
  75. }
  76.  
  77. void D_MeasureSet_Dimension::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  78. {
  79. Q_UNUSED(option);
  80. Q_UNUSED(widget);
  81. painter->save();
  82.  
  83. painter->setPen(pen());
  84. painter->setBrush(brush());
  85.  
  86. painter->drawPath(starFromRect(boundingRect().adjusted(pen().width(), pen().width(),
  87. -pen().width(),-pen().width())));
  88. painter->setPen(QPen(Qt::green,1));
  89. painter->drawRect(rect());
  90.  
  91. drawHandlesDimensionIfNecessary();
  92.  
  93. painter->restore();
  94. }
To copy to clipboard, switch view to plain text mode 

But I have issue: when I move the dimension to opposite direction (width of rect < 0 ) the dimension line disappeared. If I use method:

Qt Code:
  1. rectItem->setSelectorFrameBounds(boundingFrameRect.normalized());
To copy to clipboard, switch view to plain text mode 

The dimension can not move to opposite direction, and the rectangle also was moved.

So, could you help me fix this issue? or you have any idea or other way to do this job, please instruct me.