QGraphicsPixmapItem Class
QGraphicsPixmapItem 类提供了一个像素图项,您可以将其添加到QGraphicsScene 中。更多内容...
| 头文件: | #include <QGraphicsPixmapItem> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
| qmake: | QT += widgets |
| 继承自: | QGraphicsItem |
公共类型
公共函数
| QGraphicsPixmapItem(QGraphicsItem *parent = nullptr) | |
| QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr) | |
| virtual | ~QGraphicsPixmapItem() |
| QPointF | offset() const |
| QPixmap | pixmap() const |
| void | setOffset(const QPointF &offset) |
| void | setOffset(qreal x, qreal y) |
| void | setPixmap(const QPixmap &pixmap) |
| void | setShapeMode(QGraphicsPixmapItem::ShapeMode mode) |
| void | setTransformationMode(Qt::TransformationMode mode) |
| QGraphicsPixmapItem::ShapeMode | shapeMode() const |
| Qt::TransformationMode | transformationMode() const |
重新实现的公共函数
| virtual QRectF | boundingRect() const override |
| virtual bool | contains(const QPointF &point) const override |
| virtual bool | isObscuredBy(const QGraphicsItem *item) const override |
| virtual QPainterPath | opaqueArea() const override |
| virtual void | paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override |
| virtual QPainterPath | shape() const override |
| virtual int | type() const override |
详细说明
要设置项的位图,请将QPixmap 传递给QGraphicsPixmapItem的构造函数,或调用setPixmap()函数。pixmap()函数返回当前的位图。
QGraphicsPixmapItem利用pixmap的可选Alpha蒙版,为boundingRect()、shape()和contains()提供了合理的实现。

像素图绘制在项目坐标 (0, 0) 处,该坐标由offset() 返回。您可以通过调用setOffset() 来更改绘制偏移量。
您可以通过调用setTransformationMode() 来设置位图的变换模式。默认情况下使用Qt::FastTransformation ,该模式提供快速但非平滑的缩放。Qt::SmoothTransformation 会在绘图器上启用QPainter::SmoothPixmapTransform ,其质量取决于平台和视口。通常,其效果不如直接调用 QPixmap::scale() 好。 调用transformationMode() 可获取该项的当前变换模式。
另请参阅 QGraphicsPathItem 、QGraphicsRectItem 、QGraphicsEllipseItem 、QGraphicsTextItem 、QGraphicsPolygonItem 、QGraphicsLineItem 以及Graphics View Framework。
成员类型文档
enum QGraphicsPixmapItem::ShapeMode
此枚举描述了QGraphicsPixmapItem 如何计算其形状和不透明区域。
默认值为 MaskShape。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphicsPixmapItem::MaskShape | 0 | 形状由调用QPixmap::mask() 确定。该形状仅包括位图的不透明像素。但是,由于形状更为复杂,因此它可能比其他模式运行得更慢,并且占用更多的内存。 |
QGraphicsPixmapItem::BoundingRectShape | 1 | 该形状通过追踪位图的轮廓来确定。这是最快的形状模式,但它不会考虑位图上的任何透明区域。 |
QGraphicsPixmapItem::HeuristicMaskShape | 2 | 形状通过调用QPixmap::createHeuristicMask()来确定。其性能和内存消耗与MaskShape类似。 |
[anonymous] enum
虚拟函数type() 返回的值。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphicsPixmapItem::Type | 7 | 一个图形位图项 |
成员函数文档
[explicit] QGraphicsPixmapItem::QGraphicsPixmapItem(QGraphicsItem *parent = nullptr)
创建一个 QGraphicsPixmapItem 对象。parent 将作为参数传递给QGraphicsItem 的构造函数。
另请参阅 QGraphicsScene::addItem()。
[explicit] QGraphicsPixmapItem::QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr)
使用pixmap 作为默认像素图,创建一个QGraphicsPixmapItem。parent 将被传递给QGraphicsItem 的构造函数。
另请参阅 QGraphicsScene::addItem()。
[virtual noexcept] QGraphicsPixmapItem::~QGraphicsPixmapItem()
[override virtual] QRectF QGraphicsPixmapItem::boundingRect() const
重新实现了:QGraphicsItem::boundingRect() const。
[override virtual] bool QGraphicsPixmapItem::contains(const QPointF &point) const
重写了:QGraphicsItem::contains(const QPointF &point) const。
[override virtual] bool QGraphicsPixmapItem::isObscuredBy(const QGraphicsItem *item) const
重写了:QGraphicsItem::isObscuredBy(const QGraphicsItem *item) const。
QPointF QGraphicsPixmapItem::offset() const
返回像素图项的偏移量,该偏移量定义了像素图左上角的位置(以本地坐标系表示)。
另请参阅 ` setOffset()`。
[override virtual] QPainterPath QGraphicsPixmapItem::opaqueArea() const
重新实现了:QGraphicsItem::opaqueArea() const。
[override virtual] void QGraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
重写了:QGraphicsItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)。
QPixmap QGraphicsPixmapItem::pixmap() const
返回该项的像素图,如果未设置像素图,则返回一个无效的QPixmap 。
另请参阅 setPixmap()。
void QGraphicsPixmapItem::setOffset(const QPointF &offset)
将像素图项的偏移量设置为offset 。QGraphicsPixmapItem 将使用offset 作为其左上角坐标来绘制该像素图。
另请参阅 offset()。
void QGraphicsPixmapItem::setOffset(qreal x, qreal y)
此便捷函数等同于调用 setOffset(QPointF(x,y))。
void QGraphicsPixmapItem::setPixmap(const QPixmap &pixmap)
将该项的位图设置为pixmap 。
另请参阅 pixmap()。
void QGraphicsPixmapItem::setShapeMode(QGraphicsPixmapItem::ShapeMode mode)
将项的形状模式设置为mode 。形状模式描述了QGraphicsPixmapItem 如何计算其形状。默认模式为MaskShape 。
void QGraphicsPixmapItem::setTransformationMode(Qt::TransformationMode mode)
将像素图项的变换模式设置为mode ,并切换该项的更新状态。默认模式为Qt::FastTransformation ,该模式提供快速变换但不进行平滑处理。
Qt::SmoothTransformation 在绘图器上启用QPainter::SmoothPixmapTransform ,其质量取决于平台和视口。通常,其效果不如直接调用 QPixmap::scale() 好。
另请参阅 transformationMode()。
[override virtual] QPainterPath QGraphicsPixmapItem::shape() const
重新实现了:QGraphicsItem::shape() const。
QGraphicsPixmapItem::ShapeMode QGraphicsPixmapItem::shapeMode() const
返回该项的形状模式。形状模式描述了QGraphicsPixmapItem 如何计算其形状。默认模式为MaskShape 。
另请参阅 setShapeMode() 和ShapeMode 。
Qt::TransformationMode QGraphicsPixmapItem::transformationMode() const
返回位图的变换模式。默认模式为Qt::FastTransformation ,该模式可实现快速变换,且不进行平滑处理。
另请参阅 setTransformationMode()。
[override virtual] int QGraphicsPixmapItem::type() const
重新实现了:QGraphicsItem::type() const。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.