QT实现图片缩放的同时标记指定坐标

本文以我的另一篇文章《QT实现图片缩放》为基础,连接为:https://blog.csdn.net/weixin_43935474/article/details/89327314
现在有个需求,图片缩放的同时,我需要标记图片中的某些坐标,以便用户查看,效果如下图:
在这里插入图片描述
功能:1.显示图片的时候,需要画出红色的矩形标记图片中的指定坐标
2.缩放的时候,矩形标记大小不变
3.缩放及拖动图片的时候,矩形标记的中心始终在图片的指定坐标
工程下载链接:https://download.csdn.net/download/weixin_43935474/11500546
主要代码如下:
1.用QVector m_vPoint;//存放需要标记的图片中点的位置,注意:图片正中央的坐标为(0,0),往右为x轴正方向,往下为y轴正方向!!!
2.用QVector<QGraphicsRectItem*> m_vQGRecItem;//存放要画出来的矩形标记,根据m_vPoint来绘制m_vQGRecItem。
3.界面上绘图区域的组成为“QGraphicsView”—包含—>“QGraphicsScene”—包含—>“QGraphicsRectItem(其中包含矩形)”和“QGraphicsItem(其中包含图片)”
主要代码如下:

void MainWindow::recvShowPicSignal(QImage image)//显示图片及矩形标记的函数
{
    QPixmap ConvertPixmap=QPixmap::fromImage(image);//The QPixmap class is an off-screen image representation that can be used as a paint device
    m_qgraphicsScene->clear();
    m_Image = new ImageWidget(&ConvertPixmap);//实例化类ImageWidget的对象m_Image,该类继承自QGraphicsItem,是自己写的类
    QObject::connect(m_Image,&ImageWidget::showPos,this,&MainWindow::showMousePos);
    QObject::connect(m_Image,&ImageWidget::mouseWheelSig,this,&MainWindow::recvGreViewMouseWheel);
    int nwith = ui->ImageGraphic->width();//获取界面控件Graphics View的宽度
    int nheight = ui->ImageGraphic->height();//获取界面控件Graphics View的高度
    m_Image->setQGraphicsViewWH(nwith,nheight);//将界面控件Graphics View的width和height传进类m_Image中
    m_Image->setAcceptHoverEvents(true);
    //test QGraphicsRectItem
    m_vPoint.clear();
    m_vPoint.push_back(QPointF(-500/2,-500/2));
    m_vPoint.push_back(QPointF(-500/2,500/2));
    m_vPoint.push_back(QPointF(500/2,-500/2));
    m_vPoint.push_back(QPointF(500/2,500/2));
    m_vPoint.push_back(QPointF(0,0));
    m_vQGRecItem.clear();
    QPen pen(Qt::red);//设置矩形标记的颜色
    pen.setWidth(5);//设置矩形标记的边宽度
    for(int i=0;i<m_vPoint.size();i++)
    {
        QGraphicsRectItem *pQGRectItem = new QGraphicsRectItem(m_vPoint[i].x()-20,m_vPoint[i].y()-20,40,40,m_Image);
        pQGRectItem->setPen(pen);
        m_vQGRecItem.push_back(pQGRectItem);
    }
    //test QGraphicsRectItem
    m_qgraphicsScene->addItem(m_Image);//将1QGraphicsItem类对象放进QGraphicsScene中
    for(int i=0;i<m_vQGRecItem.size();i++)
    {
        m_qgraphicsScene->addItem(m_vQGRecItem[i]);
    }
    ui->ImageGraphic->setSceneRect(QRectF(-(nwith/2),-(nheight/2),nwith,nheight));//使视窗的大小固定在原始大小,不会随图片的放大而放大(默认状态下图片放大的时候视窗两边会自动出现滚动条,并且视窗内的视野会变大),防止图片放大后重新缩小的时候视窗太大而不方便观察图片
    ui->ImageGraphic->setScene(m_qgraphicsScene);//Sets the current scene to scene. If scene is already being viewed, this function does nothing.
    ui->ImageGraphic->setFocus();//将界面的焦点设置到当前Graphics View控件
    ui->ImageGraphic->setMouseTracking(true);
}

void MainWindow::recvGreViewMouseWheel(qreal qrScale)//缩放图片后,矩形标记需要跟着做相应变换
{
    qreal tempScale = m_Image->m_scaleDafault/qrScale;
    for(int i=0;i<m_vQGRecItem.size();i++)
    {
        m_vQGRecItem[i]->setScale(tempScale);
        QRectF rect = m_vQGRecItem[i]->rect();
        m_vQGRecItem[i]->setRect(m_vPoint[i].x()/tempScale-20,m_vPoint[i].y()/tempScale-20,rect.width(),rect.height());
    }
}
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreenHandBruce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值