QGraphicsScene Class Reference

[ QtGui module]

The QGraphicsScene class provides a surface for managing a large number of 2D graphical items. 更多...

继承 QObject .

类型

方法

Qt Signals


详细描述

The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.

The class serves as a container for QGraphicsItems. It is used together with QGraphicsView for visualizing graphical items, such as lines, rectangles, text, or even custom items, on a 2D surface. QGraphicsScene is part of the 图形视图框架 .

QGraphicsScene also 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 the QGraphicsView widget, you can either visualize the whole scene, or zoom in and view only parts of the scene.

范例:

 QGraphicsScene scene;
 scene.addText("Hello, world!");
 QGraphicsView view(&scene);
 view.show();
			

Note that QGraphicsScene has no visual appearance of its own; it only manages the items. You need to create a QGraphicsView widget to visualize the scene.

To add items to a scene, you start off by constructing a QGraphicsScene object. Then, you have two options: either add your existing QGraphicsItem objects by calling addItem (), or you can call one of the convenience functions addEllipse (), addLine (), addPath (), addPixmap (), addPolygon (), addRect (),或 addText (), which all return a pointer to the newly added item. The dimensions of the items added with these functions are relative to the item's 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) QGraphicsScene emits the changed () signal. To remove an item, call removeItem ().

QGraphicsScene uses 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 calling setItemIndexMethod (). For more information about the available indexing algorithms, see the itemIndexMethod 特性。

The scene's 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's item index. If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect (), as the scene rect. However, itemsBoundingRect () 是 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's greatest strengths is its ability to efficiently determine the location of items. Even with millions of items on the scene, the items () functions can determine the location of an item within few milliseconds. There are several overloads to items (): 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 an itemAt () function that returns the topmost item at a given position.

QGraphicsScene maintains selection information for the scene. To select items, call setSelectionArea (),和 to clear the current selection, call clearSelection ()。调用 selectedItems () 到 get the list of all selected items.

事件处理和传播

Another responsibility that QGraphicsScene has, is to propagate events from QGraphicsView . To send an event to a scene, you construct an event that inherits QEvent , and then send it using, for example, QApplication.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 by keyPressEvent (), and mouse press events are handled by mousePressEvent ().

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 call QGraphicsItem.setFocus ()。调用 focusItem () 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. If setFocus () 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 calls clearFocus ()) 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, QGraphicsScene dispatches hover events . If an item accepts hover events (see QGraphicsItem.acceptHoverEvents ()), it will receive a GraphicsSceneHoverEnter event when the mouse enters its area. As the mouse continues moving inside the item's area, QGraphicsScene will send it GraphicsSceneHoverMove events. When the mouse leaves the item's area, the item will receive a GraphicsSceneHoverLeave 事件。

All mouse events are delivered to the current mouse grabber item. An item becomes the scene's mouse grabber if it accepts mouse events (see QGraphicsItem.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. 可以调用 mouseGrabberItem () 到 determine what item is currently grabbing the mouse.


类型文档编制

QGraphicsScene.ItemIndexMethod

This enum describes the indexing algorithms QGraphicsScene provides for managing positional information about items on the scene.

常量 描述
QGraphicsScene.BspTreeIndex 0 A Binary Space Partitioning tree is applied. 所有 QGraphicsScene 's 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 -1 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.

另请参阅 setItemIndexMethod () and bspTreeDepth .

QGraphicsScene.SceneLayer

This enum describes the rendering layers in a QGraphicsScene 。当 QGraphicsScene draws the scene contents, it renders each of these layers separately, in order.

Each layer represents a flag that can be OR'ed together when calling functions such as invalidate () 或 QGraphicsView.invalidateScene ().

常量 描述
QGraphicsScene.ItemLayer 0x1 The item layer. QGraphicsScene renders all items are in this layer by calling the virtual function drawItems (). The item layer is drawn after the background layer, but before the foreground layer.
QGraphicsScene.BackgroundLayer 0x2 The background layer. QGraphicsScene renders the scene's background in this layer by calling the virtual function drawBackground ()。 background layer is drawn first of all layers.
QGraphicsScene.ForegroundLayer 0x4 The foreground layer. QGraphicsScene renders the scene's foreground in this layer by calling the virtual function drawForeground ()。 foreground layer is drawn last of all layers.
QGraphicsScene.AllLayers 0xffff All layers; this value represents a combination of all three layers.

该枚举在 Qt 4.3 引入或被修改。

The SceneLayers type is a typedef for QFlags <SceneLayer>. It stores an OR combination of SceneLayer values.

另请参阅 invalidate () 和 QGraphicsView.invalidateScene ().


方法文档编制

QGraphicsScene.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QGraphicsScene 对象。 parent 参数会被传递给 QObject 的构造函数。

QGraphicsScene.__init__ ( self , QRectF   sceneRect , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QGraphicsScene object, using sceneRect for its scene rectangle. The parent 参数会被传递给 QObject 的构造函数。

另请参阅 sceneRect .

QGraphicsScene.__init__ ( self , float  x , float  y , float  width , float  height , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QGraphicsScene object, using the rectangle specified by ( x , y ), and the given width and height for its scene rectangle. parent 参数会被传递给 QObject 的构造函数。

另请参阅 sceneRect .

QGraphicsItem QGraphicsScene.activePanel ( self )

Returns the current active panel, or 0 if no panel is currently active.

该函数在 Qt 4.6 引入。

另请参阅 QGraphicsScene.setActivePanel ().

QGraphicsWidget QGraphicsScene.activeWindow ( self )

Returns the current active window, or 0 if no window is currently active.

该函数在 Qt 4.4 引入。

另请参阅 QGraphicsScene.setActiveWindow ().

QGraphicsEllipseItem QGraphicsScene.addEllipse ( self , QRectF   rect , QPen   pen  = QPen(), QBrush   brush  = QBrush())

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 to pen and brush .

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addLine (), addPath (), addPixmap (), addRect (), addText (), addItem (),和 addWidget ().

QGraphicsEllipseItem QGraphicsScene.addEllipse ( self , float  x , float  y , float  w , float  h , QPen   pen  = QPen(), QBrush   brush  = QBrush())

This convenience function is equivalent to calling addEllipse( QRectF ( x , y , w , h ), pen , brush ).

该函数在 Qt 4.3 引入。

QGraphicsScene.addItem ( self , QGraphicsItem   item )

item argument has it's ownership transferred to Qt.

Adds or moves the item and all its childen to this scene. This scene takes ownership of the item .

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () 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.

QGraphicsScene will send ItemSceneChange notifications to item while 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), QGraphicsScene will 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.

另请参阅 removeItem (), addEllipse (), addLine (), addPath (), addPixmap (), addRect (), addText (), addWidget (),和 排序 .

QGraphicsLineItem QGraphicsScene.addLine ( self , QLineF   line , QPen   pen  = QPen())

Creates and adds a line item to the scene, and returns the item pointer. The geometry of the line is defined by line ,和 its pen is initialized to pen .

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addPath (), addPixmap (), addRect (), addText (), addItem (),和 addWidget ().

QGraphicsLineItem QGraphicsScene.addLine ( self , float  x1 , float  y1 , float  x2 , float  y2 , QPen   pen  = QPen())

This convenience function is equivalent to calling addLine( QLineF ( x1 , y1 , x2 , y2 ), pen ).

该函数在 Qt 4.3 引入。

QGraphicsPathItem QGraphicsScene.addPath ( self , QPainterPath   path , QPen   pen  = QPen(), QBrush   brush  = QBrush())

Creates and adds a path item to the scene, and returns the item pointer. The geometry of the path is defined by path ,和 its pen and brush are initialized to pen and brush .

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPixmap (), addRect (), addText (), addItem (),和 addWidget ().

QGraphicsPixmapItem QGraphicsScene.addPixmap ( self , QPixmap   pixmap )

Creates and adds a pixmap item to the scene, and returns the item pointer. The pixmap is defined by pixmap .

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPath (), addRect (), addText (), addItem (),和 addWidget ().

QGraphicsPolygonItem QGraphicsScene.addPolygon ( self , QPolygonF   polygon , QPen   pen  = QPen(), QBrush   brush  = QBrush())

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 to pen and brush .

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPath (), addRect (), addText (), addItem (),和 addWidget ().

QGraphicsRectItem QGraphicsScene.addRect ( self , QRectF   rect , QPen   pen  = QPen(), QBrush   brush  = QBrush())

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 to pen and brush .

Note that the item's 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 items coordinate system.

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPixmap (), addPixmap (), addText (), addItem (),和 addWidget ().

QGraphicsRectItem QGraphicsScene.addRect ( self , float  x , float  y , float  w , float  h , QPen   pen  = QPen(), QBrush   brush  = QBrush())

This convenience function is equivalent to calling addRect( QRectF ( x , y , w , h ), pen , brush ).

该函数在 Qt 4.3 引入。

QGraphicsSimpleTextItem QGraphicsScene.addSimpleText ( self , QString  text , QFont   font  = QFont())

Creates and adds a QGraphicsSimpleTextItem 到 scene, and returns the item pointer. The text string is initialized to text , and its font is initialized to font .

The item's position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPixmap (), addPixmap (), addRect (), addItem (),和 addWidget ().

QGraphicsTextItem QGraphicsScene.addText ( self , QString  text , QFont   font  = QFont())

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 to font .

The item's position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

另请参阅 addEllipse (), addLine (), addPixmap (), addPixmap (), addRect (), addItem (),和 addWidget ().

QGraphicsProxyWidget QGraphicsScene.addWidget ( self , QWidget   widget , Qt.WindowFlags   flags  = 0)

widget argument has it's ownership transferred to Qt.

创建新的 QGraphicsProxyWidget for widget , adds it to the scene, and returns a pointer to the proxy. wFlags set the default window flags for the embedding proxy widget.

The item's position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem.isVisible () returns true), QGraphicsScene will emit changed () once control goes back to the event loop.

Note that widgets with the Qt.WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller are not supported. Examples are QGLWidget and QAxWidget .

另请参阅 addEllipse (), addLine (), addPixmap (), addPixmap (), addRect (), addText (), addSimpleText (),和 addItem ().

QGraphicsScene.advance ( self )

This method is also a Qt slot with the C++ signature void advance() .

This slot advances the scene by one step, by calling QGraphicsItem.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, QGraphicsItem.advance () is called passing a value of 0 as an argument, and 1 is passed in the second phase.

另请参阅 QGraphicsItem.advance (), QGraphicsItemAnimation ,和 QTimeLine .

QBrush QGraphicsScene.backgroundBrush ( self )

int QGraphicsScene.bspTreeDepth ( self )

QGraphicsScene.clear ( self )

This method is also a Qt slot with the C++ signature void clear() .

Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.

该函数在 Qt 4.4 引入。

另请参阅 addItem ().

QGraphicsScene.clearFocus ( self )

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.

另请参阅 hasFocus (), setFocus (),和 setFocusItem ().

QGraphicsScene.clearSelection ( self )

Clears the current selection.

另请参阅 setSelectionArea () 和 selectedItems ().

list-of-QGraphicsItem QGraphicsScene.collidingItems ( self , QGraphicsItem   item , Qt.ItemSelectionMode   mode  = Qt.IntersectsItemShape)

Returns a list of all items that collide with item . Collisions are determined by calling QGraphicsItem.collidesWithItem (); the collision detection is determined by mode . By default, all items whose shape intersects item or is contained inside item 's 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).

另请参阅 items (), itemAt (), QGraphicsItem.collidesWithItem (), and 排序 .

QGraphicsScene.contextMenuEvent ( self , QGraphicsSceneContextMenuEvent   event )

此事件处理程序对于事件 contextMenuEvent , can be reimplemented in a subclass to receive context menu events. The default implementation forwards the event to the topmost 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.

另请参阅 QGraphicsItem.contextMenuEvent ().

QGraphicsItemGroup QGraphicsScene.createItemGroup ( self , list-of-QGraphicsItem  items )

items argument has it's ownership transferred to Qt.

Groups all items in items into a new QGraphicsItemGroup , and returns a pointer to the group. The group is created with the common ancestor of items as 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. If items is empty, this function will return an empty top-level QGraphicsItemGroup .

QGraphicsScene has ownership of the group item; you do not need to delete it. To dismantle (ungroup) a group, call destroyItemGroup ().

另请参阅 destroyItemGroup () 和 QGraphicsItemGroup.addToGroup ().

QGraphicsScene.destroyItemGroup ( self , QGraphicsItemGroup   group )

group argument has it's ownership transferred to Qt.

Reparents all items in group to group 's parent item, then removes group from the scene, and finally deletes it. The items' positions and transformations are mapped from the group to the group's parent.

另请参阅 createItemGroup () 和 QGraphicsItemGroup.removeFromGroup ().

QGraphicsScene.dragEnterEvent ( self , QGraphicsSceneDragDropEvent   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.

另请参阅 QGraphicsItem.dragEnterEvent (), dragMoveEvent (), dragLeaveEvent (), and dropEvent ().

QGraphicsScene.dragLeaveEvent ( self , QGraphicsSceneDragDropEvent   event )

此事件处理程序对于事件 event , can be reimplemented in a subclass to receive drag leave events for the scene.

另请参阅 QGraphicsItem.dragLeaveEvent (), dragEnterEvent (), dragMoveEvent (), and dropEvent ().

QGraphicsScene.dragMoveEvent ( self , QGraphicsSceneDragDropEvent   event )

此事件处理程序对于事件 event , can be reimplemented in a subclass to receive drag move events for the scene.

另请参阅 QGraphicsItem.dragMoveEvent (), dragEnterEvent (), dragLeaveEvent (), and dropEvent ().

QGraphicsScene.drawBackground ( self , QPainter   painter , QRectF   rect )

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 rect parameter is the exposed rectangle.

If all you want is to define a color, texture, or gradient for the background, you can call setBackgroundBrush () 代替。

另请参阅 drawForeground () 和 drawItems ().

QGraphicsScene.drawForeground ( self , QPainter   painter , QRectF   rect )

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 rect parameter is the exposed rectangle.

If all you want is to define a color, texture or gradient for the foreground, you can call setForegroundBrush () 代替。

另请参阅 drawBackground () 和 drawItems ().

QGraphicsScene.drawItems ( self , QPainter   painter , list-of-QGraphicsItem  items , list-of-QStyleOptionGraphicsItem  options , QWidget   widget  = None)

QGraphicsScene.dropEvent ( self , QGraphicsSceneDragDropEvent   event )

此事件处理程序对于事件 event , can be reimplemented in a subclass to receive drop events for the scene.

另请参阅 QGraphicsItem.dropEvent (), dragEnterEvent (), dragMoveEvent (), and dragLeaveEvent ().

bool QGraphicsScene.event ( self , QEvent   event )

重实现自 QObject.event ().

Processes the event event , and dispatches it to the respective event handlers.

In addition to calling the convenience event handlers, this function is responsible for converting mouse move events to hover events for when there is no mouse grabber item. Hover events are delivered directly to items; there is no convenience function for them.

不像 QWidget , QGraphicsScene does not have the convenience functions enterEvent() and leaveEvent() . Use this function to obtain those events instead.

另请参阅 contextMenuEvent (), keyPressEvent (), keyReleaseEvent (), mousePressEvent (), mouseMoveEvent (), mouseReleaseEvent (), mouseDoubleClickEvent (), focusInEvent (),和 focusOutEvent ().

bool QGraphicsScene.eventFilter ( self , QObject   watched , QEvent   event )

重实现自 QObject.eventFilter ().

QGraphicsScene filters QApplication 's events to detect palette and font changes.

QGraphicsScene.focusInEvent ( self , QFocusEvent   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.

另请参阅 QGraphicsItem.focusOutEvent ().

QGraphicsItem QGraphicsScene.focusItem ( self )

When the scene is active, this functions returns the scene's current focus item, or 0 if 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.

另请参阅 setFocusItem (), QGraphicsItem.hasFocus (),和 isActive ().

bool QGraphicsScene.focusNextPrevChild ( self , bool  next )

Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it cannot. If next is true, this function searches forward; if next is false, it searches backward.

You can reimplement this function in a subclass of QGraphicsScene to provide fine-grained control over how tab focus passes inside your scene. The default implementation is based on the tab focus chain defined by QGraphicsWidget.setTabOrder ().

该函数在 Qt 4.4 引入。

QGraphicsScene.focusOutEvent ( self , QFocusEvent   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.

另请参阅 QGraphicsItem.focusInEvent ().

QFont QGraphicsScene.font ( self )

QBrush QGraphicsScene.foregroundBrush ( self )

bool QGraphicsScene.hasFocus ( self )

Returns true if the scene has focus; otherwise returns false. If the scene has focus, it will will forward key events from QKeyEvent to any item that has focus.

另请参阅 setFocus () 和 setFocusItem ().

float QGraphicsScene.height ( self )

This convenience function is equivalent to calling sceneRect().height() .

另请参阅 width ().

QGraphicsScene.helpEvent ( self , QGraphicsSceneHelpEvent   event )

此事件处理程序对于事件 helpEvent , can be reimplemented in a subclass to receive help events. The events are of type QEvent.ToolTip , which are created when a tooltip is requested.

The default implementation shows the tooltip of the topmost 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.

另请参阅 QGraphicsItem.toolTip () 和 QGraphicsSceneHelpEvent .

QGraphicsScene.inputMethodEvent ( self , QInputMethodEvent   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.

另请参阅 QGraphicsItem.inputMethodEvent ().

QVariant QGraphicsScene.inputMethodQuery ( self , Qt.InputMethodQuery   query )

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.

query parameter specifies which property is queried.

另请参阅 QWidget.inputMethodQuery ().

QGraphicsScene.invalidate ( self , QRectF   rect  = QRectF(), SceneLayers   layers  = QGraphicsScene.AllLayers)

This method is also a Qt slot with the C++ signature void invalidate(const QRectF& = QRectF(),QGraphicsScene::SceneLayers = QGraphicsScene.AllLayers) .

Invalidates and schedules a redraw of the layers in rect on the scene. Any cached content in layers is unconditionally invalidated and redrawn.

You can use this function overload to notify QGraphicsScene of changes to the background or the foreground of the scene. This function is commonly used for scenes with tile-based backgrounds to notify changes when QGraphicsView has enabled CacheBackground .

范例:

 QRectF TileScene.rectForTile(int x, int y) const
 {
     // Return the rectangle for the tile at position (x, y).
     return QRectF(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
 }
 void TileScene.setTile(int x, int y, const QPixmap &pixmap)
 {
     // Sets or replaces the tile at position (x, y) with pixmap.
     if (x >= 0 && x < numTilesH && y >= 0 && y < numTilesV) {
         tiles[y][x] = pixmap;
         invalidate(rectForTile(x, y), BackgroundLayer);
     }
 }
 void TileScene.drawBackground(QPainter *painter, const QRectF &exposed)
 {
     // Draws all tiles that intersect the exposed area.
     for (int y = 0; y < numTilesV; ++y) {
         for (int x = 0; x < numTilesH; ++x) {
             QRectF rect = rectForTile(x, y);
             if (exposed.intersects(rect))
                 painter->drawPixmap(rect.topLeft(), tiles[y][x]);
         }
     }
 }
			

注意: QGraphicsView currently supports background caching only (see QGraphicsView.CacheBackground ). 此函数相当于调用 update () if any layer but BackgroundLayer is passed.

另请参阅 QGraphicsView.resetCachedContent ().

QGraphicsScene.invalidate ( self , float  x , float  y , float  w , float  h , SceneLayers   layers  = QGraphicsScene.AllLayers)

这是重载函数。

This convenience function is equivalent to calling invalidate( QRectF ( x , y , w , h ), layers );

该函数在 Qt 4.3 引入。

bool QGraphicsScene.isActive ( self )

Returns true if the scene is active (e.g., it's viewed by at least one QGraphicsView that is active); otherwise returns false.

该函数在 Qt 4.6 引入。

另请参阅 QGraphicsItem.isActive () 和 QWidget.isActiveWindow ().

bool QGraphicsScene.isSortCacheEnabled ( self )

QGraphicsItem QGraphicsScene.itemAt ( self , QPointF   pos )

Returns the topmost visible item at the specified position , or 0 if there are no items at this position.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 items (), collidingItems (), and 排序 .

QGraphicsItem QGraphicsScene.itemAt ( self , float  x , float  y )

QGraphicsItem QGraphicsScene.itemAt ( self , QPointF   pos , QTransform   deviceTransform )

这是重载函数。

Returns the topmost item at the position specified by ( x , y ), or 0 if there are no items at this position.

deviceTransform is 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) .

该函数在 Qt 4.6 引入。

QGraphicsItem QGraphicsScene.itemAt ( self , float  x , float  y , QTransform   deviceTransform )

ItemIndexMethod QGraphicsScene.itemIndexMethod ( self )

list-of-QGraphicsItem QGraphicsScene.items ( self )

Returns a list of all items in the scene in descending stacking order.

另请参阅 addItem (), removeItem (),和 排序 .

list-of-QGraphicsItem QGraphicsScene.items ( self , Qt.SortOrder   order )

Returns an ordered list of all items on the scene. order decides the stacking order.

另请参阅 addItem (), removeItem (),和 排序 .

list-of-QGraphicsItem QGraphicsScene.items ( self , QPointF   pos )

Returns all visible items that, depending on mode , are at the specified pos in a list sorted using order .

The default value for mode is Qt.IntersectsItemShape ; all items whose exact shape intersects with pos are returned.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 itemAt () 和 排序 .

list-of-QGraphicsItem QGraphicsScene.items ( self , QPointF   pos , Qt.ItemSelectionMode   mode , Qt.SortOrder   order , QTransform   deviceTransform  = QTransform())

list-of-QGraphicsItem QGraphicsScene.items ( self , QRectF   rectangle , Qt.ItemSelectionMode   mode  = Qt.IntersectsItemShape)

list-of-QGraphicsItem QGraphicsScene.items ( self , QRectF   rect , Qt.ItemSelectionMode   mode , Qt.SortOrder   order , QTransform   deviceTransform  = QTransform())

list-of-QGraphicsItem QGraphicsScene.items ( self , QPolygonF   polygon , Qt.ItemSelectionMode   mode  = Qt.IntersectsItemShape)

这是重载函数。

Returns all visible items that, depending on mode , are either inside or intersect with the rectangle defined by x , y , w and h , in a list sorted using order .

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

list-of-QGraphicsItem QGraphicsScene.items ( self , QPolygonF   polygon , Qt.ItemSelectionMode   mode , Qt.SortOrder   order , QTransform   deviceTransform  = QTransform())

list-of-QGraphicsItem QGraphicsScene.items ( self , QPainterPath   path , Qt.ItemSelectionMode   mode  = Qt.IntersectsItemShape)

list-of-QGraphicsItem QGraphicsScene.items ( self , QPainterPath   path , Qt.ItemSelectionMode   mode , Qt.SortOrder   order , QTransform   deviceTransform  = QTransform())

这是重载函数。

Returns all visible items that, depending on mode , are either inside or intersect with the specified rect and return a list sorted using order .

The default value for mode is Qt.IntersectsItemShape ; all items whose exact shape intersects with or is contained by rect are returned.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 itemAt () 和 排序 .

list-of-QGraphicsItem QGraphicsScene.items ( self , float  x , float  y , float  w , float  h , Qt.ItemSelectionMode   mode  = Qt.IntersectsItemShape)

这是重载函数。

Returns all visible items that, depending on mode , are either inside or intersect with the specified polygon and return a list sorted using order .

The default value for mode is Qt.IntersectsItemShape ; all items whose exact shape intersects with or is contained by polygon are returned.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 itemAt () 和 排序 .

list-of-QGraphicsItem QGraphicsScene.items ( self , float  x , float  y , float  w , float  h , Qt.ItemSelectionMode   mode , Qt.SortOrder   order , QTransform   deviceTransform  = QTransform())

这是重载函数。

Returns all visible items that, depending on mode , are either inside or intersect with the specified path and return a list sorted using order .

The default value for mode is Qt.IntersectsItemShape ; all items whose exact shape intersects with or is contained by path are returned.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 itemAt () 和 排序 .

QRectF QGraphicsScene.itemsBoundingRect ( self )

Calculates and returns the bounding rect of all items on the scene. This function works by iterating over all items, and because if this, it can be slow for large scenes.

另请参阅 sceneRect ().

QGraphicsScene.keyPressEvent ( self , QKeyEvent   event )

此事件处理程序对于事件 keyEvent , can be reimplemented in a subclass to receive keypress events. The default implementation forwards the event to current focus item.

另请参阅 QGraphicsItem.keyPressEvent () and focusItem ().

QGraphicsScene.keyReleaseEvent ( self , QKeyEvent   event )

此事件处理程序对于事件 keyEvent , can be reimplemented in a subclass to receive key release events. The default implementation forwards the event to current focus 项。

另请参阅 QGraphicsItem.keyReleaseEvent () and focusItem ().

QGraphicsScene.mouseDoubleClickEvent ( self , QGraphicsSceneMouseEvent   event )

此事件处理程序对于事件 mouseEvent , can be reimplemented in a subclass to receive mouse doubleclick 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 doubleclick event, and finally a release event. If the doubleclick 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 doubleclick events in this case.

The default implementation is similar to mousePressEvent ().

另请参阅 QGraphicsItem.mousePressEvent (), QGraphicsItem.mouseMoveEvent (), QGraphicsItem.mouseReleaseEvent (), and QGraphicsItem.setAcceptedMouseButtons ().

QGraphicsItem QGraphicsScene.mouseGrabberItem ( self )

Returns the current mouse grabber item, or 0 if 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 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).

QGraphicsScene.mouseMoveEvent ( self , QGraphicsSceneMouseEvent   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's ignored.

另请参阅 QGraphicsItem.mousePressEvent (), QGraphicsItem.mouseReleaseEvent (), QGraphicsItem.mouseDoubleClickEvent (), and QGraphicsItem.setAcceptedMouseButtons ().

QGraphicsScene.mousePressEvent ( self , QGraphicsSceneMouseEvent   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 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.

另请参阅 QGraphicsItem.mousePressEvent () and QGraphicsItem.setAcceptedMouseButtons ().

QGraphicsScene.mouseReleaseEvent ( self , QGraphicsSceneMouseEvent   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.

另请参阅 QGraphicsItem.mousePressEvent (), QGraphicsItem.mouseMoveEvent (), QGraphicsItem.mouseDoubleClickEvent (), and QGraphicsItem.setAcceptedMouseButtons ().

QPalette QGraphicsScene.palette ( self )

QGraphicsScene.removeItem ( self , QGraphicsItem   item )

item argument

Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (即, QGraphicsScene will no longer delete item when destroyed).

另请参阅 addItem ().

QGraphicsScene.render ( self , QPainter   painter , QRectF   target  = QRectF(), QRectF   source  = QRectF(), Qt.AspectRatioMode   mode  = Qt.KeepAspectRatio)

渲染 source rect from scene into target , 使用 painter . This function is useful for capturing the contents of the scene onto a paint device, such as a QImage (e.g., to take a screenshot), or for printing with QPrinter . For example:

 QGraphicsScene scene;
 scene.addItem(...
 ...
 QPrinter printer(QPrinter.HighResolution);
 printer.setPaperSize(QPrinter.A4);
 QPainter painter(&printer);
 scene.render(&painter);
			

source is a null rect, this function will use sceneRect () to determine what to render. If target is a null rect, the dimensions of painter 's paint device will be used.

The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in target .

另请参阅 QGraphicsView.render ().

QRectF QGraphicsScene.sceneRect ( self )

list-of-QGraphicsItem QGraphicsScene.selectedItems ( self )

Returns a list of all currently selected items. The items are returned in no particular order.

另请参阅 setSelectionArea ().

QPainterPath QGraphicsScene.selectionArea ( self )

Returns the selection area that was previously set with setSelectionArea (),或 空 QPainterPath if no selection area has been set.

另请参阅 setSelectionArea ().

bool QGraphicsScene.sendEvent ( self , QGraphicsItem   item , QEvent   event )

发送事件 event to item item through possible event filters.

The event is sent only if the item is enabled.

返回 false if the event was filtered or if the item is disabled. Otherwise returns the value that was returned from the event handler.

该函数在 Qt 4.6 引入。

另请参阅 QGraphicsItem.sceneEvent () 和 QGraphicsItem.sceneEventFilter ().

QGraphicsScene.setActivePanel ( self , QGraphicsItem   item )

Activates item , which must be an item in this scene. You can also pass 0 for item , in which case QGraphicsScene will deactivate any currently active panel.

If the scene is currently inactive, item remains inactive until the scene becomes active (or, ir item is 0, no item will be activated).

该函数在 Qt 4.6 引入。

另请参阅 activePanel (), isActive (),和 QGraphicsItem.isActive ().

QGraphicsScene.setActiveWindow ( self , QGraphicsWidget   widget )

Activates widget , which must be a widget in this scene. You can also pass 0 for widget , in which case QGraphicsScene will deactivate any currently active window.

该函数在 Qt 4.4 引入。

另请参阅 activeWindow () 和 QGraphicsWidget.isActiveWindow ().

QGraphicsScene.setBackgroundBrush ( self , QBrush   brush )

QGraphicsScene.setBspTreeDepth ( self , int  depth )

QGraphicsScene.setFocus ( self , Qt.FocusReason   focusReason  = Qt.OtherFocusReason)

Sets focus on the scene by sending a QFocusEvent to the scene, passing focusReason as the reason. If the scene regains focus after having previously lost it while an item had focus, the last focus item will receive focus with focusReason as the reason.

If the scene already has focus, this function does nothing.

另请参阅 hasFocus (), clearFocus (),和 setFocusItem ().

QGraphicsScene.setFocusItem ( self , QGraphicsItem   item , Qt.FocusReason   focusReason  = Qt.OtherFocusReason)

Sets the scene's focus item to item , with the focus reason focusReason , after removing focus from any previous item that may have had focus.

item is 0, or if it either does not accept focus (i.e., it does not have the QGraphicsItem.ItemIsFocusable flag enabled), or is not visible or not enabled, this function only removes focus from any previous focusitem.

If item is not 0, and the scene does not currently have focus (即, hasFocus () returns false), this function will call setFocus () automatically.

另请参阅 focusItem (), hasFocus (),和 setFocus ().

QGraphicsScene.setFont ( self , QFont   font )

QGraphicsScene.setForegroundBrush ( self , QBrush   brush )

QGraphicsScene.setItemIndexMethod ( self , ItemIndexMethod   方法 )

QGraphicsScene.setPalette ( self , QPalette   palette )

QGraphicsScene.setSceneRect ( self , QRectF   rect )

QGraphicsScene.setSceneRect ( self , float  x , float  y , float  w , float  h )

QGraphicsScene.setSelectionArea ( self , QPainterPath   path , QTransform   deviceTransform )

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 calling selectedItems ().

deviceTransform is 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 ( QGraphicsItem.ItemIsSelectable ).

该函数在 Qt 4.6 引入。

另请参阅 clearSelection () 和 selectionArea ().

QGraphicsScene.setSelectionArea ( self , QPainterPath   path )

QGraphicsScene.setSelectionArea ( self , QPainterPath   path , Qt.ItemSelectionMode )

QGraphicsScene.setSelectionArea ( self , QPainterPath   path , Qt.ItemSelectionMode   mode , QTransform   deviceTransform )

这是重载函数。

Sets the selection area to path 使用 mode to determine if items are included in the selection area.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

该函数在 Qt 4.6 引入。

另请参阅 clearSelection () 和 selectionArea ().

QGraphicsScene.setSortCacheEnabled ( self , bool  enabled )

QGraphicsScene.setStickyFocus ( self , bool  enabled )

QGraphicsScene.setStyle ( self , QStyle   style )

style argument has it's ownership transferred to Qt.

Sets or replaces the style of the scene to style ,和 reparents the style to this scene. Any previously assigned style is deleted. The scene's style defaults to QApplication.style (), 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 QApplication.setStyle (), will automatically update the style for all widgets in the scene that do not have a style explicitly assigned to them.

style is 0, QGraphicsScene will revert to QApplication.style ().

该函数在 Qt 4.4 引入。

另请参阅 style ().

bool QGraphicsScene.stickyFocus ( self )

QStyle QGraphicsScene.style ( self )

Returns the scene's style, or the same as QApplication.style () if the scene has not been explicitly assigned a style.

该函数在 Qt 4.4 引入。

另请参阅 setStyle ().

QGraphicsScene.update ( self , QRectF   rect  = QRectF())

This method is also a Qt slot with the C++ signature void update(const QRectF& = QRectF()) .

Schedules a redraw of the area rect on the scene.

另请参阅 sceneRect () 和 changed ().

QGraphicsScene.update ( self , float  x , float  y , float  w , float  h )

这是重载函数。

This function is equivalent to calling update( QRectF ( x , y , w , h ));

该函数在 Qt 4.3 引入。

list-of-QGraphicsView QGraphicsScene.views ( self )

Returns a list of all the views that display this scene.

另请参阅 QGraphicsView.scene ().

QGraphicsScene.wheelEvent ( self , QGraphicsSceneWheelEvent   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.

另请参阅 QGraphicsItem.wheelEvent ().

float QGraphicsScene.width ( self )

This convenience function is equivalent to calling sceneRect ().width().

另请参阅 height ().


Qt Signal Documentation

void changed (const QList<QRectF>&)

This is the default overload of this signal.

此信号被发射由 QGraphicsScene when control reaches the event loop, if the scene content changes. The region parameter contains a list of scene rectangles that indicate the area that has been changed.

另请参阅 QGraphicsView.updateScene ().

void sceneRectChanged (const QRectF&)

This is the default overload of this signal.

此信号被发射由 QGraphicsScene whenever the scene rect 改变。 rect parameter is the new scene rectangle.

另请参阅 QGraphicsView.updateSceneRect ().

void selectionChanged ()

This is the default overload of this signal.

此信号被发射由 QGraphicsScene whenever the selection changes. You can call selectedItems () to get the new list of selected items.

The selection changes whenever an item is selected or unselected, a selection area is set, cleared or otherwise changed, if a preselected item is added to the scene, or if a selected item is removed from the scene.

QGraphicsScene emits this signal only once for group selection operations. For example, if you set a selection area, select or unselect a QGraphicsItemGroup , or if you add or remove from the scene a parent item that contains several selected items, selectionChanged() is emitted only once after the operation has completed (instead of once for each item).

该函数在 Qt 4.3 引入。

另请参阅 setSelectionArea (), selectedItems (), and QGraphicsItem.setSelected ().