使用Qt中的鼠标事件裁剪QImage
Cropping a QImage using Mouse Events in Qt
我需要裁剪QLabel上显示的QImage。我想用鼠标事件来完成,比如在图像上拖动一个矩形,然后在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);
错误:成员函数的使用无效(您忘记了"()"吗?)
a.x=int(a.x*sx);^
我该如何解决此问题?
通过监听编译器:
a.setX(int(a.x() * sx));
相关文章:
- 在Windows上用C++裁剪HBITMAP
- Android NDK传感器向事件队列报告奇怪的间隔
- 从文本文件中读取时钟时间和事件时间并进行处理
- WMI检测进程创建事件-c++
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 处理闪烁窗口事件
- C++Builder中的OnClick事件签名存在问题
- 跟踪滚动条上的鼠标事件
- 什么是事件表 (wxWidgets)?
- 如何在 MFCaptureEngine 中获取"Camera removed"事件
- 给定顺序中的事件处理
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL
- 如何创建事件驱动的 SDL2 应用程序
- Azure Kinect 使用正文索引映射裁剪正文
- Windows 进程间同步类似事件?
- 如何从C++端挂接到 QML 项的 onClick 事件
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 如何在Qt 4.8中阻止/忽略/丢弃早于特定超时的用户输入事件
- 使用Qt中的鼠标事件裁剪QImage