当前位置: 首页 > news >正文

Qt Graphics View 绘图架构

Qt Graphics View 绘图架构

"QWGraphicsView.h" 头文件代码如下:

#pragma once#include <QGraphicsView>class QWGraphicsView : public QGraphicsView
{Q_OBJECTpublic:QWGraphicsView(QWidget *parent);~QWGraphicsView();protected:void mouseMoveEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);signals:void mouseMovePoint(QPoint point);void mouseClicked(QPoint point);
};

mouseMoveEvent()是鼠标移动事件,代码如下:

//鼠标移动事件
void QWGraphicsView::mouseMoveEvent(QMouseEvent *event)
{QPoint point = event->pos();//QGraphicsView的坐标emit mouseMovePoint(point); //释放信号QGraphicsView::mouseMoveEvent(event);
}

       在此事件响应代码里,通过事件的pos()函数获取鼠标光标在视图中的坐标point,然后作为参数发射 mouseMovePoint(point)信号。这样,若在其他地方编写槽函数与此信号关联,就可以对鼠标移动事件作出响应。

mousePressEvent()是鼠标按键按下事件,代码如下:

//鼠标左键按下事件
void QWGraphicsView::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){QPoint point = event->pos();//QGraphicsView的坐标emit mouseClicked(point);//释放信号}QGraphicsView::mousePressEvent(event);
}

       在此事件响应代码里,首先判断是否是鼠标左键按下,然后通过事件的pos()函数获取鼠标光标在视图中的坐标point,然后作为参数发射mouseClicked(point)信号。

"sample8_4QGraphicsView.h"  头文件代码如下:

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_sample8_4QGraphicsView.h"#include    <QGraphicsScene>
#include    <QLabel>class sample8_4QGraphicsView : public QMainWindow
{Q_OBJECTpublic:sample8_4QGraphicsView(QWidget *parent = Q_NULLPTR);private:Ui::sample8_4QGraphicsViewClass ui;protected:void resizeEvent(QResizeEvent *event);private:QGraphicsScene  *scene;QLabel  *labViewCord;QLabel  *labSceneCord;QLabel  *labItemCord;void iniGraphicsSystem(); //创建Graphics View的各项private slots:void on_mouseMovePoint(QPoint point);void on_mouseClicked(QPoint point);};

#include "sample8_4QGraphicsView.h"#include  "ui_sample8_4QGraphicsView.h"
#include   <QGraphicsEllipseItem>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")sample8_4QGraphicsView::sample8_4QGraphicsView(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);labViewCord = new QLabel("View 坐标:");labViewCord->setMinimumWidth(150);ui.statusBar->addWidget(labViewCord);labSceneCord = new QLabel("Scene 坐标:");labSceneCord->setMinimumWidth(150);ui.statusBar->addWidget(labSceneCord);labItemCord = new QLabel("Item 坐标:");labItemCord->setMinimumWidth(150);ui.statusBar->addWidget(labItemCord);ui.View->setCursor(Qt::CrossCursor);ui.View->setMouseTracking(true);ui.View->setDragMode(QGraphicsView::RubberBandDrag);QObject::connect(ui.View, SIGNAL(mouseMovePoint(QPoint)),this, SLOT(on_mouseMovePoint(QPoint)));QObject::connect(ui.View, SIGNAL(mouseClicked(QPoint)),this, SLOT(on_mouseClicked(QPoint)));iniGraphicsSystem();
}

鼠标移动事件响应:

//鼠标移动事件,point是 GraphicsView的坐标,物理坐标
void sample8_4QGraphicsView::on_mouseMovePoint(QPoint point)
{labViewCord->setText(QString::asprintf("View 坐标:%d,%d", point.x(), point.y()));QPointF pointScene = ui.View->mapToScene(point); //转换到Scene坐标labSceneCord->setText(QString::asprintf("Scene 坐标:%.0f,%.0f", pointScene.x(), pointScene.y()));
}

鼠标单击事件响应:

//鼠标单击事件
void sample8_4QGraphicsView::on_mouseClicked(QPoint point)
{QPointF pointScene = ui.View->mapToScene(point); //转换到Scene坐标QGraphicsItem  *item = NULL;item = scene->itemAt(pointScene, ui.View->transform()); //获取光标下的绘图项if (item != NULL) //有绘图项{QPointF pointItem = item->mapFromScene(pointScene); //转换为绘图项的局部坐标labItemCord->setText(QString::asprintf("Item 坐标:%.0f,%.0f", pointItem.x(), pointItem.y()));}
}

窗口变化大小时的事件:

//窗口变化大小时的事件
void sample8_4QGraphicsView::resizeEvent(QResizeEvent *event)
{ Q_UNUSED(event);//Graphics View坐标,左上角总是(0,0),宽度=,长度=ui.labViewSize->setText(QString::asprintf("Graphics View坐标,左上角总是(0,0),宽度=%d,高度=%d",ui.View->width(), ui.View->height()));QRectF  rectF = ui.View->sceneRect(); //Scene的矩形区ui.LabSceneRect->setText(QString::asprintf("QGraphicsView::sceneRect=(Left,Top,Width,Height)=%.0f,%.0f,%.0f,%.0f",rectF.left(), rectF.top(), rectF.width(), rectF.height()));
}

构造Graphics View的各项:

//构造Graphics View的各项
void sample8_4QGraphicsView::iniGraphicsSystem()
{QRectF   rect(-200, -100, 400, 200);//左上角坐标,宽度,高度scene = new QGraphicsScene(rect); //scene逻辑坐标系定义ui.View->setScene(scene);//画一个矩形框,大小等于sceneQGraphicsRectItem *item = new QGraphicsRectItem(rect); //矩形框正好等于scene的大小item->setFlags(QGraphicsItem::ItemIsSelectable     //可选,可以有焦点,但是不能移动| QGraphicsItem::ItemIsFocusable);QPen    pen;pen.setWidth(2);item->setPen(pen);//item->setPos(500,0);//缺省位置在scene的(0,0)scene->addItem(item);//一个位于scene中心的椭圆,测试局部坐标QGraphicsEllipseItem *item2 = new QGraphicsEllipseItem(-100, -50, 200, 100); //矩形框内创建椭圆,绘图项的局部坐标,左上角(-100,-50),宽200,高100item2->setPos(0, 0);//设置椭圆中心位置item2->setBrush(QBrush(Qt::blue));item2->setFlags(QGraphicsItem::ItemIsMovable //可移动| QGraphicsItem::ItemIsSelectable  //可选择| QGraphicsItem::ItemIsFocusable); //可获得焦点scene->addItem(item2);//一个圆,中心位于scene的边缘QGraphicsEllipseItem *item3 = new QGraphicsEllipseItem(-50, -50, 100, 100); //矩形框内创建椭圆,绘图项的局部坐标,左上角(-100,-50),宽200,高100item3->setPos(rect.right(), rect.bottom());//设置圆中心位置item3->setBrush(QBrush(Qt::red));item3->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);scene->addItem(item3);scene->clearSelection();//item->setSelected(true);//ui.View->setDragMode(QGraphicsView::RubberBandDrag);
}

运行结果如下:

"QWGraphicsView.cpp" 文件代码如下:

#include "QWGraphicsView.h"#include    <QMouseEvent>
#include    <QPoint>QWGraphicsView::QWGraphicsView(QWidget *parent): QGraphicsView(parent)
{
}QWGraphicsView::~QWGraphicsView()
{
}//鼠标移动事件
void QWGraphicsView::mouseMoveEvent(QMouseEvent *event)
{QPoint point = event->pos();//QGraphicsView的坐标emit mouseMovePoint(point); //释放信号QGraphicsView::mouseMoveEvent(event);
}//鼠标左键按下事件
void QWGraphicsView::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){QPoint point = event->pos();//QGraphicsView的坐标emit mouseClicked(point);//释放信号}QGraphicsView::mousePressEvent(event);
}

"sample8_4QGraphicsView.cpp"  文件代码如下:

#include "sample8_4QGraphicsView.h"#include  "ui_sample8_4QGraphicsView.h"
#include   <QGraphicsEllipseItem>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")sample8_4QGraphicsView::sample8_4QGraphicsView(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);labViewCord = new QLabel("View 坐标:");labViewCord->setMinimumWidth(150);ui.statusBar->addWidget(labViewCord);labSceneCord = new QLabel("Scene 坐标:");labSceneCord->setMinimumWidth(150);ui.statusBar->addWidget(labSceneCord);labItemCord = new QLabel("Item 坐标:");labItemCord->setMinimumWidth(150);ui.statusBar->addWidget(labItemCord);ui.View->setCursor(Qt::CrossCursor);ui.View->setMouseTracking(true);ui.View->setDragMode(QGraphicsView::RubberBandDrag);QObject::connect(ui.View, SIGNAL(mouseMovePoint(QPoint)),this, SLOT(on_mouseMovePoint(QPoint)));QObject::connect(ui.View, SIGNAL(mouseClicked(QPoint)),this, SLOT(on_mouseClicked(QPoint)));iniGraphicsSystem();
}//鼠标移动事件,point是 GraphicsView的坐标,物理坐标
void sample8_4QGraphicsView::on_mouseMovePoint(QPoint point)
{labViewCord->setText(QString::asprintf("View 坐标:%d,%d", point.x(), point.y()));QPointF pointScene = ui.View->mapToScene(point); //转换到Scene坐标labSceneCord->setText(QString::asprintf("Scene 坐标:%.0f,%.0f", pointScene.x(), pointScene.y()));
}//鼠标单击事件
void sample8_4QGraphicsView::on_mouseClicked(QPoint point)
{QPointF pointScene = ui.View->mapToScene(point); //转换到Scene坐标QGraphicsItem  *item = NULL;item = scene->itemAt(pointScene, ui.View->transform()); //获取光标下的绘图项if (item != NULL) //有绘图项{QPointF pointItem = item->mapFromScene(pointScene); //转换为绘图项的局部坐标labItemCord->setText(QString::asprintf("Item 坐标:%.0f,%.0f", pointItem.x(), pointItem.y()));}
}//构造Graphics View的各项
void sample8_4QGraphicsView::iniGraphicsSystem()
{QRectF   rect(-200, -100, 400, 200);//左上角坐标,宽度,高度scene = new QGraphicsScene(rect); //scene逻辑坐标系定义ui.View->setScene(scene);//画一个矩形框,大小等于sceneQGraphicsRectItem *item = new QGraphicsRectItem(rect); //矩形框正好等于scene的大小item->setFlags(QGraphicsItem::ItemIsSelectable     //可选,可以有焦点,但是不能移动| QGraphicsItem::ItemIsFocusable);QPen    pen;pen.setWidth(2);item->setPen(pen);//item->setPos(500,0);//缺省位置在scene的(0,0)scene->addItem(item);//一个位于scene中心的椭圆,测试局部坐标QGraphicsEllipseItem *item2 = new QGraphicsEllipseItem(-100, -50, 200, 100); //矩形框内创建椭圆,绘图项的局部坐标,左上角(-100,-50),宽200,高100item2->setPos(0, 0);//设置椭圆中心位置item2->setBrush(QBrush(Qt::blue));item2->setFlags(QGraphicsItem::ItemIsMovable //可移动| QGraphicsItem::ItemIsSelectable  //可选择| QGraphicsItem::ItemIsFocusable); //可获得焦点scene->addItem(item2);//一个圆,中心位于scene的边缘QGraphicsEllipseItem *item3 = new QGraphicsEllipseItem(-50, -50, 100, 100); //矩形框内创建椭圆,绘图项的局部坐标,左上角(-100,-50),宽200,高100item3->setPos(rect.right(), rect.bottom());//设置圆中心位置item3->setBrush(QBrush(Qt::red));item3->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);scene->addItem(item3);scene->clearSelection();//item->setSelected(true);//ui.View->setDragMode(QGraphicsView::RubberBandDrag);
}//窗口变化大小时的事件
void sample8_4QGraphicsView::resizeEvent(QResizeEvent *event)
{ Q_UNUSED(event);//Graphics View坐标,左上角总是(0,0),宽度=,长度=ui.labViewSize->setText(QString::asprintf("Graphics View坐标,左上角总是(0,0),宽度=%d,高度=%d",ui.View->width(), ui.View->height()));QRectF  rectF = ui.View->sceneRect(); //Scene的矩形区ui.LabSceneRect->setText(QString::asprintf("QGraphicsView::sceneRect=(Left,Top,Width,Height)=%.0f,%.0f,%.0f,%.0f",rectF.left(), rectF.top(), rectF.width(), rectF.height()));
}

"main.cpp"  文件代码如下:

#include "sample8_4QGraphicsView.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);sample8_4QGraphicsView w;w.show();return a.exec();
}

《Qt5/6 C++开发指南》


http://www.mrgr.cn/news/78059.html

相关文章:

  • DTC独立站的机遇与挑战
  • 详解Oracle表的类型(二)
  • qt添加模块
  • vue页面成绩案例(for渲染表格/删除/添加/统计总分/平均分/不及格显红色/输入内容去首尾空格trim/输入内容转数字number)
  • VSCode 新建 Python 包/模块 Pylance 无法解析
  • 高新技术行业中的知识管理:关键性、挑战、策略及工具应用
  • ubuntu中使用ffmpeg和nginx推http hls视频流
  • 大表建/重建索引
  • 【ONE·基础算法 || 动态规划(二)】
  • C++ —— 以真我之名 如飞花般绚丽 - 智能指针
  • 极智嘉嵌入式面试题及参考答案
  • 不同查询构建器的使用方式(Mybatis、Mybatis-Plus、Mybatis-Flex、Spring Data JPA、QueryDsl)
  • 【Unity基础】如何选择渲染管线?
  • Failed to find SV in PRN block of SINEX file (Name svnav.dat)
  • [OpenGL]使用OpenGL+OIT实现透明效果
  • 内存不足引发C++程序闪退崩溃问题的分析与总结
  • 2024 年:Kubernetes 包管理的新前沿
  • AI:电商平台销售效率提升的魔法钥匙
  • 深度学习笔记1:自动微分与神经网络实现(附代码)
  • Springboot 整合 Java DL4J 搭建智能问答系统
  • 小米C++ 面试题及参考答案下(120道面试题覆盖各种类型八股文)
  • Django 自定义路由转换器
  • 免费下载 | 2025中国5G产业全景图谱报告
  • Leetcode647. 回文子串(HOT100)
  • 【可解释性机器学习】基于SHAP进行特征选择和贡献度计算
  • AI-Talk开发板之Camera