PosCalibrate/imageview.cpp
2025-10-15 14:50:31 +08:00

81 lines
2.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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