QGraphicsScene类提供了一个用于管理大量2D图形项目的界面。
Header: | #include <QGraphicsScene> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
Inherits: | QObject |
详细描述>>>
Public Types
enum | ItemIndexMethod { BspTreeIndex, NoIndex } |
enum | SceneLayer { ItemLayer, BackgroundLayer, ForegroundLayer, AllLayers } |
flags | SceneLayers |
Properties
- backgroundBrush : QBrush
此属性保存该场景的背景笔刷。
设置此属性,可将场景的背景更改为不同的颜色、渐变或纹理。默认的背景笔刷是Qt::NoBrush。背景被绘制在项目的前面(后面)。
QGraphicsScene scene; QGraphicsView view(&scene); view.show(); // a blue background scene.setBackgroundBrush(Qt::blue); // a gradient background QRadialGradient gradient(0, 0, 10); gradient.setSpread(QGradient::RepeatSpread); scene.setBackgroundBrush(gradient);
QGraphicsScene::render() 调用绘制背景drawBackground() 来绘制场景背景。要更详细地控制如何绘制的背景,您可以在图形场景QGraphicsScene的子类中重新实现 drawBackground() 。
访问函数:
QBrush backgroundBrush() const void setBackgroundBrush(const QBrush &brush)
- bspTreeDepth : int
此属性保存QGraphicsScene的BSP索引树的深度
当使用NoIndex时,此属性无效。
此值决定QGraphicsScene的BSP树的深度。深度直接影响QGraphicsScene的性能和内存使用;后者随着树的深度呈指数级增长。有了合适树形深度,场景可以立即确定项目的位置,即使是有数千或数百万个图形元素的场景。
默认情况下,该值为0,在这种情况下,Qt将根据场景中图形的大小、位置和数量来猜测一个合理的默认深度。但如果这些参数频繁变化,那么在内部重新调整深度时,速度可能会变慢。您可以通过设置此属性来固定树的深度来避免潜在的减速。
树的深度和场景矩形的大小决定了场景划分的粒度。每个场景段的大小由以下算法确定:
QSizeF segmentSize = sceneRect().size() / pow(2, depth - 1);
当每个段包含 0 到 10 个项目时,BSP 树具有最佳大小。
访问函数:
int bspTreeDepth() const void setBspTreeDepth(int depth)
- focusOnTouch : bool
这是一个与触控板相关的属性,它记录着在接收到触摸开始事件时,项目是否获得焦点。
访问函数:
bool focusOnTouch() const void setFocusOnTouch(bool enabled)
- font : QFont
此属性保存场景的默认字体,场景字体默认为QApplication::font。
如果场景的字体发生更改,无论是直接通过 setFont() 还是间接更改应用程序字体,QGraphicsScene都会首先向自身发送一个FontChange 事件,然后将此事件发送到场景中的所有顶级控件。这些项目通过将自己的字体解析到场景中来做出响应,然后它们会通知其子项,子项会再次通知其子项,依此类推,直到所有控件都更新了字体。
更改场景字体(直接或间接通过QApplication::setFont())自动安排重新绘制整个场景。
Access functions:
QFont font() const void setFont(const QFont &font)
- foregroundBrush : QBrush
- itemIndexMethod : ItemIndexMethod
- minimumRenderSize : qreal
- palette : QPalette
- sceneRect : QRectF
- stickyFocus : bool
Public Functions
QGraphicsScene(QObject *parent = nullptr) | |
QGraphicsScene(const QRectF &sceneRect, QObject *parent = nullptr) | |
QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = nullptr) | |
virtual | ~QGraphicsScene() |
QGraphicsItem * | activePanel() const |
QGraphicsWidget * | activeWindow() const |
QGraphicsEllipseItem * | addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
QGraphicsEllipseItem * | addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
void | addItem(QGraphicsItem *item) |
QGraphicsLineItem * | addLine(const QLineF &line, const QPen &pen = QPen()) |
QGraphicsLineItem * | addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen = QPen()) |
QGraphicsPathItem * | addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
QGraphicsPixmapItem * | addPixmap(const QPixmap &pixmap) |
QGraphicsPolygonItem * | addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
QGraphicsRectItem * | addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
QGraphicsRectItem * | addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush()) |
QGraphicsSimpleTextItem * | addSimpleText(const QString &text, const QFont &font = QFont()) |
QGraphicsTextItem * | addText(const QString &text, const QFont &font = QFont()) |
QGraphicsProxyWidget * | addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags()) |
QBrush | backgroundBrush() const |
int | bspTreeDepth() const |
void | clearFocus() |
QList<QGraphicsItem *> | collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const |
QGraphicsItemGroup * | createItemGroup(const QList<QGraphicsItem *> &items) |
void | destroyItemGroup(QGraphicsItemGroup *group) |
QGraphicsItem * | focusItem() const |
bool | focusOnTouch() const |
QFont | font() const |
QBrush | foregroundBrush() const |
bool | hasFocus() const |
qreal | height() const |
virtual QVariant | inputMethodQuery(Qt::InputMethodQuery query) const |
void | invalidate(qreal x, qreal y, qreal w, qreal h, QGraphicsScene::SceneLayers layers = AllLayers) |
bool | isActive() const |
QGraphicsItem * | itemAt(const QPointF &position, const QTransform &deviceTransform) const |
QGraphicsItem * | itemAt(qreal x, qreal y, const QTransform &deviceTransform) const |
QGraphicsScene::ItemIndexMethod | itemIndexMethod() const |
QList<QGraphicsItem *> | items(Qt::SortOrder order = Qt::DescendingOrder) const |
QList<QGraphicsItem *> | items(const QPointF &pos, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const |
QList<QGraphicsItem *> | items(const QRectF &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const |
QList<QGraphicsItem *> | items(const QPolygonF &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const |
QList<QGraphicsItem *> | items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const |
QList<QGraphicsItem *> | items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform = QTransform()) const |
QRectF | itemsBoundingRect() const |
qreal | minimumRenderSize() const |
QGraphicsItem * | mouseGrabberItem() const |
QPalette | palette() const |
void | removeItem(QGraphicsItem *item) |
void | render(QPainter *painter, const QRectF &target = QRectF(), const QRectF &source = QRectF(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio) |
QRectF | sceneRect() const |
QList<QGraphicsItem *> | selectedItems() const |
QPainterPath | selectionArea() const |
bool | sendEvent(QGraphicsItem *item, QEvent *event) |
void | setActivePanel(QGraphicsItem *item) |
void | setActiveWindow(QGraphicsWidget *widget) |
void | setBackgroundBrush(const QBrush &brush) |
void | setBspTreeDepth(int depth) |
void | setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason) |
void | setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason) |
void | setFocusOnTouch(bool enabled) |
void | setFont(const QFont &font) |
void | setForegroundBrush(const QBrush &brush) |
void | setItemIndexMethod(QGraphicsScene::ItemIndexMethod method) |
void | setMinimumRenderSize(qreal minSize) |
void | setPalette(const QPalette &palette) |
void | setSceneRect(const QRectF &rect) |
void | setSceneRect(qreal x, qreal y, qreal w, qreal h) |
void | setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform) |
void | setSelectionArea(const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform()) |
void | setStickyFocus(bool enabled) |
void | setStyle(QStyle *style) |
bool | stickyFocus() const |
QStyle * | style() const |
void | update(qreal x, qreal y, qreal w, qreal h) |
QList<QGraphicsView *> | views() const |
qreal | width() const |
Public Slots
void | advance() |
void | clear() |
void | clearSelection() |
void | invalidate(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = AllLayers) |
void | update(const QRectF &rect = QRectF()) |
Signals
void | changed(const QList<QRectF> ®ion) |
void | focusItemChanged(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason) |
void | sceneRectChanged(const QRectF &rect) |
void | selectionChanged() |
Protected Functions
virtual void | contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) |
virtual void | dragEnterEvent(QGraphicsSceneDragDropEvent *event) |
virtual void | dragLeaveEvent(QGraphicsSceneDragDropEvent *event) |
virtual void | dragMoveEvent(QGraphicsSceneDragDropEvent *event) |
virtual void | drawBackground(QPainter *painter, const QRectF &rect) |
virtual void | drawForeground(QPainter *painter, const QRectF &rect) |
virtual void | dropEvent(QGraphicsSceneDragDropEvent *event) |
virtual void | focusInEvent(QFocusEvent *focusEvent) |
virtual void | focusOutEvent(QFocusEvent *focusEvent) |
virtual void | helpEvent(QGraphicsSceneHelpEvent *helpEvent) |
virtual void | inputMethodEvent(QInputMethodEvent *event) |
virtual void | keyPressEvent(QKeyEvent *keyEvent) |
virtual void | keyReleaseEvent(QKeyEvent *keyEvent) |
virtual void | mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) |
virtual void | mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) |
virtual void | mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) |
virtual void | mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) |
virtual void | wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) |
Reimplemented Protected Functions
virtual bool | event(QEvent *event) override |
virtual bool | eventFilter(QObject *watched, QEvent *event) override |
Protected Slots
virtual bool | focusNextPrevChild(bool next) |