Important: Please read the Qt Code of Conduct -

QGraphicsRectItem on QGraphicsView doesn't update while zooming the QGraphicsView

  • I would like to ask you for help with the problem. (Qt 4.8)

    In general, problem lies in QGraphicsView and QGraphicsRectItems in it. I've created simple application which allows you to add some pixels by clicking left mouse button. Everything works nice, except zooming which is done using mouse wheel events.

    The problem is in not updating QGraphicsRectItem objects which are already added to the QGraphicsScene .

    As you can see on the image below, i added one pixel and everything is shown properly.


    But when i will zoom out the picture, this happens:


    Here is code which is responsible for adding rects to the scene:

    void CustomView::mousePressEvent( QMouseEvent *event )
        if( m_Cursor == MOVE_CURSOR )
            m_OriginX = event->x();
            m_OriginY = event->y();
            setCursor( Qt::ClosedHandCursor );
            QPoint position = mapToScene( event->pos() ).toPoint();
            QGraphicsRectItem *pNewItem = new QGraphicsRectItem( position.x(), position.y(), 1, 1 );
            m_RectItemVector.push_back( pNewItem );
            m_pScene->addItem( pNewItem );

    And here is code which is responsible for zooming in/out:

    void CustomView::wheelEvent( QWheelEvent *event )
        QPointF pointWheel = mapToScene( event->pos() );
        if( event->delta() > 0 )
            scale( SCALE_FACTOR, SCALE_FACTOR );
            scale( 1/SCALE_FACTOR, 1/SCALE_FACTOR );
        centerOn( pointWheel.x(), pointWheel.y() );

    Maybe some of you had this issue and found some workaround, or maybe i'm doing something wrong? I would be grateful for any tips and thanks in advance!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I am not sure I understand your issue here.

    Would it be possible for you to draw the outcome you are expecting with the zoom ?

  • Hello @SGaist

    Basically i wanted to create application which will allow to "mark" chosen pixel by clicking on one with mouse button. On the first screenshot chosen rect is drawn properly, but when image is zoomed out, on the second screenshot rect is slightly "moved" to the left which schould not happen.

    This phenomenon occurs only with few scale factors. Today i found also the origin of the issue. When scaling, image is being cropped a bit on the right side, so the last column of pixels is cut in half or sometimes more.

    Although today i found that this was a bug in Qt which was reported, i'm still looking for some kind of workaround.

    Link to the issue:

    Here is how the result should looks like (compared to the second screenshot):

  • Lifetime Qt Champion

    Can you check with a more recent version of Qt if you still have that issue ?

  • I was checking it with Qt 5 and it seems that this issue is no longer exists. My problem lies in that i need to find some workaround on Qt 4.8.

  • Lifetime Qt Champion

    Then you will have to do some code comparison between the Qt 5 version were the issue is fixed and what you have with Qt 4.

  • Thanks, nice idea. I will give it a try.

Log in to reply