Qt 知识:使用 QGraphicsPixmapItem类

目录

一、使用 QGraphicsPixmapItem的不同

二、 QPixmap 简介

三、QGraphicsPixmapItem详细

四、下面写一个使用案例 

五、其它相关类


一、使用 QGraphicsPixmapItem的不同

        首先,QGraphicsPixmapItem是QGraphicsItem类的子类。都可以独立构成item加入scene中,但区别在于,QGraphicsItem可以图形,图像;而QGraphicsPixmapItem是隐式图像QPixmap类,类似于MFC的CDC图像,可以不在屏幕显示。

二、 QPixmap 简介

   QPixmap类是一种 off-screen 图像表示形式,可以用作绘画设备。使用QLabelQAbstractButton的子类之一(例如QPushButtonQToolButton),可以轻松地在屏幕上显示QPixmapQLabel具有pixmap属性,而QAbstractButton具有icon属性。

        由于QPixmap类使用隐式数据共享,因此可以按值传递QPixmap对象。有关更多信息,请参见隐式数据共享(Implicit Data Sharing)文档。QPixmap对象也可以流式传输。

        请注意,pixmap 中的像素数据是内部的,并由基础窗口系统管理。由于QPixmapQPaintDevice子类,因此QPainter可用于直接绘制到 pixmap 上。只能通过QPainter函数或将QPixmap转换为QImage来访问像素。但是,fill()函数可用于以给定的颜色初始化整个像素图。

        有一些函数可以在QImageQPixmap之间进行转换。通常,在将QImage对象转换为要在屏幕上显示的QPixmap之前,使用QImage类加载图像文件,并选择处理图像数据。或者,如果不需要任何操作,则可以将图像文件直接加载到QPixmap中。

  QPixmap提供了一组函数,这些函数可用于获取有关像素图的各种信息。另外,有几个函数可以转换像素图。

三、QGraphicsPixmapItem详细

 

QGraphicsPixmapItem类提供了一个 pixmap 项(注意,不是图形数据),它可以将其添加到 QGraphicsScene 中。

  • QGraphicsPixmapItem的构建

1)请将QPixmap对象直接传递给QGraphicsPixmapItem的构造函数,构成图像对象;

2)或QGraphicsPixmapItem构建后调用setPixmap()函数将QPixmap对象载入

3)用pixmap()函数返回当前的像素图。

QGraphicsPixmapItem使用pixmap的可选功能函数有:

        像素图是在项目的  坐标处绘制的,由 offset() 返回。您可以通过调用 setOffset() 来更改图形偏移。

        您可以通过调用 setTransformationMode() 来设置像素图的转换模式。默认情况下,使用FastTransformation,它提供了快速,平滑的缩放比例。SmoothTransformation 在 painter 上启用SmoothPixmapTransform,其质量取决于平台和视口。结果通常不如直接调用 QPixmap::scale() 好。调用 transformationMode() 以获取该项目的当前转换模式。

QGraphicsPixmapItem([parent=None]):其中 parent 即 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无水先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值