QGraphicsScene露
The
QGraphicsSceneclass provides a surface for managing a large number of 2D graphical items. More鈥

Synopsis露
Functions露
def
activePanel()def
activeWindow()def
addEllipse(rect[, pen=QPen()[, brush=QBrush()]])def
addEllipse(x, y, w, h[, pen=QPen()[, brush=QBrush()]])def
addItem(item)def
addLine(line[, pen=QPen()])def
addLine(x1, y1, x2, y2[, pen=QPen()])def
addPath(path[, pen=QPen()[, brush=QBrush()]])def
addPixmap(pixmap)def
addPolygon(polygon[, pen=QPen()[, brush=QBrush()]])def
addRect(rect[, pen=QPen()[, brush=QBrush()]])def
addRect(x, y, w, h[, pen=QPen()[, brush=QBrush()]])def
addSimpleText(text[, font=QFont()])def
addText(text[, font=QFont()])def
addWidget(widget[, wFlags=Qt.WindowFlags()])def
backgroundBrush()def
bspTreeDepth()def
clearFocus()def
collidingItems(item[, mode=Qt.IntersectsItemShape])def
createItemGroup(items)def
destroyItemGroup(group)def
focusItem()def
focusOnTouch()def
font()def
foregroundBrush()def
hasFocus()def
height()def
invalidate(x, y, w, h[, layers=QGraphicsScene.AllLayers])def
isActive()def
isSortCacheEnabled()def
itemAt(pos, deviceTransform)def
itemAt(x, y, deviceTransform)def
itemIndexMethod()def
items([order=Qt.DescendingOrder])def
items(path[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])def
items(polygon[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])def
items(pos[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])def
items(rect[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])def
items(x, y, w, h, mode, order[, deviceTransform=QTransform()])def
itemsBoundingRect()def
minimumRenderSize()def
mouseGrabberItem()def
palette()def
removeItem(item)def
render(painter[, target=QRectF()[, source=QRectF()[, aspectRatioMode=Qt.KeepAspectRatio]]])def
sceneRect()def
selectedItems()def
selectionArea()def
sendEvent(item, event)def
setActivePanel(item)def
setActiveWindow(widget)def
setBackgroundBrush(brush)def
setBspTreeDepth(depth)def
setFocus([focusReason=Qt.OtherFocusReason])def
setFocusItem(item[, focusReason=Qt.OtherFocusReason])def
setFocusOnTouch(enabled)def
setFont(font)def
setForegroundBrush(brush)def
setItemIndexMethod(method)def
setMinimumRenderSize(minSize)def
setPalette(palette)def
setSceneRect(rect)def
setSceneRect(x, y, w, h)def
setSelectionArea(path, deviceTransform)def
setSelectionArea(path, selectionOperation[, mode=Qt.IntersectsItemShape[, deviceTransform=QTransform()]])def
setSelectionArea(path[, mode=Qt.IntersectsItemShape[, deviceTransform=QTransform()]])def
setSortCacheEnabled(enabled)def
setStickyFocus(enabled)def
setStyle(style)def
stickyFocus()def
style()def
update(x, y, w, h)def
views()def
width()
Virtual functions露
def
contextMenuEvent(event)def
dragEnterEvent(event)def
dragLeaveEvent(event)def
dragMoveEvent(event)def
drawBackground(painter, rect)def
drawForeground(painter, rect)def
dropEvent(event)def
focusInEvent(event)def
focusOutEvent(event)def
helpEvent(event)def
inputMethodEvent(event)def
inputMethodQuery(query)def
keyPressEvent(event)def
keyReleaseEvent(event)def
mouseDoubleClickEvent(event)def
mouseMoveEvent(event)def
mousePressEvent(event)def
mouseReleaseEvent(event)def
wheelEvent(event)
Slots露
def
advance()def
clear()def
clearSelection()def
focusNextPrevChild(next)def
invalidate([rect=QRectF()[, layers=QGraphicsScene.AllLayers]])def
update([rect=QRectF()])
Signals露
def
changed(region)def
focusItemChanged(newFocus, oldFocus, reason)def
sceneRectChanged(rect)def
selectionChanged()
Detailed Description露
The class serves as a container for QGraphicsItems. It is used together with
QGraphicsViewfor visualizing graphical items, such as lines, rectangles, text, or even custom items, on a 2D surface.QGraphicsSceneis part of the Graphics View Framework .
QGraphicsScenealso provides functionality that lets you efficiently determine both the location of items, and for determining what items are visible within an arbitrary area on the scene. With theQGraphicsViewwidget, you can either visualize the whole scene, or zoom in and view only parts of the scene.Example:
scene = QGraphicsScene() scene.addText("Hello, world!") view = QGraphicsView(&scene) view.show()Note that
QGraphicsScenehas no visual appearance of its own; it only manages the items. You need to create aQGraphicsViewwidget to visualize the scene.To add items to a scene, you start off by constructing a
QGraphicsSceneobject. Then, you have two options: either add your existingQGraphicsItemobjects by callingaddItem(), or you can call one of the convenience functionsaddEllipse(),addLine(),addPath(),addPixmap(),addPolygon(),addRect(), oraddText(), which all return a pointer to the newly added item. The dimensions of the items added with these functions are relative to the item鈥檚 coordinate system, and the items position is initialized to (0, 0) in the scene.You can then visualize the scene using
QGraphicsView. When the scene changes, (e.g., when an item moves or is transformed)QGraphicsSceneemits thechanged()signal. To remove an item, callremoveItem().
QGraphicsSceneuses an indexing algorithm to manage the location of items efficiently. By default, a BSP (Binary Space Partitioning) tree is used; an algorithm suitable for large scenes where most items remain static (i.e., do not move around). You can choose to disable this index by callingsetItemIndexMethod(). For more information about the available indexing algorithms, see theitemIndexMethodproperty.The scene鈥檚 bounding rect is set by calling
setSceneRect(). Items can be placed at any position on the scene, and the size of the scene is by default unlimited. The scene rect is used only for internal bookkeeping, maintaining the scene鈥檚 item index. If the scene rect is unset,QGraphicsScenewill use the bounding area of all items, as returned byitemsBoundingRect(), as the scene rect. However,itemsBoundingRect()is a relatively time consuming function, as it operates by collecting positional information for every item on the scene. Because of this, you should always set the scene rect when operating on large scenes.One of
QGraphicsScene鈥榮 greatest strengths is its ability to efficiently determine the location of items. Even with millions of items on the scene, theitems()functions can determine the location of an item within a few milliseconds. There are several overloads toitems(): one that finds items at a certain position, one that finds items inside or intersecting with a polygon or a rectangle, and more. The list of returned items is sorted by stacking order, with the topmost item being the first item in the list. For convenience, there is also anitemAt()function that returns the topmost item at a given position.
QGraphicsScenemaintains selection information for the scene. To select items, callsetSelectionArea(), and to clear the current selection, callclearSelection(). CallselectedItems()to get the list of all selected items.
Event Handling and Propagation露
Another responsibility that
QGraphicsScenehas, is to propagate events fromQGraphicsView. To send an event to a scene, you construct an event that inheritsQEvent, and then send it using, for example,sendEvent().event()is responsible for dispatching the event to the individual items. Some common events are handled by convenience event handlers. For example, key press events are handled bykeyPressEvent(), and mouse press events are handled bymousePressEvent().Key events are delivered to the focus item . To set the focus item, you can either call
setFocusItem(), passing an item that accepts focus, or the item itself can callsetFocus(). CallfocusItem()to get the current focus item. For compatibility with widgets, the scene also maintains its own focus information. By default, the scene does not have focus, and all key events are discarded. IfsetFocus()is called, or if an item on the scene gains focus, the scene automatically gains focus. If the scene has focus,hasFocus()will return true, and key events will be forwarded to the focus item, if any. If the scene loses focus, (i.e., someone callsclearFocus()) while an item has focus, the scene will maintain its item focus information, and once the scene regains focus, it will make sure the last focus item regains focus.For mouse-over effects,
QGraphicsScenedispatches hover events . If an item accepts hover events (seeacceptHoverEvents()), it will receive aGraphicsSceneHoverEnterevent when the mouse enters its area. As the mouse continues moving inside the item鈥檚 area,QGraphicsScenewill send itGraphicsSceneHoverMoveevents. When the mouse leaves the item鈥檚 area, the item will receive aGraphicsSceneHoverLeaveevent.All mouse events are delivered to the current mouse grabber item. An item becomes the scene鈥檚 mouse grabber if it accepts mouse events (see
acceptedMouseButtons()) and it receives a mouse press. It stays the mouse grabber until it receives a mouse release when no other mouse buttons are pressed. You can callmouseGrabberItem()to determine what item is currently grabbing the mouse.See also
- class PySide2.QtWidgets.QGraphicsScene([parent=None])露
PySide2.QtWidgets.QGraphicsScene(sceneRect[, parent=None])
PySide2.QtWidgets.QGraphicsScene(x, y, width, height[, parent=None])
- param parent:
- param x:
float
- param y:
float
- param width:
float
- param sceneRect:
- param height:
float
Constructs a
QGraphicsSceneobject. Theparentparameter is passed toQObject鈥榮 constructor.Constructs a
QGraphicsSceneobject, usingsceneRectfor its scene rectangle. Theparentparameter is passed toQObject鈥榮 constructor.See also
Constructs a
QGraphicsSceneobject, using the rectangle specified by (x,y), and the givenwidthandheightfor its scene rectangle. Theparentparameter is passed toQObject鈥榮 constructor.See also
- PySide2.QtWidgets.QGraphicsScene.ItemIndexMethod露
This enum describes the indexing algorithms
QGraphicsSceneprovides for managing positional information about items on the scene.Constant
Description
QGraphicsScene.BspTreeIndex
A Binary Space Partitioning tree is applied. All
QGraphicsScene鈥榮 item location algorithms are of an order close to logarithmic complexity, by making use of binary search. Adding, moving and removing items is logarithmic. This approach is best for static scenes (i.e., scenes where most items do not move).QGraphicsScene.NoIndex
No index is applied. Item location is of linear complexity, as all items on the scene are searched. Adding, moving and removing items, however, is done in constant time. This approach is ideal for dynamic scenes, where many items are added, moved or removed continuously.
See also
- PySide2.QtWidgets.QGraphicsScene.SceneLayer露
This enum describes the rendering layers in a
QGraphicsScene. WhenQGraphicsScenedraws the scene contents, it renders each of these layers separately, in order.Each layer represents a flag that can be OR鈥檈d together when calling functions such as
invalidate()orinvalidateScene().Constant
Description
QGraphicsScene.ItemLayer
The item layer.
QGraphicsScenerenders all items are in this layer by calling the virtual functiondrawItems(). The item layer is drawn after the background layer, but before the foreground layer.QGraphicsScene.BackgroundLayer
The background layer.
QGraphicsScenerenders the scene鈥檚 background in this layer by calling the virtual functiondrawBackground(). The background layer is drawn first of all layers.QGraphicsScene.ForegroundLayer
The foreground layer.
QGraphicsScenerenders the scene鈥檚 foreground in this layer by calling the virtual functiondrawForeground(). The foreground layer is drawn last of all layers.QGraphicsScene.AllLayers
All layers; this value represents a combination of all three layers.
See also
- PySide2.QtWidgets.QGraphicsScene.activePanel()露
- Return type:
Returns the current active panel, or
Noneif no panel is currently active.See also
- PySide2.QtWidgets.QGraphicsScene.activeWindow()露
- Return type:
Returns the current active window, or
Noneif no window is currently active.See also
- PySide2.QtWidgets.QGraphicsScene.addEllipse(rect[, pen=QPen()[, brush=QBrush()]])露
- Parameters:
rect 鈥
PySide2.QtCore.QRectFpen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
Creates and adds an ellipse item to the scene, and returns the item pointer. The geometry of the ellipse is defined by
rect, and its pen and brush are initialized topenandbrush.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addEllipse(x, y, w, h[, pen=QPen()[, brush=QBrush()]])
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
pen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
This convenience function is equivalent to calling
addEllipse(QRectF(x,y,w,h),pen,brush).
- PySide2.QtWidgets.QGraphicsScene.addItem(item)露
- Parameters:
Adds or moves the
itemand all its children to this scene. This scene takes ownership of theitem.If the item is visible (i.e.,
isVisible()returns true),QGraphicsScenewill emitchanged()once control goes back to the event loop.If the item is already in a different scene, it will first be removed from its old scene, and then added to this scene as a top-level.
QGraphicsScenewill send ItemSceneChange notifications toitemwhile it is added to the scene. If item does not currently belong to a scene, only one notification is sent. If it does belong to scene already (i.e., it is moved to this scene),QGraphicsScenewill send an addition notification as the item is removed from its previous scene.If the item is a panel, the scene is active, and there is no active panel in the scene, then the item will be activated.
See also
removeItem()addEllipse()addLine()addPath()addPixmap()addRect()addText()addWidget()Sorting
- PySide2.QtWidgets.QGraphicsScene.addLine(line[, pen=QPen()])露
- Parameters:
line 鈥
PySide2.QtCore.QLineFpen 鈥
PySide2.QtGui.QPen
- Return type:
Creates and adds a line item to the scene, and returns the item pointer. The geometry of the line is defined by
line, and its pen is initialized topen.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addLine(x1, y1, x2, y2[, pen=QPen()])
- Parameters:
x1 鈥 float
y1 鈥 float
x2 鈥 float
y2 鈥 float
pen 鈥
PySide2.QtGui.QPen
- Return type:
This convenience function is equivalent to calling
addLine(QLineF(x1,y1,x2,y2),pen).
- PySide2.QtWidgets.QGraphicsScene.addPath(path[, pen=QPen()[, brush=QBrush()]])露
- Parameters:
path 鈥
PySide2.QtGui.QPainterPathpen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
Creates and adds a path item to the scene, and returns the item pointer. The geometry of the path is defined by
path, and its pen and brush are initialized topenandbrush.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addPixmap(pixmap)露
- Parameters:
pixmap 鈥
PySide2.QtGui.QPixmap- Return type:
Creates and adds a pixmap item to the scene, and returns the item pointer. The pixmap is defined by
pixmap.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addPolygon(polygon[, pen=QPen()[, brush=QBrush()]])露
- Parameters:
polygon 鈥
PySide2.QtGui.QPolygonFpen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
Creates and adds a polygon item to the scene, and returns the item pointer. The polygon is defined by
polygon, and its pen and brush are initialized topenandbrush.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addRect(rect[, pen=QPen()[, brush=QBrush()]])露
- Parameters:
rect 鈥
PySide2.QtCore.QRectFpen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
Creates and adds a rectangle item to the scene, and returns the item pointer. The geometry of the rectangle is defined by
rect, and its pen and brush are initialized topenandbrush.Note that the item鈥檚 geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a
QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item鈥檚 coordinate system.If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addRect(x, y, w, h[, pen=QPen()[, brush=QBrush()]])
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
pen 鈥
PySide2.QtGui.QPenbrush 鈥
PySide2.QtGui.QBrush
- Return type:
This convenience function is equivalent to calling
addRect(QRectF(x,y,w,h),pen,brush).
- PySide2.QtWidgets.QGraphicsScene.addSimpleText(text[, font=QFont()])露
- Parameters:
text 鈥 str
font 鈥
PySide2.QtGui.QFont
- Return type:
Creates and adds a
QGraphicsSimpleTextItemto the scene, and returns the item pointer. The text string is initialized totext, and its font is initialized tofont.The item鈥檚 position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addText(text[, font=QFont()])露
- Parameters:
text 鈥 str
font 鈥
PySide2.QtGui.QFont
- Return type:
Creates and adds a text item to the scene, and returns the item pointer. The text string is initialized to
text, and its font is initialized tofont.The item鈥檚 position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.
- PySide2.QtWidgets.QGraphicsScene.addWidget(widget[, wFlags=Qt.WindowFlags()])露
- Parameters:
widget 鈥
PySide2.QtWidgets.QWidgetwFlags 鈥
WindowFlags
- Return type:
Creates a new
QGraphicsProxyWidgetforwidget, adds it to the scene, and returns a pointer to the proxy.wFlagsset the default window flags for the embedding proxy widget.The item鈥檚 position is initialized to (0, 0).
If the item is visible (i.e.,
isVisible()returnstrue),QGraphicsScenewill emitchanged()once control goes back to the event loop.Note that widgets with the
WA_PaintOnScreenwidget attribute set and widgets that wrap an external application or controller are not supported. Examples areQOpenGLWidgetand QAxWidget.
- PySide2.QtWidgets.QGraphicsScene.advance()露
This slot advances the scene by one step, by calling
advance()for all items on the scene. This is done in two phases: in the first phase, all items are notified that the scene is about to change, and in the second phase all items are notified that they can move. In the first phase,advance()is called passing a value of 0 as an argument, and 1 is passed in the second phase.Note that you can also use the Animation Framework for animations.
See also
advance()QTimeLine
- PySide2.QtWidgets.QGraphicsScene.backgroundBrush()露
- Return type:
This property holds the background brush of the scene..
Set this property to changes the scene鈥檚 background to a different color, gradient or texture. The default background brush is
NoBrush. The background is drawn before (behind) the items.Example:
scene = QGraphicsScene() view = QGraphicsView(scene) view.show() # a blue background scene.setBackgroundBrush(Qt.blue) # a gradient background gradient = QRadialGradient(0, 0, 10) gradient.setSpread(QGradient.RepeatSpread) scene.setBackgroundBrush(gradient)
render()callsdrawBackground()to draw the scene background. For more detailed control over how the background is drawn, you can reimplementdrawBackground()in a subclass ofQGraphicsScene.
- PySide2.QtWidgets.QGraphicsScene.bspTreeDepth()露
- Return type:
int
This property holds the depth of
QGraphicsScene鈥榮 BSP index tree.This property has no effect when
NoIndexis used.This value determines the depth of
QGraphicsScene鈥榮 BSP tree. The depth directly affectsQGraphicsScene鈥榮 performance and memory usage; the latter growing exponentially with the depth of the tree. With an optimal tree depth,QGraphicsScenecan instantly determine the locality of items, even for scenes with thousands or millions of items. This also greatly improves rendering performance.By default, the value is 0, in which case Qt will guess a reasonable default depth based on the size, location and number of items in the scene. If these parameters change frequently, however, you may experience slowdowns as
QGraphicsSceneretunes the depth internally. You can avoid potential slowdowns by fixating the tree depth through setting this property.The depth of the tree and the size of the scene rectangle decide the granularity of the scene鈥檚 partitioning. The size of each scene segment is determined by the following algorithm:
segmentSize = sceneRect().size() / math.pow(2, depth - 1)
The BSP tree has an optimal size when each segment contains between 0 and 10 items.
See also
- PySide2.QtWidgets.QGraphicsScene.changed(region)露
- Parameters:
region 鈥
- PySide2.QtWidgets.QGraphicsScene.clear()露
Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.
See also
- PySide2.QtWidgets.QGraphicsScene.clearFocus()露
Clears focus from the scene. If any item has focus when this function is called, it will lose focus, and regain focus again once the scene regains focus.
A scene that does not have focus ignores key events.
See also
- PySide2.QtWidgets.QGraphicsScene.clearSelection()露
Clears the current selection.
See also
- PySide2.QtWidgets.QGraphicsScene.collidingItems(item[, mode=Qt.IntersectsItemShape])露
- Parameters:
mode 鈥
ItemSelectionMode
- Return type:
Returns a list of all items that collide with
item. Collisions are determined by callingcollidesWithItem(); the collision detection is determined bymode. By default, all items whose shape intersectsitemor is contained insideitem鈥榮 shape are returned.The items are returned in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item).
See also
items()itemAt()collidesWithItem()Sorting
- PySide2.QtWidgets.QGraphicsScene.contextMenuEvent(event)露
- Parameters:
This event handler, for event
contextMenuEvent, can be reimplemented in a subclass to receive context menu events. The default implementation forwards the event to the topmost visible item that accepts context menu events at the position of the event. If no items accept context menu events at this position, the event is ignored.Note: See
items()for a definition of which items are considered visible by this function.See also
- PySide2.QtWidgets.QGraphicsScene.createItemGroup(items)露
- Parameters:
items 鈥
- Return type:
Groups all items in
itemsinto a newQGraphicsItemGroup, and returns a pointer to the group. The group is created with the common ancestor ofitemsas its parent, and with position (0, 0). The items are all reparented to the group, and their positions and transformations are mapped to the group. Ifitemsis empty, this function will return an empty top-levelQGraphicsItemGroup.QGraphicsScenehas ownership of the group item; you do not need to delete it. To dismantle (ungroup) a group, calldestroyItemGroup().See also
- PySide2.QtWidgets.QGraphicsScene.destroyItemGroup(group)露
- Parameters:
Reparents all items in
grouptogroup鈥榮 parent item, then removesgroupfrom the scene, and finally deletes it. The items鈥 positions and transformations are mapped from the group to the group鈥檚 parent.See also
- PySide2.QtWidgets.QGraphicsScene.dragEnterEvent(event)露
- Parameters:
This event handler, for event
event, can be reimplemented in a subclass to receive drag enter events for the scene.The default implementation accepts the event and prepares the scene to accept drag move events.
- PySide2.QtWidgets.QGraphicsScene.dragLeaveEvent(event)露
- Parameters:
This event handler, for event
event, can be reimplemented in a subclass to receive drag leave events for the scene.
- PySide2.QtWidgets.QGraphicsScene.dragMoveEvent(event)露
- Parameters:
This event handler, for event
event, can be reimplemented in a subclass to receive drag move events for the scene.Note: See
items()for a definition of which items are considered visible by this function.
- PySide2.QtWidgets.QGraphicsScene.drawBackground(painter, rect)露
- Parameters:
painter 鈥
PySide2.QtGui.QPainterrect 鈥
PySide2.QtCore.QRectF
Draws the background of the scene using
painter, before any items and the foreground are drawn. Reimplement this function to provide a custom background for the scene.All painting is done in scene coordinates. The
rectparameter is the exposed rectangle.If all you want is to define a color, texture, or gradient for the background, you can call
setBackgroundBrush()instead.See also
drawForeground()drawItems()
- PySide2.QtWidgets.QGraphicsScene.drawForeground(painter, rect)露
- Parameters:
painter 鈥
PySide2.QtGui.QPainterrect 鈥
PySide2.QtCore.QRectF
Draws the foreground of the scene using
painter, after the background and all items have been drawn. Reimplement this function to provide a custom foreground for the scene.All painting is done in scene coordinates. The
rectparameter is the exposed rectangle.If all you want is to define a color, texture or gradient for the foreground, you can call
setForegroundBrush()instead.See also
drawBackground()drawItems()
- PySide2.QtWidgets.QGraphicsScene.dropEvent(event)露
- Parameters:
This event handler, for event
event, can be reimplemented in a subclass to receive drop events for the scene.
- PySide2.QtWidgets.QGraphicsScene.focusInEvent(event)露
- Parameters:
event 鈥
PySide2.QtGui.QFocusEvent
This event handler, for event
focusEvent, can be reimplemented in a subclass to receive focus in events.The default implementation sets focus on the scene, and then on the last focus item.
See also
- PySide2.QtWidgets.QGraphicsScene.focusItem()露
- Return type:
When the scene is active, this functions returns the scene鈥檚 current focus item, or
Noneif no item currently has focus. When the scene is inactive, this functions returns the item that will gain input focus when the scene becomes active.The focus item receives keyboard input when the scene receives a key event.
See also
- PySide2.QtWidgets.QGraphicsScene.focusItemChanged(newFocus, oldFocus, reason)露
- Parameters:
newFocus 鈥
PySide2.QtWidgets.QGraphicsItemoldFocus 鈥
PySide2.QtWidgets.QGraphicsItemreason 鈥
FocusReason
- PySide2.QtWidgets.QGraphicsScene.focusNextPrevChild(next)露
- Parameters:
next 鈥 bool
- Return type:
bool
Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns
trueif it can find a new widget, or false if it cannot. Ifnextis true, this function searches forward; ifnextis false, it searches backward.You can reimplement this function in a subclass of
QGraphicsSceneto provide fine-grained control over how tab focus passes inside your scene. The default implementation is based on the tab focus chain defined bysetTabOrder().
- PySide2.QtWidgets.QGraphicsScene.focusOnTouch()露
- Return type:
bool
This property holds whether items gain focus when receiving a touch begin event..
The usual behavior is to transfer focus only when an item is clicked. Often a tap on a touchpad is interpreted as equivalent to a mouse click by the operating system, generating a synthesized click event in response. However, at least on macOS you can configure this behavior.
By default,
QGraphicsScenealso transfers focus when you touch on a trackpad or similar. If the operating system is configured to not generate a synthetic mouse click on tapping the trackpad, this is surprising. If the operating system does generate synthetic mouse clicks on tapping the trackpad, the focus transfer on starting a touch gesture is unnecessary.With switched off,
QGraphicsScenebehaves as one would expect on macOS.The default value is
true, ensuring that the default behavior is just as in Qt versions prior to 5.12. Set tofalseto prevent touch events from triggering focus changes.
- PySide2.QtWidgets.QGraphicsScene.focusOutEvent(event)露
- Parameters:
event 鈥
PySide2.QtGui.QFocusEvent
This event handler, for event
focusEvent, can be reimplemented in a subclass to receive focus out events.The default implementation removes focus from any focus item, then removes focus from the scene.
See also
- PySide2.QtWidgets.QGraphicsScene.font()露
- Return type:
This property holds the scene鈥檚 default font.
This property provides the scene鈥檚 font. The scene font defaults to, and resolves all its entries from,
font.If the scene鈥檚 font changes, either directly through or indirectly when the application font changes,
QGraphicsScenefirst sends itself aFontChangeevent, and it then sendsFontChangeevents to all top-level widget items in the scene. These items respond by resolving their own fonts to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their fonts.Changing the scene font, (directly or indirectly through
setFont(),) automatically schedules a redraw the entire scene.
- PySide2.QtWidgets.QGraphicsScene.foregroundBrush()露
- Return type:
This property holds the foreground brush of the scene..
Change this property to set the scene鈥檚 foreground to a different color, gradient or texture.
The foreground is drawn after (on top of) the items. The default foreground brush is
NoBrush( i.e. the foreground is not drawn).Example:
scene = QGraphicsScene() view = QGraphicsView(scene) view.show() # a white semi-transparent foreground scene.setForegroundBrush(QColor(255, 255, 255, 127)) # a grid foreground scene.setForegroundBrush(QBrush(Qt.lightGray, Qt.CrossPattern))
render()callsdrawForeground()to draw the scene foreground. For more detailed control over how the foreground is drawn, you can reimplement thedrawForeground()function in aQGraphicsScenesubclass.
- PySide2.QtWidgets.QGraphicsScene.hasFocus()露
- Return type:
bool
Returns
trueif the scene has focus; otherwise returnsfalse. If the scene has focus, it will will forward key events fromQKeyEventto any item that has focus.See also
- PySide2.QtWidgets.QGraphicsScene.height()露
- Return type:
float
This convenience function is equivalent to calling
sceneRect().height().See also
- PySide2.QtWidgets.QGraphicsScene.helpEvent(event)露
- Parameters:
This event handler, for event
helpEvent, can be reimplemented in a subclass to receive help events. The events are of typeToolTip, which are created when a tooltip is requested.The default implementation shows the tooltip of the topmost visible item, i.e., the item with the highest z-value, at the mouse cursor position. If no item has a tooltip set, this function does nothing.
Note: See
items()for a definition of which items are considered visible by this function.See also
- PySide2.QtWidgets.QGraphicsScene.inputMethodEvent(event)露
- Parameters:
event 鈥
PySide2.QtGui.QInputMethodEvent
This event handler, for event
event, can be reimplemented in a subclass to receive input method events for the scene.The default implementation forwards the event to the
focusItem(). If no item currently has focus or the current focus item does not accept input methods, this function does nothing.See also
- PySide2.QtWidgets.QGraphicsScene.inputMethodQuery(query)露
- Parameters:
query 鈥
InputMethodQuery- Return type:
object
This method is used by input methods to query a set of properties of the scene to be able to support complex input method operations as support for surrounding text and reconversions.
The
queryparameter specifies which property is queried.See also
- PySide2.QtWidgets.QGraphicsScene.invalidate([rect=QRectF()[, layers=QGraphicsScene.AllLayers]])露
- Parameters:
rect 鈥
PySide2.QtCore.QRectFlayers 鈥
SceneLayers
Invalidates and schedules a redraw of the
layersinrecton the scene. Any cached content inlayersis unconditionally invalidated and redrawn.You can use this function overload to notify
QGraphicsSceneof changes to the background or the foreground of the scene. This function is commonly used for scenes with tile-based backgrounds to notify changes whenQGraphicsViewhas enabledCacheBackground.Example:
class TileScene (QGraphicsScene): # ... def rectForTile(x, y): # Return the rectangle for the tile at position (x, y). return QRectF(x * self.tileWidth, y * self.tileHeight, self.tileWidth, self.tileHeight) def setTile(x, y, pixmap): # Sets or replaces the tile at position (x, y) with pixmap. if x >= 0 && x < self.numTilesH && y >= 0 && y < self.numTilesV: self.tiles[y][x] = pixmap invalidate(rectForTile(x, y), BackgroundLayer) def drawBackground(painter, exposed): # Draws all tiles that intersect the exposed area. for y in range(0, self.numTilesV: for x in range(0, self.numTilesH: rect = rectForTile(x, y) if exposed.intersects(rect): painter.drawPixmap(rect.topLeft(), tiles[y][x])
Note that
QGraphicsViewcurrently supports background caching only (seeCacheBackground). This function is equivalent to callingupdate()if any layer butBackgroundLayeris passed.See also
- PySide2.QtWidgets.QGraphicsScene.invalidate(x, y, w, h[, layers=QGraphicsScene.AllLayers])
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
layers 鈥
SceneLayers
This is an overloaded function.
This convenience function is equivalent to calling invalidate(
QRectF(x,y,w,h),layers);
- PySide2.QtWidgets.QGraphicsScene.isActive()露
- Return type:
bool
Returns
trueif the scene is active (e.g., it鈥檚 viewed by at least oneQGraphicsViewthat is active); otherwise returnsfalse.See also
- PySide2.QtWidgets.QGraphicsScene.isSortCacheEnabled()露
- Return type:
bool
Note
This function is deprecated.
This property holds whether sort caching is enabled.
Since Qt 4.6, this property has no effect.
- PySide2.QtWidgets.QGraphicsScene.itemAt(x, y, deviceTransform)露
- Parameters:
x 鈥 float
y 鈥 float
deviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
This is an overloaded function.
Returns the topmost visible item at the position specified by (
x,y), orNoneif there are no items at this position.deviceTransformis the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.This convenience function is equivalent to calling
itemAt(QPointF(x, y), deviceTransform).Note: See
items()for a definition of which items are considered visible by this function.
- PySide2.QtWidgets.QGraphicsScene.itemAt(pos, deviceTransform)
- Parameters:
pos 鈥
PySide2.QtCore.QPointFdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
- PySide2.QtWidgets.QGraphicsScene.itemIndexMethod()露
- Return type:
This property holds the item indexing method..
QGraphicsSceneapplies an indexing algorithm to the scene, to speed up item discovery functions likeitems()anditemAt(). Indexing is most efficient for static scenes (i.e., where items don鈥檛 move around). For dynamic scenes, or scenes with many animated items, the index bookkeeping can outweigh the fast lookup speeds.For the common case, the default index method
BspTreeIndexworks fine. If your scene uses many animations and you are experiencing slowness, you can disable indexing by callingsetItemIndexMethod(NoIndex).See also
- PySide2.QtWidgets.QGraphicsScene.items(x, y, w, h, mode, order[, deviceTransform=QTransform()])露
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
mode 鈥
ItemSelectionModeorder 鈥
SortOrderdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
This is an overloaded function.
Returns all visible items that, depending on
mode, are either inside or intersect with the rectangle defined byx,y,wandh, in a list sorted usingorder. In this case, 鈥渧isible鈥 defines items for which: isVisible() returnstrue, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.deviceTransformis the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.
- PySide2.QtWidgets.QGraphicsScene.items(rect[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])
- Parameters:
rect 鈥
PySide2.QtCore.QRectFmode 鈥
ItemSelectionModeorder 鈥
SortOrderdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
- PySide2.QtWidgets.QGraphicsScene.items(polygon[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])
- Parameters:
polygon 鈥
PySide2.QtGui.QPolygonFmode 鈥
ItemSelectionModeorder 鈥
SortOrderdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
- PySide2.QtWidgets.QGraphicsScene.items(pos[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])
- Parameters:
pos 鈥
PySide2.QtCore.QPointFmode 鈥
ItemSelectionModeorder 鈥
SortOrderdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
- PySide2.QtWidgets.QGraphicsScene.items(path[, mode=Qt.IntersectsItemShape[, order=Qt.DescendingOrder[, deviceTransform=QTransform()]]])
- Parameters:
path 鈥
PySide2.QtGui.QPainterPathmode 鈥
ItemSelectionModeorder 鈥
SortOrderdeviceTransform 鈥
PySide2.QtGui.QTransform
- Return type:
- PySide2.QtWidgets.QGraphicsScene.items([order=Qt.DescendingOrder])
- Parameters:
order 鈥
SortOrder- Return type:
Returns an ordered list of all items on the scene.
orderdecides the stacking order.See also
addItem()removeItem()Sorting
- PySide2.QtWidgets.QGraphicsScene.itemsBoundingRect()露
- Return type:
Calculates and returns the bounding rect of all items on the scene. This function works by iterating over all items, and because of this, it can be slow for large scenes.
See also
- PySide2.QtWidgets.QGraphicsScene.keyPressEvent(event)露
- Parameters:
event 鈥
PySide2.QtGui.QKeyEvent
This event handler, for event
keyEvent, can be reimplemented in a subclass to receive keypress events. The default implementation forwards the event to current focus item.See also
- PySide2.QtWidgets.QGraphicsScene.keyReleaseEvent(event)露
- Parameters:
event 鈥
PySide2.QtGui.QKeyEvent
This event handler, for event
keyEvent, can be reimplemented in a subclass to receive key release events. The default implementation forwards the event to current focus item.See also
- PySide2.QtWidgets.QGraphicsScene.minimumRenderSize()露
- Return type:
float
This property holds the minimal view-transformed size an item must have to be drawn.
When the scene is rendered, any item whose width or height, transformed to the target view, is smaller that , will not be rendered. If an item is not rendered and it clips its children items they will also not be rendered. Set this value to speed up rendering of scenes with many objects rendered on a zoomed out view.
The default value is 0. If unset, or if set to 0 or a negative value, all items will always be rendered.
For example, setting this property can be especially useful if a scene is rendered by multiple views, one of which serves as an overview which always displays all items. In scenes with many items, such a view will use a high scaling factor so that all items can be shown. Due to the scaling, smaller items will only make an insignificant contribution to the final rendered scene. To avoid drawing these items and reduce the time necessary to render the scene, you can call with a non-negative value.
Note
Items that are not drawn as a result of being too small, are still returned by methods such as
items()anditemAt(), and participate in collision detection and interactions. It is recommended that you set to a value less than or equal to 1 in order to avoid large unrendered items that are interactive.See also
- PySide2.QtWidgets.QGraphicsScene.mouseDoubleClickEvent(event)露
- Parameters:
This event handler, for event
mouseEvent, can be reimplemented in a subclass to receive mouse double-click events for the scene.If someone doubleclicks on the scene, the scene will first receive a mouse press event, followed by a release event (i.e., a click), then a double-click event, and finally a release event. If the double-click event is delivered to a different item than the one that received the first press and release, it will be delivered as a press event. However, tripleclick events are not delivered as double-click events in this case.
The default implementation is similar to
mousePressEvent().Note: See
items()for a definition of which items are considered visible by this function.
- PySide2.QtWidgets.QGraphicsScene.mouseGrabberItem()露
- Return type:
Returns the current mouse grabber item, or
Noneif no item is currently grabbing the mouse. The mouse grabber item is the item that receives all mouse events sent to the scene.An item becomes a mouse grabber when it receives and accepts a mouse press event, and it stays the mouse grabber until either of the following events occur:
If the item receives a mouse release event when there are no other buttons pressed, it loses the mouse grab.
If the item becomes invisible (i.e., someone calls
item->setVisible(false)), or if it becomes disabled (i.e., someone callsitem->setEnabled(false)), it loses the mouse grab.If the item is removed from the scene, it loses the mouse grab.
If the item loses its mouse grab, the scene will ignore all mouse events until a new item grabs the mouse (i.e., until a new item receives a mouse press event).
- PySide2.QtWidgets.QGraphicsScene.mouseMoveEvent(event)露
- Parameters:
This event handler, for event
mouseEvent, can be reimplemented in a subclass to receive mouse move events for the scene.The default implementation depends on the mouse grabber state. If there is a mouse grabber item, the event is sent to the mouse grabber. If there are any items that accept hover events at the current position, the event is translated into a hover event and accepted; otherwise it鈥檚 ignored.
- PySide2.QtWidgets.QGraphicsScene.mousePressEvent(event)露
- Parameters:
This event handler, for event
mouseEvent, can be reimplemented in a subclass to receive mouse press events for the scene.The default implementation depends on the state of the scene. If there is a mouse grabber item, then the event is sent to the mouse grabber. Otherwise, it is forwarded to the topmost visible item that accepts mouse events at the scene position from the event, and that item promptly becomes the mouse grabber item.
If there is no item at the given position on the scene, the selection area is reset, any focus item loses its input focus, and the event is then ignored.
Note: See
items()for a definition of which items are considered visible by this function.
- PySide2.QtWidgets.QGraphicsScene.mouseReleaseEvent(event)露
- Parameters:
This event handler, for event
mouseEvent, can be reimplemented in a subclass to receive mouse release events for the scene.The default implementation depends on the mouse grabber state. If there is no mouse grabber, the event is ignored. Otherwise, if there is a mouse grabber item, the event is sent to the mouse grabber. If this mouse release represents the last pressed button on the mouse, the mouse grabber item then loses the mouse grab.
- PySide2.QtWidgets.QGraphicsScene.palette()露
- Return type:
This property holds the scene鈥檚 default palette.
This property provides the scene鈥檚 palette. The scene palette defaults to, and resolves all its entries from,
palette.If the scene鈥檚 palette changes, either directly through or indirectly when the application palette changes,
QGraphicsScenefirst sends itself aPaletteChangeevent, and it then sendsPaletteChangeevents to all top-level widget items in the scene. These items respond by resolving their own palettes to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their palettes.Changing the scene palette, (directly or indirectly through
setPalette(),) automatically schedules a redraw the entire scene.See also
- PySide2.QtWidgets.QGraphicsScene.removeItem(item)露
- Parameters:
Removes the item
itemand all its children from the scene. The ownership ofitemis passed on to the caller (i.e.,QGraphicsScenewill no longer deleteitemwhen destroyed).See also
- PySide2.QtWidgets.QGraphicsScene.render(painter[, target=QRectF()[, source=QRectF()[, aspectRatioMode=Qt.KeepAspectRatio]]])露
- Parameters:
painter 鈥
PySide2.QtGui.QPaintertarget 鈥
PySide2.QtCore.QRectFsource 鈥
PySide2.QtCore.QRectFaspectRatioMode 鈥
AspectRatioMode
Renders the
sourcerect from scene intotarget, usingpainter. This function is useful for capturing the contents of the scene onto a paint device, such as aQImage(e.g., to take a screenshot), or for printing withQPrinter. For example:scene = QGraphicsScene scene.addItem(... ... printer = QPrinter(QPrinter.HighResolution) printer.setPaperSize(QPrinter.A4) painter = QPainter(printer) scene.render(&painter)
If
sourceis a null rect, this function will usesceneRect()to determine what to render. Iftargetis a null rect, the dimensions ofpainter鈥榮 paint device will be used.The source rect contents will be transformed according to
aspectRatioModeto fit into the target rect. By default, the aspect ratio is kept, andsourceis scaled to fit intarget.See also
- PySide2.QtWidgets.QGraphicsScene.sceneRect()露
- Return type:
This property holds the scene rectangle; the bounding rectangle of the scene.
The scene rectangle defines the extent of the scene. It is primarily used by
QGraphicsViewto determine the view鈥檚 default scrollable area, and byQGraphicsSceneto manage item indexing.If unset, or if set to a null
QRectF, will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
- PySide2.QtWidgets.QGraphicsScene.sceneRectChanged(rect)露
- Parameters:
rect 鈥
PySide2.QtCore.QRectF
- PySide2.QtWidgets.QGraphicsScene.selectedItems()露
- Return type:
Returns a list of all currently selected items. The items are returned in no particular order.
See also
- PySide2.QtWidgets.QGraphicsScene.selectionArea()露
- Return type:
Returns the selection area that was previously set with
setSelectionArea(), or an emptyQPainterPathif no selection area has been set.See also
- PySide2.QtWidgets.QGraphicsScene.selectionChanged()露
- PySide2.QtWidgets.QGraphicsScene.sendEvent(item, event)露
- Parameters:
event 鈥
PySide2.QtCore.QEvent
- Return type:
bool
Sends event
eventto itemitemthrough possible event filters.The event is sent only if the item is enabled.
Returns
falseif the event was filtered or if the item is disabled. Otherwise returns the value that was returned from the event handler.See also
- PySide2.QtWidgets.QGraphicsScene.setActivePanel(item)露
- Parameters:
Activates
item, which must be an item in this scene. You can also pass 0 foritem, in which caseQGraphicsScenewill deactivate any currently active panel.If the scene is currently inactive,
itemremains inactive until the scene becomes active (or, iritemisNone, no item will be activated).See also
- PySide2.QtWidgets.QGraphicsScene.setActiveWindow(widget)露
- Parameters:
widget 鈥
PySide2.QtWidgets.QGraphicsWidget
Activates
widget, which must be a widget in this scene. You can also pass 0 forwidget, in which caseQGraphicsScenewill deactivate any currently active window.See also
- PySide2.QtWidgets.QGraphicsScene.setBackgroundBrush(brush)露
- Parameters:
brush 鈥
PySide2.QtGui.QBrush
This property holds the background brush of the scene..
Set this property to changes the scene鈥檚 background to a different color, gradient or texture. The default background brush is
NoBrush. The background is drawn before (behind) the items.Example:
scene = QGraphicsScene() view = QGraphicsView(scene) view.show() # a blue background scene.setBackgroundBrush(Qt.blue) # a gradient background gradient = QRadialGradient(0, 0, 10) gradient.setSpread(QGradient.RepeatSpread) scene.setBackgroundBrush(gradient)
render()callsdrawBackground()to draw the scene background. For more detailed control over how the background is drawn, you can reimplementdrawBackground()in a subclass ofQGraphicsScene.
- PySide2.QtWidgets.QGraphicsScene.setBspTreeDepth(depth)露
- Parameters:
depth 鈥 int
This property holds the depth of
QGraphicsScene鈥榮 BSP index tree.This property has no effect when
NoIndexis used.This value determines the depth of
QGraphicsScene鈥榮 BSP tree. The depth directly affectsQGraphicsScene鈥榮 performance and memory usage; the latter growing exponentially with the depth of the tree. With an optimal tree depth,QGraphicsScenecan instantly determine the locality of items, even for scenes with thousands or millions of items. This also greatly improves rendering performance.By default, the value is 0, in which case Qt will guess a reasonable default depth based on the size, location and number of items in the scene. If these parameters change frequently, however, you may experience slowdowns as
QGraphicsSceneretunes the depth internally. You can avoid potential slowdowns by fixating the tree depth through setting this property.The depth of the tree and the size of the scene rectangle decide the granularity of the scene鈥檚 partitioning. The size of each scene segment is determined by the following algorithm:
segmentSize = sceneRect().size() / math.pow(2, depth - 1)
The BSP tree has an optimal size when each segment contains between 0 and 10 items.
See also
- PySide2.QtWidgets.QGraphicsScene.setFocus([focusReason=Qt.OtherFocusReason])露
- Parameters:
focusReason 鈥
FocusReason
Sets focus on the scene by sending a
QFocusEventto the scene, passingfocusReasonas the reason. If the scene regains focus after having previously lost it while an item had focus, the last focus item will receive focus withfocusReasonas the reason.If the scene already has focus, this function does nothing.
See also
- PySide2.QtWidgets.QGraphicsScene.setFocusItem(item[, focusReason=Qt.OtherFocusReason])露
- Parameters:
focusReason 鈥
FocusReason
Sets the scene鈥檚 focus item to
item, with the focus reasonfocusReason, after removing focus from any previous item that may have had focus.If
itemisNone, or if it either does not accept focus (i.e., it does not have theItemIsFocusableflag enabled), or is not visible or not enabled, this function only removes focus from any previous focusitem.If item is not
None, and the scene does not currently have focus (i.e.,hasFocus()returnsfalse), this function will callsetFocus()automatically.See also
- PySide2.QtWidgets.QGraphicsScene.setFocusOnTouch(enabled)露
- Parameters:
enabled 鈥 bool
This property holds whether items gain focus when receiving a touch begin event..
The usual behavior is to transfer focus only when an item is clicked. Often a tap on a touchpad is interpreted as equivalent to a mouse click by the operating system, generating a synthesized click event in response. However, at least on macOS you can configure this behavior.
By default,
QGraphicsScenealso transfers focus when you touch on a trackpad or similar. If the operating system is configured to not generate a synthetic mouse click on tapping the trackpad, this is surprising. If the operating system does generate synthetic mouse clicks on tapping the trackpad, the focus transfer on starting a touch gesture is unnecessary.With switched off,
QGraphicsScenebehaves as one would expect on macOS.The default value is
true, ensuring that the default behavior is just as in Qt versions prior to 5.12. Set tofalseto prevent touch events from triggering focus changes.
- PySide2.QtWidgets.QGraphicsScene.setFont(font)露
- Parameters:
font 鈥
PySide2.QtGui.QFont
This property holds the scene鈥檚 default font.
This property provides the scene鈥檚 font. The scene font defaults to, and resolves all its entries from,
font.If the scene鈥檚 font changes, either directly through or indirectly when the application font changes,
QGraphicsScenefirst sends itself aFontChangeevent, and it then sendsFontChangeevents to all top-level widget items in the scene. These items respond by resolving their own fonts to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their fonts.Changing the scene font, (directly or indirectly through
setFont(),) automatically schedules a redraw the entire scene.
- PySide2.QtWidgets.QGraphicsScene.setForegroundBrush(brush)露
- Parameters:
brush 鈥
PySide2.QtGui.QBrush
This property holds the foreground brush of the scene..
Change this property to set the scene鈥檚 foreground to a different color, gradient or texture.
The foreground is drawn after (on top of) the items. The default foreground brush is
NoBrush( i.e. the foreground is not drawn).Example:
scene = QGraphicsScene() view = QGraphicsView(scene) view.show() # a white semi-transparent foreground scene.setForegroundBrush(QColor(255, 255, 255, 127)) # a grid foreground scene.setForegroundBrush(QBrush(Qt.lightGray, Qt.CrossPattern))
render()callsdrawForeground()to draw the scene foreground. For more detailed control over how the foreground is drawn, you can reimplement thedrawForeground()function in aQGraphicsScenesubclass.
- PySide2.QtWidgets.QGraphicsScene.setItemIndexMethod(method)露
- Parameters:
method 鈥
ItemIndexMethod
This property holds the item indexing method..
QGraphicsSceneapplies an indexing algorithm to the scene, to speed up item discovery functions likeitems()anditemAt(). Indexing is most efficient for static scenes (i.e., where items don鈥檛 move around). For dynamic scenes, or scenes with many animated items, the index bookkeeping can outweigh the fast lookup speeds.For the common case, the default index method
BspTreeIndexworks fine. If your scene uses many animations and you are experiencing slowness, you can disable indexing by callingsetItemIndexMethod(NoIndex).See also
- PySide2.QtWidgets.QGraphicsScene.setMinimumRenderSize(minSize)露
- Parameters:
minSize 鈥 float
This property holds the minimal view-transformed size an item must have to be drawn.
When the scene is rendered, any item whose width or height, transformed to the target view, is smaller that , will not be rendered. If an item is not rendered and it clips its children items they will also not be rendered. Set this value to speed up rendering of scenes with many objects rendered on a zoomed out view.
The default value is 0. If unset, or if set to 0 or a negative value, all items will always be rendered.
For example, setting this property can be especially useful if a scene is rendered by multiple views, one of which serves as an overview which always displays all items. In scenes with many items, such a view will use a high scaling factor so that all items can be shown. Due to the scaling, smaller items will only make an insignificant contribution to the final rendered scene. To avoid drawing these items and reduce the time necessary to render the scene, you can call with a non-negative value.
Note
Items that are not drawn as a result of being too small, are still returned by methods such as
items()anditemAt(), and participate in collision detection and interactions. It is recommended that you set to a value less than or equal to 1 in order to avoid large unrendered items that are interactive.See also
- PySide2.QtWidgets.QGraphicsScene.setPalette(palette)露
- Parameters:
palette 鈥
PySide2.QtGui.QPalette
This property holds the scene鈥檚 default palette.
This property provides the scene鈥檚 palette. The scene palette defaults to, and resolves all its entries from,
palette.If the scene鈥檚 palette changes, either directly through or indirectly when the application palette changes,
QGraphicsScenefirst sends itself aPaletteChangeevent, and it then sendsPaletteChangeevents to all top-level widget items in the scene. These items respond by resolving their own palettes to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their palettes.Changing the scene palette, (directly or indirectly through
setPalette(),) automatically schedules a redraw the entire scene.See also
- PySide2.QtWidgets.QGraphicsScene.setSceneRect(rect)露
- Parameters:
rect 鈥
PySide2.QtCore.QRectF
This property holds the scene rectangle; the bounding rectangle of the scene.
The scene rectangle defines the extent of the scene. It is primarily used by
QGraphicsViewto determine the view鈥檚 default scrollable area, and byQGraphicsSceneto manage item indexing.If unset, or if set to a null
QRectF, will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
- PySide2.QtWidgets.QGraphicsScene.setSceneRect(x, y, w, h)
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
- PySide2.QtWidgets.QGraphicsScene.setSelectionArea(path[, mode=Qt.IntersectsItemShape[, deviceTransform=QTransform()]])露
- Parameters:
path 鈥
PySide2.QtGui.QPainterPathmode 鈥
ItemSelectionModedeviceTransform 鈥
PySide2.QtGui.QTransform
This is an overloaded function.
Sets the selection area to
pathusingmodeto determine if items are included in the selection area.deviceTransformis the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.See also
- PySide2.QtWidgets.QGraphicsScene.setSelectionArea(path, selectionOperation[, mode=Qt.IntersectsItemShape[, deviceTransform=QTransform()]])
- Parameters:
path 鈥
PySide2.QtGui.QPainterPathselectionOperation 鈥
ItemSelectionOperationmode 鈥
ItemSelectionModedeviceTransform 鈥
PySide2.QtGui.QTransform
This is an overloaded function.
Sets the selection area to
pathusingmodeto determine if items are included in the selection area.deviceTransformis the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.selectionOperationdetermines what to do with the currently selected items.See also
- PySide2.QtWidgets.QGraphicsScene.setSelectionArea(path, deviceTransform)
- Parameters:
path 鈥
PySide2.QtGui.QPainterPathdeviceTransform 鈥
PySide2.QtGui.QTransform
Sets the selection area to
path. All items within this area are immediately selected, and all items outside are unselected. You can get the list of all selected items by callingselectedItems().deviceTransformis the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.For an item to be selected, it must be marked as selectable (
ItemIsSelectable).See also
- PySide2.QtWidgets.QGraphicsScene.setSortCacheEnabled(enabled)露
- Parameters:
enabled 鈥 bool
Note
This function is deprecated.
This property holds whether sort caching is enabled.
Since Qt 4.6, this property has no effect.
- PySide2.QtWidgets.QGraphicsScene.setStickyFocus(enabled)露
- Parameters:
enabled 鈥 bool
This property holds whether clicking into the scene background will clear focus.
In a
QGraphicsScenewith set to true, focus will remain unchanged when the user clicks into the scene background or on an item that does not accept focus. Otherwise, focus will be cleared.By default, this property is
false.Focus changes in response to a mouse press. You can reimplement
mousePressEvent()in a subclass ofQGraphicsSceneto toggle this property based on where the user has clicked.See also
- PySide2.QtWidgets.QGraphicsScene.setStyle(style)露
- Parameters:
style 鈥
PySide2.QtWidgets.QStyle
Sets or replaces the style of the scene to
style, and reparents the style to this scene. Any previously assigned style is deleted. The scene鈥檚 style defaults tostyle(), and serves as the default for all QGraphicsWidget items in the scene.Changing the style, either directly by calling this function, or indirectly by calling
setStyle(), will automatically update the style for all widgets in the scene that do not have a style explicitly assigned to them.If
styleisNone,QGraphicsScenewill revert tostyle().See also
- PySide2.QtWidgets.QGraphicsScene.stickyFocus()露
- Return type:
bool
This property holds whether clicking into the scene background will clear focus.
In a
QGraphicsScenewith set to true, focus will remain unchanged when the user clicks into the scene background or on an item that does not accept focus. Otherwise, focus will be cleared.By default, this property is
false.Focus changes in response to a mouse press. You can reimplement
mousePressEvent()in a subclass ofQGraphicsSceneto toggle this property based on where the user has clicked.See also
- PySide2.QtWidgets.QGraphicsScene.style()露
- Return type:
Returns the scene鈥檚 style, or the same as
style()if the scene has not been explicitly assigned a style.See also
- PySide2.QtWidgets.QGraphicsScene.update([rect=QRectF()])露
- Parameters:
rect 鈥
PySide2.QtCore.QRectF
Schedules a redraw of the area
recton the scene.See also
- PySide2.QtWidgets.QGraphicsScene.update(x, y, w, h)
- Parameters:
x 鈥 float
y 鈥 float
w 鈥 float
h 鈥 float
This is an overloaded function.
This function is equivalent to calling update(
QRectF(x,y,w,h));
- PySide2.QtWidgets.QGraphicsScene.views()露
- Return type:
Returns a list of all the views that display this scene.
See also
- PySide2.QtWidgets.QGraphicsScene.wheelEvent(event)露
- Parameters:
This event handler, for event
wheelEvent, can be reimplemented in a subclass to receive mouse wheel events for the scene.By default, the event is delivered to the topmost visible item under the cursor. If ignored, the event propagates to the item beneath, and again until the event is accepted, or it reaches the scene. If no items accept the event, it is ignored.
Note: See
items()for a definition of which items are considered visible by this function.See also
- PySide2.QtWidgets.QGraphicsScene.width()露
- Return type:
float
This convenience function is equivalent to calling
sceneRect()..See also
漏 2022 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.