QGraphicsView的使用,view坐标,scene坐标,item坐标

2024-03-22 17:28

本文主要是介绍QGraphicsView的使用,view坐标,scene坐标,item坐标,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Graphics View绘图构架

  1. QGraphicsScene(场景):可以管理多个图形项
  2. QGraphicsItem(图形项):也就是图元,支持鼠标事件响应。
  3. QGraphicsView(视图):关联场景可以让场景中的所有图形项可视化

QGraphicsView是QT的图形视图组件,在UI设计器的Display Widgets分组

QGraphicsView没有与mouseMoveEvent()相关的信号,需要继承自定义一个派生类

代码演示

工程文件不需要添加模块

注意事项是,graphics需要派生一个类才能用事件等相关信息

所以要新建一个派生类:

mygraphicsview.h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H#include <QObject>
#include <QGraphicsView>class mygraphicsview : public QGraphicsView
{Q_OBJECT
public:explicit mygraphicsview(QWidget *parent = nullptr);
protected:void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);
signals:void mouseMovePoint(QPoint point);void mouseClicked(QPoint point);};#endif // MYGRAPHICSVIEW_H

mygraphicsview.cpp

#include "mygraphicsview.h"#include <QMouseEvent>mygraphicsview::mygraphicsview(QWidget *parent) : QGraphicsView(parent)
{}void mygraphicsview::mousePressEvent(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){QPoint point = event->pos();//view坐标emit mouseClicked(point);}QGraphicsView::mousePressEvent(event);
}void mygraphicsview::mouseMoveEvent(QMouseEvent *event)
{QPoint point = event->pos();emit mouseMovePoint(point);QGraphicsView::mouseMoveEvent(event);
}

 派生类的事件通过信号的方式发送出去,其他地方connect连接信号读取数据。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QLabel>
#include <QMainWindow>
#include <QGraphicsScene>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTprivate:QLabel *labViewCord;QLabel *labSceneCord;QLabel *labItemCord;QGraphicsScene *scene;void initGraphics();
public:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;private slots:void on_mouseMovePoint(QPoint point);void on_mouseClicked(QPoint point);protected:void resizeEvent(QResizeEvent *event);
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"#include "mygraphicsview.h"#include <QGraphicsRectItem>void MainWindow::initGraphics()
{QRectF rect(-200,-100,400,200);scene = new QGraphicsScene(rect);ui->graphicsView->setScene(scene);QGraphicsRectItem *item = new QGraphicsRectItem(rect);item->setFlags(QGraphicsItem::ItemIsFocusable |QGraphicsItem::ItemIsSelectable);QPen pen;pen.setWidth(2);item->setPen(pen);scene->addItem(item);//蓝色椭圆QGraphicsEllipseItem *item2 = new QGraphicsEllipseItem(-100,-50,200,100);item2->setPos(0,0);item2->setFlags(QGraphicsItem::ItemIsFocusable |QGraphicsItem::ItemIsSelectable |QGraphicsItem::ItemIsMovable);item2->setBrush(QBrush(Qt::blue));scene->addItem(item2);//红色小圆QGraphicsEllipseItem *item3 = new QGraphicsEllipseItem(-50,-50,100,100);item3->setPos(rect.right(),rect.bottom());item3->setFlags(QGraphicsItem::ItemIsFocusable |QGraphicsItem::ItemIsSelectable |QGraphicsItem::ItemIsMovable);item3->setBrush(QBrush(Qt::red));scene->addItem(item3);
}MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);labViewCord = new QLabel("View坐标:");labViewCord->setMidLineWidth(150);ui->statusbar->addWidget(labViewCord);labSceneCord = new QLabel("Scene坐标:");labSceneCord->setMidLineWidth(150);ui->statusbar->addWidget(labSceneCord);labItemCord = new QLabel("Item坐标:");labItemCord->setMidLineWidth(150);ui->statusbar->addWidget(labItemCord);ui->graphicsView->setCursor(Qt::CrossCursor);//光标ui->graphicsView->setMouseTracking(true);//跟随鼠标connect(ui->graphicsView,&mygraphicsview::mouseMovePoint,this,&MainWindow::on_mouseMovePoint);connect(ui->graphicsView,&mygraphicsview::mouseClicked,this,&MainWindow::on_mouseClicked);initGraphics();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_mouseMovePoint(QPoint point)
{labViewCord->setText(QString::asprintf("View坐标:%d,%d",point.x(),point.y()));QPointF pointScene = ui->graphicsView->mapToScene(point);labSceneCord->setText(QString::asprintf("Scene坐标:%.0f,%.0f",pointScene.x(),pointScene.y()));
}void MainWindow::on_mouseClicked(QPoint point)
{QPointF pointScene = ui->graphicsView->mapToScene(point);QGraphicsItem *item = NULL;item = scene->itemAt(pointScene,ui->graphicsView->transform());if(item!=NULL){QPointF pointItem = item->mapFromScene(pointScene);labItemCord->setText(QString::asprintf("Item坐标%.0f,%.0f",pointItem.x(),pointItem.y()));}
}void MainWindow::resizeEvent(QResizeEvent *event)
{Q_UNUSED(event)ui->label->setText(QString::asprintf("Graphics View坐标""宽=%d,高=%d",ui->graphicsView->width(),ui->graphicsView->height()));QRectF rect = ui->graphicsView->sceneRect();ui->label_2->setText(QString::asprintf("QGraphicsView::sceneRect=""(L,T,W,H)=%.0f,%.0f,%.0f,%.0f",rect.left(),rect.top(),rect.width(),rect.height()));
}

ui部分要注意,需要吧graphics的窗口提升关联到派生类,不要用他的默认类。

这篇关于QGraphicsView的使用,view坐标,scene坐标,item坐标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/835809

相关文章

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案