Using QRubberBand to crop image in Qt

前端 未结 1 683
再見小時候
再見小時候 2021-01-14 09:34

I want to be able to use rubberband to select an area of an image, then remove the parts of the image outside of the rubberband and display the new image. However when I cur

1条回答
  •  隐瞒了意图╮
    2021-01-14 09:46

    There are two issues here - the position of the rect relative to the image and the fact that the image is (potentially) scaled in the label.

    Position issue:

    QRect myRect(myPoint, event->pos());
    

    You should perhaps change this to:

    QPoint a = mapToGlobal(myPoint);
    QPoint b = event->globalPos();
    
    a = ui->imageLabel->mapFromGlobal(a);
    b = ui->imageLabel->mapFromGlobal(b);
    

    Then, the label may be scaling the image because you used setScaledContents(). So you need to work out the actual coordinates on the unscaled image. Something like this maybe (untested/compiled):

    QPixmap OriginalPix(*ui->imageLabel->pixmap());
    double sx = ui->imageLabel->rect().width();
    double sy = ui->imageLabel->rect().height();
    sx = OriginalPix.width() / sx;
    sy = OriginalPix.height() / sy;
    a.x = int(a.x * sx);
    b.x = int(b.x * sx);
    a.y = int(a.y * sy);
    b.y = int(b.y * sy);
    
    QRect myRect(a, b);
    ...
    

    0 讨论(0)
提交回复
热议问题