81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
#include "imageview.h"
|
||
#include <QDebug>
|
||
|
||
ImageView::ImageView(QWidget *parent)
|
||
: QGraphicsView(parent),
|
||
rubberBand(nullptr)
|
||
{
|
||
scene = new QGraphicsScene(this);
|
||
setScene(scene);
|
||
pximg = new QGraphicsPixmapItem();
|
||
scene->addItem(pximg);
|
||
|
||
|
||
setDragMode(QGraphicsView::NoDrag);
|
||
setRenderHint(QPainter::Antialiasing);
|
||
|
||
// 设置视图属性
|
||
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
||
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
|
||
state = false;
|
||
}
|
||
|
||
|
||
|
||
void ImageView::mousePressEvent(QMouseEvent *event)
|
||
{
|
||
if (event->button() == Qt::LeftButton && pximg)
|
||
{
|
||
origin = event->pos();
|
||
if (!rubberBand)
|
||
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
|
||
rubberBand->setGeometry(QRect(origin, QSize()));
|
||
rubberBand->show();
|
||
}
|
||
if(event->button()==Qt::RightButton){
|
||
state = false;
|
||
}
|
||
QGraphicsView::mousePressEvent(event);
|
||
}
|
||
|
||
void ImageView::mouseMoveEvent(QMouseEvent *event)
|
||
{
|
||
if (rubberBand && rubberBand->isVisible())
|
||
{
|
||
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
|
||
}
|
||
QGraphicsView::mouseMoveEvent(event);
|
||
}
|
||
|
||
void ImageView::mouseReleaseEvent(QMouseEvent *event)
|
||
{
|
||
if (event->button() == Qt::LeftButton && rubberBand && pximg)
|
||
{
|
||
rubberBand->hide();
|
||
|
||
// 获取ROI区域(视图坐标)
|
||
QRect roiRect = rubberBand->geometry();
|
||
|
||
// 转换为场景坐标
|
||
QPointF topLeft = mapToScene(roiRect.topLeft());
|
||
QPointF bottomRight = mapToScene(roiRect.bottomRight());
|
||
QRectF sceneRect(topLeft, bottomRight);
|
||
|
||
// 确保在图片范围内
|
||
QRectF imageRect = pximg->boundingRect();
|
||
sceneRect = sceneRect.intersected(imageRect);
|
||
|
||
if (!sceneRect.isEmpty())
|
||
{
|
||
// 发出信号
|
||
qDebug()<<"跟踪清除";
|
||
emit roiSelected(sceneRect);
|
||
// qDebug() << "ROI Selected - Position:" << sceneRect.topLeft()
|
||
// << "Size:" << sceneRect.size();
|
||
}
|
||
}
|
||
QGraphicsView::mouseReleaseEvent(event);
|
||
}
|