QT5笔记: 35. QGraphicsView 视图

三者关系:View中可以有多个Scene,Scene放在View中,Scene可以装多个Item图形
若要实现鼠标事件,则需要重写QGraphicsView
MyGraphicsView.h
#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H
#include <QGraphicsView>
#include <QMouseEvent>
class MyGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyGraphicsView(QWidget *parent = nullptr);
signals:
void mouseMoveEvent(QPoint point);//发送鼠标事件
void mouseClickEvent(QPoint point);
// QWidget interface
protected:
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
};
#endif // MYGRAPHICSVIEW_H
MyGraphicsView.cpp
窗口#include "mygraphicsview.h"
MyGraphicsView::MyGraphicsView(QWidget *parent) : QGraphicsView(parent)
{
}
void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
emit(mouseMoveEvent(event->pos()));
QGraphicsView::mouseMoveEvent(event);
}
void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
emit(mouseClickEvent(event->pos()));
}
QGraphicsView::mouseReleaseEvent(event);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsScene>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *graphicsScene;//场景
void initGraphicsView();
private slots:
void onMouseMove(QPoint point);
void onMouseClick(QPoint point);
// QWidget interface
protected:
void resizeEvent(QResizeEvent *event) override;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setCursor(Qt::CrossCursor);
ui->graphicsView->setMouseTracking(true);
initGraphicsView();
connect(ui->graphicsView, SIGNAL(mouseMoveEvent(QPoint)), this, SLOT(onMouseMove(QPoint)));
connect(ui->graphicsView, SIGNAL(mouseClickEvent(QPoint)), this, SLOT(onMouseClick(QPoint)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initGraphicsView()
{
QRectF rect = QRectF(-200, -100, 400, 200);
graphicsScene = new QGraphicsScene(rect);
ui->graphicsView->setScene(graphicsScene);//设置场景
QPen pen;
pen.setWidth(2);
pen.setStyle(Qt::SolidLine);
QBrush brush;
brush.setColor(QColor(100, 200, 150, 150));
brush.setStyle(Qt::CrossPattern);
QGraphicsRectItem *item = new QGraphicsRectItem(rect);//画矩形
item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
item->setPen(pen);
graphicsScene->addItem(item);
QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(-100, -50, 200, 100);//椭圆
ellipseItem->setPen(pen);
ellipseItem->setBrush(brush);
ellipseItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
graphicsScene->addItem(ellipseItem);
QGraphicsEllipseItem *circleItem = new QGraphicsEllipseItem(-50, -50, 100, 100);//圆
circleItem->setPos(200, 100);
circleItem->setPen(pen);
brush.setStyle(Qt::SolidPattern);
circleItem->setBrush(brush);
circleItem->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable);//可选 | 可聚焦 | 可点击后移动(拖拽)
graphicsScene->addItem(circleItem);
}
void MainWindow::onMouseMove(QPoint point)
{
QPointF scenePoint = ui->graphicsView->mapToScene(point);//graphicsView映射到scene的坐标
QString info = QString::asprintf("View坐标:(%d , %d); Scene坐标:(%.0f , %.0f)", point.x(), point.y(), scenePoint.x(), scenePoint.y());
ui->statusbar->showMessage(info);
}
void MainWindow::onMouseClick(QPoint point)
{
QPointF scenePoint = ui->graphicsView->mapToScene(point);
QGraphicsItem *item = nullptr;
item = graphicsScene->itemAt(scenePoint, ui->graphicsView->transform());
if (item != nullptr) {
QPointF itemPoint = item->mapFromScene(scenePoint);//求当前点击位置相对于当前图元的坐标,图元中心为原点
QString message = ui->statusbar->currentMessage() + QString::asprintf("; Item 坐标:(%.0f , %.0f)", itemPoint.x(), itemPoint.y());
ui->statusbar->showMessage(message);
}
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QString graphicsStr = ui->label->text();
QString sceneStr = ui->label_2->text();
int w = ui->graphicsView->width();
int h = ui->graphicsView->height();
ui->label->setText(QString::asprintf("Graphics View坐标,左上角总是(0,0),宽度= %d,长度= %d", w, h));
QRectF rect = this->graphicsScene->sceneRect();
ui->label_2->setText(QString::asprintf("QGraphicsView::sceneRect=(%.0f, %.0f, %.0f, %.0f)", rect.x(), rect.y(), rect.width(), rect.height()));
}
界面:

QT5笔记: 35. QGraphicsView 视图的更多相关文章
- Spring实战第六章学习笔记————渲染Web视图
Spring实战第六章学习笔记----渲染Web视图 理解视图解析 在之前所编写的控制器方法都没有直接产生浏览器所需的HTML.这些方法只是将一些数据传入到模型中然后再将模型传递给一个用来渲染的视图. ...
- leetcode笔记——35.搜索插入位置 - CrowFea
0.问题描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 12 输入: [1,3 ...
- asp.net core mvc视频A:笔记3-5.视图数据共享之TempData
前几节讲的都是单页面数据共享,从本节开始讲跨页面数据共享 创建项目3.5,新建控制器 代码 控制器 设置TempData 另一个视图中读取TempData数据 运行 此时如果刷新页面,页面中的内容“张 ...
- Django笔记&教程 2-3 视图(view)函数介绍
Django 自学笔记兼学习教程第2章第3节--视图(view)函数介绍 点击查看教程总目录 参考文献:https://docs.djangoproject.com/en/2.2/topics/htt ...
- Django笔记&教程 2-4 视图常用
Django 自学笔记兼学习教程第2章第4节--视图常用 点击查看教程总目录 1 - shortcut 视图函数需要返回一个HttpResponse对象或者其子类对象. 不过很多时候直接手写建立一个H ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- OpenGL学习笔记4——模型视图变换
以日月地为例的一个模型视图变换.绕了比较多的弯路,下面是几个注意点总结. 注意点: 1.GL函数对模型的操作是基于当前局部坐标系,即模型坐标系而非世界坐标系,二者只在第一次初始化完毕之后才重合: 2. ...
- MySQL学习笔记——索引和视图
索引(index)和管理索引 模式中的一个数据库对象 作用:在数据库中用来加速对表的查询 创建:自动在主键和唯一键上面创建索引 通过使用快速路径访问方法快速定位数据,减少了磁盘的I/O 与表独立存放, ...
- 《BI项目笔记》数据源视图设置
目的数据源视图是物理源数据库和分析维度与多维数据集之间的逻辑数据模型.在创建数据源视图时,需要在源数据库中指定包含创建维度和多维数据集所需要的数据表格和视图.BIDS与数据库连接,读取表格和视图定义, ...
- [terry笔记]物化视图 materialized view基础学习
一.物化视图定义摘录: 物化视图是包括一个查询结果的数据库对像(由系统实现定期刷新数据),物化视图不是在使用时才读取,而是预先计算并保存表连接或聚集等耗时较多的操作结果,这样在查询时大大提高了 ...
随机推荐
- 【Amadeus原创】wordpress批量更新图片路径的方法
连上wordpress数据库,怼一句: UPDATE wp_posts SET post_content = REPLACE( post_content, '旧域名', '新域名' );
- 如何为在线客服系统的 Web Api 后台主程序添加 Bootstrap 启动页面
背景 我在业余时间开发了一款自己的独立产品:升讯威在线客服与营销系统.这个系统的核心后台主程序,在最早期是完全没有页面,经常有朋友部署之后,一访问是 404,以为没有部署成功.我一看这肯定不行啊,可后 ...
- 腾讯云 TStor 私有云存储获统信+海光/兆芯官方认证
腾讯云 TStor 是一款分布式存储产品,致力于解决私有云.混合云下的各类存储需求.产品紧跟国内信创生态,持续加强自主可控能力.目前,TStor 已经支持国内主流国产操作系统和硬件,如中标麒麟操作系统 ...
- docker.sock: connect: permission denied 解决
问题描述xjun@DESKTOP-L2R4GKN:~$ docker run -it hello-worlddocker: Got permission denied while trying to ...
- Base58在java程序中应用
Base58是用于Bitcoin中使用的一种独特的编码方式,主要用于产生Bitcoin的钱包地址. 相比Base64,Base58不使用数字"0",字母大写"O" ...
- 【转载】 SpringBoot声明式事务的简单运用
https://blog.csdn.net/justry_deng/article/details/80828180 关于事物的基本概念等这里就不介绍了. Spring声明式事物的实现,有两种方式:第 ...
- vertx 获取请求参数
表单登录(GET) <form action="/login"> <input type="text" name="user ...
- 【网络安全】Linux基础详解
声明:学习视频来自 b 站 up 主 泷羽 sec,如涉及侵权马上删除文章 声明:本文主要用作技术分享,所有内容仅供参考.任何使用或依赖于本文信息所造成的法律后果均与本人无关.请读者自行判断风险,并遵 ...
- Qt/C++音视频开发54-视频监控控件的极致设计
一.前言 跌跌撞撞摸爬滚打一步步迭代完善到今天,这个视频监控控件的设计,在现阶段水平上个人认为是做的最棒的(稍微自恋一下),理论上来说应该可以用5年不用推翻重写,推翻重写当然也是程序员爱干的事情,这个 ...
- Qt编写物联网管理平台46-云端数据同步
一.前言 在上一篇文章说的采集数据转发的基础上,针对方案一还做了云端数据同步功能,满足各式各样的用户需求.云端数据库同步,相当于把本地采集到的数据实时存储到云端,至于这些记录到了云端后什么用途,客户端 ...