在Qt中使用鼠标事件裁剪QImage

时间:2015-11-29 14:08:22

标签: c++ qt opencv3.0 qimage

我需要裁剪QLabel上显示的QImage。我想用鼠标事件来做,比如在图像上拖动一个矩形,然后在释放事件上,图像应该裁剪为矩形的大小。我已经实现了这段代码:

void surf_detection::mousePressEvent(QMouseEvent *ev)
{
if(ui->label_2->underMouse()){
    cout <<"Entered Press"<<endl;
    origin = ev->pos();
    //if (!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);

        rubberBand->show();
}
}

void surf_detection::mouseMoveEvent(QMouseEvent *ev)
{

rubberBand->setGeometry(QRect(origin, ev->pos()).normalized());                                                                                                              
}

void surf_detection::mouseReleaseEvent(QMouseEvent *ev)
{
QPoint a = mapToGlobal(origin);
QPoint b = ev->globalPos();

a = ui->label_2->mapFromGlobal(a);
b = ui->label_2->mapFromGlobal(b);
rubberBand->hide();
QPixmap OriginalPix(*ui->label_2->pixmap());
double sx = ui->label_2->rect().width();
double sy = ui->label_2->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);

QImage newImage;
newImage = OriginalPix.toImage();

QImage copyImage;
copyImage = copyImage.copy(myRect);

ui->label_2->setPixmap(QPixmap::fromImage(copyImage));
ui->label_2->repaint();

}

但是这段代码在

给了我错误
a.x = int(a.x * sx);
b.x = int(b.x * sx);
a.y = int(a.y * sy);
b.y = int(b.y * sy);

错误:无效使用会员功能(你忘了&#39;()&#39;?)

a.x = int(a.x * sx);                   ^

我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

通过收听编译器:

a.setX(int(a.x() * sx));