Problem while drawing on QGraphicsView after Translation the QGraphicsView.



  • 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;
    

    }
    @


  • Moderators

    is translating really what you want?!

    If i understood your code right on the first glance you should adapt the scrollbar values of your QGraphicsView, right?



  • Actually, I want that when i click on view whatever where i clicked, and i move the mouse in whatever direction, items and view should move in that direction.



  • i think @this->translate(dx,dy);@ code is running fine. Problem is with @ this->setSceneRect(-dx,-dy,this->sceneRect().width(), this->sceneRect().height());@ code.


  • Moderators

    well then i understood you right and you should adapt the scrollbars.



  • Sry, bt i do not understand what you want to say.



  • I do not want to use scrollbars.


  • Moderators

    so you want to simulate a QGraphicsScene of infinite size?

    The scrollbars would already provide everything you need. But when you don't want to use them you have to struggle with the drawbacks, like you already have.
    For example you will also have to translate all event positions etc. in your code...



  • Is there nothing in Qt except scrollbars which can solve my problem ??



  • If i use scrollbars, then how it will work ??? please tell me...


  • Moderators

    instead of translateing like you do now add the values (dx,dy) to the horizontal-/vertical-scrollbar respectively.
    You can get the scrollbar with QGraphicsView::horizontalScrollBar(). and use QScrollBar::setValue()



  • thanku so much sir.... Now, it works very fine.... :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.