繁体   English   中英

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

[英]Cropping a QImage using Mouse Events in Qt

我需要裁剪显示在QLabel上的QImage。 我想用mouseevents做到这一点,例如在图像上拖动一个矩形,然后在releaseevent上,图像应裁剪为矩形的大小。 我已经实现了以下代码:

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);

错误:成员函数无效使用(您忘记了'()'吗?)

ax = int(ax * sx); ^

我该如何解决?

通过听您的编译器:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM