Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Problem while drawing on QGraphicsView after Translation the QGraphicsView.

$
0
0
Hi everyone. I created a view which is subclass of QGraphicsView and its scene rect is equal to desktop’s rect. I am doing free hand drawing on it successfully. I also want to move the view on mouseMoveEvent. For that, i use the QGraphicsView::translate() function. It moves very fine. But after moving when i draw on it, mouse coordinates and draw coordinates does not match. Following code is of my QGraphicsView’s subclass. Thanks in advance. :) void ViewClass::mousePressEvent(QMouseEvent *event) {     IsMousePressed = true;       if(event->button() == Qt::LeftButton)     {         if(IsPenSelected)         {             drawingPen.setCapStyle(Qt::RoundCap);             drawingPen.setJoinStyle(Qt::RoundJoin);               currentPt = previousPt = event->globalPos();             drawLineTo(previousPt,currentPt,drawingPen);         }         else if(IsHandSelected)         {             previousPt = event->pos();           }     } }   void ViewClass::mouseMoveEvent(QMouseEvent *event) {     IsMouseMove = true;       if(IsMousePressed && IsMouseMove)     {         if(IsPenSelected)         {             drawingPen.setCapStyle(Qt::RoundCap);             drawingPen.setJoinStyle(Qt::RoundJoin);               previousPt = currentPt;             currentPt = event->pos();             drawLineTo(previousPt,currentPt,drawingPen);         }         else if(IsHandSelected)         {             QPoint eventPosition = event->pos();               adjustDisplayView(eventPosition);         }     } }   void ViewClass::mouseReleaseEvent(QMouseEvent *event) {     if(IsPenSelected)         IsPenSelected =  false;     else if(IsHandSelected)         IsHandSelected = false;       IsMouseMove = IsMousePressed = false;   }   void ViewClass::drawLineTo(const QPoint &previous, const QPoint &current, QPen drawPen) {         mRedPathItem->setBrush(drawPen.brush());           lineHelper = new QGraphicsLineItem;         lineHelper->setLine( QLineF(previous, current) );         lineHelper->setPen(drawPen);         lineHelper->setFlag(QGraphicsItem::ItemIsMovable, true);           QPainterPath path = mRedPathItem->path();         path.setFillRule(Qt::WindingFill);         path.addPath( lineHelper->shape() );           path = path.simplified();           mRedPathItem->setPath(path);   }   void ViewClass::adjustDisplayView(const QPoint &currnt) {     dx += currnt.x() - previousPt.x();     dy += currnt.y() - previousPt.y();       this->translate(dx,dy);     this->setSceneRect(-dx,-dy,this->sceneRect().width(), this->sceneRect().height());       previousPt = currnt;   }

Viewing all articles
Browse latest Browse all 13965

Trending Articles