转摘Qt 知识:使用 QGraphicsPixmapItem类
目录
[一、使用 QGraphicsPixmapItem的不同](#一、使用 QGraphicsPixmapItem的不同)
[二、 QPixmap 简介](#二、 QPixmap 简介)
一、使用 QGraphicsPixmapItem的不同
首先,QGraphicsPixmapItem是QGraphicsItem类的子类。都可以独立构成item加入scene中,但区别在于,QGraphicsItem可以图形,图像;而QGraphicsPixmapItem是隐式图像QPixmap
类,类似于MFC的CDC图像,可以不在屏幕显示。
二、 [QPixmap](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtGui/QPixmap.html#qpixmap "QPixmap")
简介
QPixmap
类是一种 off-screen 图像表示形式,可以用作绘画设备。使用QLabel
或QAbstractButton
的子类之一(例如QPushButton
和QToolButton
),可以轻松地在屏幕上显示QPixmap
。QLabel
具有pixmap
属性,而QAbstractButton
具有icon
属性。
由于QPixmap
类使用隐式数据共享,因此可以按值传递QPixmap
对象。有关更多信息,请参见隐式数据共享([Implicit Data Sharing](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/overviews/implicit-sharing.html#implicit-sharing "Implicit Data Sharing")
)文档。QPixmap
对象也可以流式传输。
请注意,pixmap 中的像素数据是内部的,并由基础窗口系统管理。由于QPixmap
是QPaintDevice
子类,因此QPainter
可用于直接绘制到 pixmap 上。只能通过QPainter
函数或将QPixmap
转换为QImage
来访问像素。但是,fill()
函数可用于以给定的颜色初始化整个像素图。
有一些函数可以在QImage
和QPixmap
之间进行转换。通常,在将QImage
对象转换为要在屏幕上显示的QPixmap
之前,使用QImage
类加载图像文件,并选择处理图像数据。或者,如果不需要任何操作,则可以将图像文件直接加载到QPixmap
中。
QPixmap
提供了一组函数,这些函数可用于获取有关像素图的各种信息。另外,有几个函数可以转换像素图。
三、QGraphicsPixmapItem详细

QGraphicsPixmapItem
类提供了一个 pixmap
项(注意,不是图形数据),它可以将其添加到 QGraphicsScene
中。
QGraphicsPixmapItem的构建
1)请将QPixmap对象直接
传递给QGraphicsPixmapItem
的构造函数,构成图像对象;
2)或QGraphicsPixmapItem构建后
调用setPixmap()
函数将QPixmap对象载入
。
3)用pixmap()
函数返回当前的像素图。
QGraphicsPixmapItem
使用pixmap
的可选功能函数有:
-
def [offset]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.offset "offset")
() -
def [pixmap]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.pixmap "pixmap")
() -
def [setOffset]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setOffset "setOffset")
(offset) -
def [setOffset]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setOffset "setOffset")
(x, y) -
def [setPixmap]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setPixmap "setPixmap")
(pixmap) -
def [setShapeMode]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setShapeMode "setShapeMode")
(mode) -
def [setTransformationMode]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setTransformationMode "setTransformationMode")
(mode) -
def [shapeMode]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.shapeMode "shapeMode")
() -
def [transformationMode]
(https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.transformationMode "transformationMode")
()
像素图是在项目的 坐标处绘制的,由 [offset()](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.offset "offset()
") 返回。您可以通过调用 [setOffset()](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtWidgets/QGraphicsPixmapItem.html#id0 "setOffset()
") 来更改图形偏移。
您可以通过调用 [setTransformationMode()](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.PySide2.QtWidgets.QGraphicsPixmapItem.setTransformationMode "setTransformationMode()
") 来设置像素图的转换模式。默认情况下,使用FastTransformation
,它提供了快速,平滑的缩放比例。SmoothTransformation
在 painter 上启用SmoothPixmapTransform
,其质量取决于平台和视口。结果通常不如直接调用 QPixmap::scale()
好。调用 transformationMode()
以获取该项目的当前转换模式。
[QGraphicsPixmapItem([parent=None])](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtWidgets/QGraphicsPixmapItem.html#PySide2.QtWidgets.QGraphicsPixmapItem "QGraphicsPixmapItem([parent=None])
"):其中 parent
即 [QGraphicsItem](https://links.jianshu.com/go?to=https://doc.qt.io/qtforpython/PySide2/QtWidgets/QGraphicsItem.html#PySide2.QtWidgets.QGraphicsItem "QGraphicsItem")
的实例。
四、下面写一个使用案例
from xinet import QtWidgets, QtCore, QtGui, RectItem
from xinet.run_qt import run
QColor = QtGui.QColor
QPen = QtGui.QPen
QGraphicsPixmapItem = QtWidgets.QGraphicsPixmapItem
class MainWindow(QtWidgets.QGraphicsView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 设定视图尺寸
#self.resize(600, 600)
# 创建场景
self.scene = QtWidgets.QGraphicsScene()
# x1, y1, w, h
self.item = RectItem(20, 25, 120, 120) # 可塑性矩形
self.scene.addItem(self.item)
self.scene.addItem(RectItem(200, 250, 120, 120))
pix_item = QGraphicsPixmapItem(self.item)
pix_item.setPixmap(QtGui.QPixmap('w.jpg'))
pix_item.setOffset(30, 50)
self.photo = QtGui.QImage("test.jpg")
self.init_Ui()
def drawBackground(self, painter, rect):
super().drawBackground(painter, rect)
if self.photo.isNull():
self.setBackgroundBrush(QColor(0, 0, 200, 100)) # 默认背景颜色
else:
# 设置场景的边界矩形,即可视化区域矩形
_w = self.photo.width()
_h = self.photo.height()
self.setSceneRect(0, 0, _w, _h)
#self.setBackgroundBrush(QColor(0, 0, 200, 30)) # 默认背景颜色
painter.drawImage(0, 0, self.photo)
def init_Ui(self):
# 设定视图的场景
self.setScene(self.scene)
self.fitInView(self.scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
self.setViewportUpdateMode(self.FullViewportUpdate) # 消除重影 移动重影
self.setDragMode(self.RubberBandDrag) # 设置可以进行鼠标的拖拽选择
# 这里是左上角方式显示
self.setAlignment(QtCore.Qt.AlignLeft |
QtCore.Qt.AlignTop)
if __name__ == '__main__':
run(MainWindow)
五、其它相关类
QGraphicsPathItem、QGraphicsRectItem、QGraphicsEllipseItem、QGraphicsTextItem、QGraphicsPolygonItem、QGraphicsLineItem、Graphics View Framework
===========================
【来源: CSDN】
【作者: 无水先生】
【原文链接】 https://yamagota.blog.csdn.net/article/details/125372496
声明:转载此文是出于传递更多信息之目的。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与本网联系,我们将及时更正、删除,谢谢。