QT笔记- QGraphicsView类函数mapFromScene()、mapToScene()说明

QPoint mapFromScene(x, y):

该函数返回某一个位置的坐标值。

首先我们要知道,同一个位置用不同的坐标系来衡量,其坐标值会是不同的。

函数的参数(x, y)是利用QGraphicsScene中的坐标系来衡量的一个位置的坐标值, 函数的返回值是该位置在QGraphicsView窗口坐标系中的坐标值。

一句话总结:函数返回的是一个在QGraphicsScene坐标系中的位置(x,y)在QGraphicsView窗口坐标系中的坐标值。

如果将这个过程反过来,那就是函数QPoint  mapToScene(x, y)的作用了。

注意: QGraphicsView有两套坐标系,详见https://blog.csdn.net/qq_43058397/article/details/109143823

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QGraphicsView 是一个基于Qt的图形视图框架,可以方便的展现和编辑大量的2D图形元素。下面是QGraphicsView的一些常见用法: 1. 创建QGraphicsView对象 ```python from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene from PyQt5.QtCore import Qt # 创建QGraphicsView对象 view = QGraphicsView() # 创建QGraphicsScene对象 scene = QGraphicsScene() # 将scene设置为view的场景 view.setScene(scene) # 设置view的背景色 view.setBackgroundBrush(Qt.white) ``` 2. 添加图形元素到场景中 ```python from PyQt5.QtGui import QPen, QColor, QBrush from PyQt5.QtCore import QPointF, QRectF # 创建QGraphicsRectItem对象 rect_item = QGraphicsRectItem(QRectF(0, 0, 100, 100)) # 设置该图形元素的画笔和画刷 pen = QPen(QColor(255, 0, 0)) brush = QBrush(QColor(0, 255, 0)) rect_item.setPen(pen) rect_item.setBrush(brush) # 将该图形元素添加到场景中 scene.addItem(rect_item) ``` 3. 设置视图参数 ```python # 设置视图的中心点 view.setCenter(QPointF(0, 0)) # 设置视图的缩放比例 view.setTransform(QTransform.fromScale(2, 2)) ``` 4. 处理鼠标和键盘事件 ```python from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QGraphicsView class MyView(QGraphicsView): def __init__(self, parent=None): super(MyView, self).__init__(parent) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: print('left button pressed') elif event.button() == Qt.RightButton: print('right button pressed') def keyPressEvent(self, event): if event.key() == Qt.Key_Up: print('up key pressed') elif event.key() == Qt.Key_Down: print('down key pressed') ``` 以上是QGraphicsView的一些常见用法,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值