Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt C++ Draw resizable Item
Forum Updated to NodeBB v4.3 + New Features

Qt C++ Draw resizable Item

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 172 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FahNos
    wrote on last edited by
    #1

    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 alt text, I want to adjust the position of dimension line,
    Now, my method is use handle Item to move the dimension, alt text
    This is my code:

    void DPS_HandleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
      //  QCursor* cursor = nullptr;
    
      switch (handlePosition) {
        
        case dimension:
          {
            setCursor(Qt::SizeHorCursor);
    
            DPS_ResizeableHandleRect * rectItem = dynamic_cast<DPS_ResizeableHandleRect *>( parentItem());
            if(rectItem){
                QRectF boundingFrameRect = rectItem->selectorFrameBounds();
    
                boundingFrameRect.setRight(event->pos().x() + 5);
    
                rectItem->setSelectorFrameBounds(boundingFrameRect);
              }
          }
          break;
        }
    
    }
    
    void DPS_ResizeableHandleRect::drawHandlesDimension()
    {
        //Populate handles in list
        if(handleListCenter.count() == 0){
    //        handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::LeftCenter));
            handleListCenter.append(new DPS_HandleItem(DPS_HandleItem::dimension));
        }
    
        //Set up pen
        QPen mPen;
        mPen.setWidth(1);
        mPen.setColor(Qt::darkGreen);
    
        //Right Center handle
        QPointF centerRightCorner = selectorFrameBounds().topRight() + QPointF(-4.5, selectorFrameBounds().height()/2.0 - 4.5);
        centerRightHandleRect  = QRectF(centerRightCorner,QSize(9,9));
        handleListCenter[0]->setRect(centerRightHandleRect);
        if(!handleListCenter.isEmpty() && (!handlesAddedToScene)){
            handleListCenter[0]->setPen(mPen);
            handleListCenter[0]->setBrush(QBrush(Qt::darkGreen));
            ownerItem->scene()->addItem(handleListCenter[0]);
            handleListCenter[0]->setParentItem(ownerItem);
        }
        handlesAddedToScene = true;
    
    }
    
    void DPS_ResizeableHandleRect::drawHandlesDimensionIfNecessary()
    {
        if(!ownerItem){
            qWarning() << "ResizableHandleRect : No ownerItem set. Not drawing any\
                          handle. Please call setOwnerItem on your ResizableHandleRect subclass";
                          return;
        }
    
    
    
        if(ownerItem->isSelected()){
            drawHandlesDimension();
            handlesAddedToScene = true;
        }else{
    
            //Remove the handles
            foreach (DPS_HandleItem * handleItem, handleListCenter) {
                ownerItem->scene()->removeItem(handleItem);
            }
            qDeleteAll(handleListCenter);
            handleListCenter.clear();
            handlesAddedToScene = false;
        }
    }
    
    void D_MeasureSet_Dimension::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        painter->save();
    
        painter->setPen(pen());
        painter->setBrush(brush());
    
        painter->drawPath(starFromRect(boundingRect().adjusted(pen().width(), pen().width(),
                                                               -pen().width(),-pen().width())));
        painter->setPen(QPen(Qt::green,1));
        painter->drawRect(rect());
    
        drawHandlesDimensionIfNecessary();
    
        painter->restore();
    }
    

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

    rectItem->setSelectorFrameBounds(boundingFrameRect.normalized());
    

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved