Qt World Summit: Submit your Presentation

Crop pixmap from qgraphicsscene with qgraphicsItem

  • I'm trying to crop pixmap of the qgraphicsScene with the qgraphicsItem. But not able to find proper API or approach to do so.

    void ImageGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
        if(m_isMousePressed && m_isMagnifyModeEnabled)
            QPointF pos = mouseEvent->scenePos();
            QRect rect(pos.x() - 150, pos.y() - 150, 150, 150);
            QPixmap cropped = m_Pixmap.copy(rect);


    In the image cropped from the scene, doesnt contain the qgraphicsItem drawn on the car.

    Please suggest any approach, by which we can crop the pixmap from scene with the qgraphicsItem.


  • Lifetime Qt Champion


    QRubberBand comes to mind for creating the selection rectangle. Then you can use it to perform your cropping.

  • @SGaist I tried, Below is the output

    But how to save the contents of the rubberband? I didn't find any api in QRubberband to get the contents in the rubberband?

    I google searched about QRubberband examples...and found most of are in QGraphicsItem's subclassed.

    I actually need to crop out from the qgraphicsscene with the qgraphicsitem on it.

  • Lifetime Qt Champion

    @sayan275 QRubberband is nor responsible for cropping an image. Use https://doc.qt.io/qt-5/qpixmap.html#copy for that.
    You can get the size of rubberband (for cropping) using https://doc.qt.io/qt-5/qwidget.html#geometry-prop

  • @jsulm size is not a problem. Please see my the top thread in this topic. I'm able to copy the pixmap from the scene, but not with the qgraphicsItem on it.

    with copy, we can get a particular item, as the image is a pixmapItem and the box is a rectItem. But both can't be grabbed as 1 pixmap and use copy on that pixmap.

    I'll look further if possible by something called ..widgetGrabber..(maybe) or else, I have to pass the whole scene to do the operation(which is not good approach).

    My requirement is a Magnifying Glass effect on the qgraphicsScene, when the user presses the left mouse and move the mouse.

  • Hi,
    I got the expected output from grab(QRect)


    Thanks all.