本页内容

QGraphicsRectItem Class

QGraphicsRectItem 类提供了一个矩形项,您可以将其添加到QGraphicsScene 中。更多内容...

头文件: #include <QGraphicsRectItem>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承自: QAbstractGraphicsShapeItem

公共类型

enum { Type }

公共函数

QGraphicsRectItem(QGraphicsItem *parent = nullptr)
QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)
QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr)
virtual ~QGraphicsRectItem()
QRectF rect() const
void setRect(const QRectF &rectangle)
void setRect(qreal x, qreal y, qreal width, qreal height)

重新实现的公共函数

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 = nullptr) override
virtual QPainterPath shape() const override
virtual int type() const override

详细说明

要设置项的矩形,请向 QGraphicsRectItem 的构造函数传递一个 `QRectF `,或调用 `setRect()` 函数。`rect()` 函数返回当前的矩形。

在原点附近画一个矩形

QGraphicsRectItem 使用矩形和笔宽来合理实现boundingRect()、shape() 和contains() 函数。paint() 函数使用项目关联的笔和画笔绘制矩形,您可以通过调用setPen() 和setBrush() 函数来设置这些参数。

注意: 无效矩形(例如宽度或高度为负数的矩形)的渲染行为未定义 。如果您无法确定所使用的矩形是否有效(例如,使用来自不可靠来源的数据创建矩形),则应使用QRectF::normalized() 创建归一化矩形,并改用这些矩形。

另请参阅 QGraphicsPathItem 、QGraphicsEllipseItem 、QGraphicsPolygonItem 、QGraphicsTextItem 、QGraphicsLineItem 、QGraphicsPixmapItem 以及Graphics View Framework。

成员类型文档

[anonymous] enum

虚拟函数type() 返回的值。

常量值描述
QGraphicsRectItem::Type3一个图形矩形项

成员函数文档

[explicit] QGraphicsRectItem::QGraphicsRectItem(QGraphicsItem *parent = nullptr)

创建一个 QGraphicsRectItem。将 `parent ` 传递给 `QAbstractGraphicsShapeItem` 的构造函数。

另请参阅 QGraphicsScene::addItem()。

[explicit] QGraphicsRectItem::QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr)

使用rect 作为默认矩形,构建一个QGraphicsRectItem。parent 将作为参数传递给QAbstractGraphicsShapeItem 的构造函数。

另请参阅 QGraphicsScene::addItem()。

[explicit] QGraphicsRectItem::QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr)

构建一个 QGraphicsRectItem,其默认矩形由 (x,y) 定义,并传入给定的width 和height 。

parent 该参数将传递给QAbstractGraphicsShapeItem 的构造函数。

另请参阅 QGraphicsScene::addItem()。

[virtual noexcept] QGraphicsRectItem::~QGraphicsRectItem()

销毁QGraphicsRectItem 。

[override virtual] QRectF QGraphicsRectItem::boundingRect() const

重新实现了:QGraphicsItem::boundingRect() const。

[override virtual] bool QGraphicsRectItem::contains(const QPointF &point) const

重写了:QGraphicsItem::contains(const QPointF &point) const。

[override virtual] bool QGraphicsRectItem::isObscuredBy(const QGraphicsItem *item) const

重写了:QAbstractGraphicsShapeItem::isObscuredBy(const QGraphicsItem *item) const。

[override virtual] QPainterPath QGraphicsRectItem::opaqueArea() const

重新实现了:QAbstractGraphicsShapeItem::opaqueArea() const。

[override virtual] void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)

重写了:QGraphicsItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)。

QRectF QGraphicsRectItem::rect() const

返回该项的矩形。

另请参阅 setRect()。

void QGraphicsRectItem::setRect(const QRectF &rectangle)

将该项的矩形设置为给定的rectangle 。

另请参阅 rect()。

void QGraphicsRectItem::setRect(qreal x, qreal y, qreal width, qreal height)

将该项的矩形设置为由 (x,y) 以及给定的width 和height 所定义的矩形。

此便捷函数等同于调用setRect(QRectF(x, y, width, height))

另请参阅 rect()。

[override virtual] QPainterPath QGraphicsRectItem::shape() const

重新实现了:QGraphicsItem::shape() const。

[override virtual] int QGraphicsRectItem::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.