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();
            qDebug()<<__FUNCTION__<<pos;
            QRect rect(pos.x() - 150, pos.y() - 150, 150, 150);
            QPixmap cropped = m_Pixmap.copy(rect);
            cropped.save("C:/Users/uidk6742/Desktop/OUT.jpg");
        }
    .
    .
    }
    

    0_1562179712364_434ab511-5465-45c4-b3b6-2b6c568e5885-image.png

    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.

    Regards,
    Sayan


  • Lifetime Qt Champion

    Hi,

    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
    0_1562232427759_aabbd508-c78c-477e-9fbe-74abb9f12ba8-image.png

    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.


  • Qt Champions 2018

    @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)
    https://stackoverflow.com/questions/16362191/qt-grab-widget-and-save-image

    m_graphicsView->grab(rect).save("OUT.jpg");
    

    Thanks all.