QT之利用QGraphicsScene图布完成在图片上层图画并放缩保持相对位置不变_qgraphicsscene上添加图片

收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
img
img

如果你需要这些资料,可以戳这里获取

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

m_graphicsView = new QGraphicsView(this);
m_graphicsView->setStyleSheet(“padding:0px;border:0px”);
m_graphicsView->setStyleSheet(“background:transparent;border:0px”);
m_graphicsView->setAttribute(Qt::WA_TransparentForMouseEvents);
m_graphicsView->setGeometry(2, 40, width(), height());
m_graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

m_graphicsView->show();
m_scene = new QGraphicsScene();
m_scene->setSceneRect(2, 40, width(), height());
m_graphicsView->setScene(m_scene);


窗口大小变化是也要跟着变化



//同步将画布大小变化
m_graphicsView->setGeometry(2, 40, width(), height());
m_scene->setSceneRect(2, 40, width(), height());
m_graphicsView->setScene(m_scene);


绘画 添加一横



m_scene->addLine(m_pStart.x(), m_pStart.y(), pt.x(), pt.y(), m_pen);


四、代码实践  
 头文件



#ifndef WIDGET_H
#define WIDGET_H

#include
#include
#include
#include
#include
namespace Ui {
class Widget;
}
struct Position
{
int x;
int y;
};
class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
void updateImage( const QImage &image);
void clearAllPaint();

int QColorToInt(const QColor &color);
void setWhiteboardId(const std::string &boardId);

protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
virtual void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);

private slots:
void on_startMark_clicked();

void on\_clearMark\_clicked();

void on\_endMark\_clicked();

private:
void repaint();
Ui::Widget *ui;
QImage m_image;

QGraphicsView \*m_graphicsView = NULL;
QGraphicsScene \*m_scene = NULL;
qreal m_scale = 1;
std::vector<QGraphicsLineItem \*> m_vcItem;
QPen    m_pen;
QPoint  m_pStart;

bool    m_bValild = false;
bool    m_bIsMark = false;
bool    m_bRepaint = false;

QMutex m_Sizemutex;

std::string m_strConfId;
std::string m_strUsrId;


std::vector<Position>    m_lstPoint;
std::vector<std::vector<Position>> m_markAllData;

};

#endif // WIDGET_H



#include “widget.h”
#include “ui_widget.h”
#include
#include
#include
#include
#include
#include
#include
#include
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

//利用 QGraphicsView 试图结合 QGraphicsScene 画布在图片上方叠加一层实现图画
m_graphicsView = new QGraphicsView(this);
m_graphicsView->setStyleSheet("padding:0px;border:0px");
m_graphicsView->setStyleSheet("background:transparent;border:0px");
m_graphicsView->setAttribute(Qt::WA_TransparentForMouseEvents);
m_graphicsView->setGeometry(2, 40, width(), height());
m_graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

m_graphicsView->show();
m_scene = new QGraphicsScene();
m_scene->setSceneRect(2, 40, width(), height());
m_graphicsView->setScene(m_scene);

m_pen.setStyle(Qt::SolidLine);
m_pen.setColor(Qt::red);
m_pen.setWidth(2);

m_image.load("1.png");

}

Widget::~Widget()
{
if (m_graphicsView)
{
delete m_graphicsView;
m_graphicsView = NULL;
}

if (m_scene)
{
delete m_scene;
m_scene = NULL;
}

delete ui;
}
//重绘事件 当窗口图片大小变化的时候我们的图画应该也要随着变化,保持与图片位置的不变性
void Widget::repaint()
{
int currentWidth = width();
int currentHeight = height();
if(currentWidth == 40 || currentHeight== 40)//做一下保护
return ;
//qDebug()<<currentWidth << " " << currentHeight;

//重绘
m_Sizemutex.lock();
if (m_scene)
    m_scene->clear();
int lines = m_markAllData.size();

for(int i = 0; i < lines; i++)
{
    //算法保持相对位置
    //-2是是x轴留一部分不填充 -40是上面按钮的高度抽出来
    int startX = ((float)m_markAllData[i][0].x)/m_image.width()\*(currentWidth - 2);
    startX += 2;
    int startY = ((float)m_markAllData[i][0].y)/m_image.height()\*(currentHeight - 40);
    startY += 40;
    qDebug() << "startX=" << startX << "startY" << startY << "currentWidth=" << currentWidth << "currentHeight" << currentHeight;

    for (int j = 1; j < m_markAllData[i].size(); j++) {
        int x = ((float)m_markAllData[i][j].x)/m_image.width()\*(currentWidth - 2);
        int y = ((float)m_markAllData[i][j].y)/m_image.height()\*(currentHeight - 40);
        x += 2;
        y += 40;
        m_scene->addLine(startX, startY, x, y, m_pen);
        startX = x;
        startY = y;
    }
}
m_Sizemutex.unlock();

}
//窗口大小变化触发事件
void Widget::resizeEvent(QResizeEvent *event)
{
//同步将画布大小变化
m_graphicsView->setGeometry(2, 40, width(), height());
m_scene->setSceneRect(2, 40, width(), height());
m_graphicsView->setScene(m_scene);

//并根据记录的位置重新图画保持与图片位置的一致性
repaint();

QWidget::resizeEvent(event);

}
//鼠标按下事件 表示图画开始
void Widget::mousePressEvent(QMouseEvent *event)
{
if ((event->button() == Qt::LeftButton) && (m_bIsMark == true))
{
m_bValild = true;
Position curPoint;

    //记录鼠标当前点击的坐标
    m_pStart = event->pos();

    curPoint.x =  event->pos().x();
    curPoint.y = event->pos().y();
   //转换到窗口坐标
    if(width() <= 2 || height() <= 40)
        return ;
    if((float)curPoint.x <= 2 || (float)curPoint.y <= 40)
        return ;
    //转换为相对位置 窗口与图片的相对位置
    curPoint.x = ((float)curPoint.x - 2)/(width() - 2)\*m_image.width();

收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
img
img

如果你需要这些资料,可以戳这里获取

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

08)]
[外链图片转存中…(img-MmsKry6l-1715898754109)]

如果你需要这些资料,可以戳这里获取

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值