QGraphicsLayout

Inherited by: QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsAnchorLayout

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtGui.QGraphicsLayout class provides the base class for all layouts in Graphics View.

PySide.QtGui.QGraphicsLayout is an abstract class that defines a virtual API for arranging PySide.QtGui.QGraphicsWidget children and other PySide.QtGui.QGraphicsLayoutItem objects for a PySide.QtGui.QGraphicsWidget . PySide.QtGui.QGraphicsWidget assigns responsibility to a PySide.QtGui.QGraphicsLayout through QGraphicsWidget.setLayout() . As the widget is resized, the layout will automatically arrange the widget’s children. PySide.QtGui.QGraphicsLayout inherits PySide.QtGui.QGraphicsLayoutItem , so, it can be managed by any layout, including its own subclasses.

Writing a Custom Layout

You can use PySide.QtGui.QGraphicsLayout as a base to write your own custom layout (e.g., a flowlayout), but it is more common to use one of its subclasses instead - PySide.QtGui.QGraphicsLinearLayout or PySide.QtGui.QGraphicsGridLayout . When creating a custom layout, the following functions must be reimplemented as a bare minimum:

Function Description
QGraphicsLayoutItem.setGeometry() Notifies you when the geometry of the layout is set. You can store the geometry in your own layout class in a reimplementation of this function.
QGraphicsLayoutItem.sizeHint() Returns the layout’s size hints.
QGraphicsLayout.count() Returns the number of items in your layout.
QGraphicsLayout.itemAt() Returns a pointer to an item in your layout.
QGraphicsLayout.removeAt() Removes an item from your layout without destroying it.

For more details on how to implement each function, refer to the individual function documentation.

Each layout defines its own API for arranging widgets and layout items. For example, with a grid layout, you require a row and a column index with optional row and column spans, alignment, spacing, and more. A linear layout, however, requires a single row or column index to position its items. For a grid layout, the order of insertion does not affect the layout in any way, but for a linear layout, the order is essential. When writing your own layout subclass, you are free to choose the API that best suits your layout.

For adding layout items to the custom layout, the PySide.QtGui.QGraphicsLayout provides convenience function PySide.QtGui.QGraphicsLayout.addChildLayoutItem() . The function will take care of automatically reparenting graphics items, if needed.

Activating the Layout

When the layout’s geometry changes, PySide.QtGui.QGraphicsLayout immediately rearranges all of its managed items by calling PySide.QtGui.QGraphicsLayoutItem.setGeometry() on each item. This rearrangement is called activating the layout.

PySide.QtGui.QGraphicsLayout updates its own geometry to match the PySide.QtGui.QGraphicsLayoutItem.contentsRect() of the PySide.QtGui.QGraphicsLayoutItem it is managing. Thus, it will automatically rearrange all its items when the widget is resized. PySide.QtGui.QGraphicsLayout caches the sizes of all its managed items to avoid calling PySide.QtGui.QGraphicsLayoutItem.setGeometry() too often.

Note

A PySide.QtGui.QGraphicsLayout will have the same geometry as the PySide.QtGui.QGraphicsLayoutItem.contentsRect() of the widget (not the layout) it is assigned to.

Activating the Layout Implicitly

The layout can be activated implicitly using one of two ways: by calling PySide.QtGui.QGraphicsLayout.activate() or by calling PySide.QtGui.QGraphicsLayout.invalidate() . Calling PySide.QtGui.QGraphicsLayout.activate() activates the layout immediately. In contrast, calling PySide.QtGui.QGraphicsLayout.invalidate() is delayed, as it posts a LayoutRequest event to the managed widget. Due to event compression, the PySide.QtGui.QGraphicsLayout.activate() will only be called once after control has returned to the event loop. This is referred to as invalidating the layout. Invalidating the layout also invalidates any cached information. Also, the PySide.QtGui.QGraphicsLayout.invalidate() function is a virtual function. So, you can invalidate your own cache in a subclass of PySide.QtGui.QGraphicsLayout by reimplementing this function.

Event Handling

PySide.QtGui.QGraphicsLayout listens to events for the widget it manages through the virtual PySide.QtGui.QGraphicsLayout.widgetEvent() event handler. When the layout is assigned to a widget, all events delivered to the widget are first processed by PySide.QtGui.QGraphicsLayout.widgetEvent() . This allows the layout to be aware of any relevant state changes on the widget such as visibility changes or layout direction changes.

Margin Handling

The margins of a PySide.QtGui.QGraphicsLayout can be modified by reimplementing PySide.QtGui.QGraphicsLayout.setContentsMargins() and PySide.QtGui.QGraphicsLayout.getContentsMargins() .
class PySide.QtGui.QGraphicsLayout([parent=None])
Parameters:parentPySide.QtGui.QGraphicsLayoutItem

Contructs a PySide.QtGui.QGraphicsLayout object.

parent is passed to PySide.QtGui.QGraphicsLayoutItem ‘s constructor and the PySide.QtGui.QGraphicsLayoutItem ‘s isLayout argument is set to true .

If parent is a PySide.QtGui.QGraphicsWidget the layout will be installed on that widget. (Note that installing a layout will delete the old one installed.)

PySide.QtGui.QGraphicsLayout.activate()

Activates the layout, causing all items in the layout to be immediately rearranged. This function is based on calling PySide.QtGui.QGraphicsLayout.count() and PySide.QtGui.QGraphicsLayout.itemAt() , and then calling PySide.QtGui.QGraphicsLayoutItem.setGeometry() on all items sequentially. When activated, the layout will adjust its geometry to its parent’s PySide.QtGui.QGraphicsLayoutItem.contentsRect() . The parent will then invalidate any layout of its own.

If called in sequence or recursively, e.g., by one of the arranged items in response to being resized, this function will do nothing.

Note that the layout is free to use geometry caching to optimize this process. To forcefully invalidate any such cache, you can call PySide.QtGui.QGraphicsLayout.invalidate() before calling PySide.QtGui.QGraphicsLayout.activate() .

PySide.QtGui.QGraphicsLayout.addChildLayoutItem(layoutItem)
Parameters:layoutItemPySide.QtGui.QGraphicsLayoutItem

This function is a convenience function provided for custom layouts, and will go through all items in the layout and reparent their graphics items to the closest PySide.QtGui.QGraphicsWidget ancestor of the layout.

If layoutItem is already in a different layout, it will be removed from that layout.

If custom layouts want special behaviour they can ignore to use this function, and implement their own behaviour.

PySide.QtGui.QGraphicsLayout.count()
Return type:PySide.QtCore.int

This pure virtual function must be reimplemented in a subclass of PySide.QtGui.QGraphicsLayout to return the number of items in the layout.

The subclass is free to decide how to store the items.

static PySide.QtGui.QGraphicsLayout.instantInvalidatePropagation()
Return type:PySide.QtCore.bool

returns true if the complete widget/layout hierarchy is rearranged in one go.

PySide.QtGui.QGraphicsLayout.invalidate()

Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event to the managed parent PySide.QtGui.QGraphicsLayoutItem .

PySide.QtGui.QGraphicsLayout.isActivated()
Return type:PySide.QtCore.bool

Returns true if the layout is currently being activated; otherwise, returns false. If the layout is being activated, this means that it is currently in the process of rearranging its items (i.e., the PySide.QtGui.QGraphicsLayout.activate() function has been called, and has not yet returned).

PySide.QtGui.QGraphicsLayout.itemAt(i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtGui.QGraphicsLayoutItem

This pure virtual function must be reimplemented in a subclass of PySide.QtGui.QGraphicsLayout to return a pointer to the item at index i . The reimplementation can assume that i is valid (i.e., it respects the value of PySide.QtGui.QGraphicsLayout.count() ). Together with PySide.QtGui.QGraphicsLayout.count() , it is provided as a means of iterating over all items in a layout.

The subclass is free to decide how to store the items, and the visual arrangement does not have to be reflected through this function.

PySide.QtGui.QGraphicsLayout.removeAt(index)
Parameters:indexPySide.QtCore.int

This pure virtual function must be reimplemented in a subclass of PySide.QtGui.QGraphicsLayout to remove the item at index . The reimplementation can assume that index is valid (i.e., it respects the value of PySide.QtGui.QGraphicsLayout.count() ).

The implementation must ensure that the PySide.QtGui.QGraphicsLayoutItem.parentLayoutItem() of the removed item does not point to this layout, since the item is considered to be removed from the layout hierarchy.

If the layout is to be reused between applications, we recommend that the layout deletes the item, but the graphics view framework does not depend on this.

The subclass is free to decide how to store the items.

PySide.QtGui.QGraphicsLayout.setContentsMargins(left, top, right, bottom)
Parameters:
  • leftPySide.QtCore.qreal
  • topPySide.QtCore.qreal
  • rightPySide.QtCore.qreal
  • bottomPySide.QtCore.qreal

Sets the contents margins to left , top , right and bottom . The default contents margins for toplevel layouts are style dependent (by querying the pixelMetric for QStyle.PM_LayoutLeftMargin , QStyle.PM_LayoutTopMargin , QStyle.PM_LayoutRightMargin and QStyle.PM_LayoutBottomMargin ).

For sublayouts the default margins are 0.

Changing the contents margins automatically invalidates the layout.

static PySide.QtGui.QGraphicsLayout.setInstantInvalidatePropagation(enable)
Parameters:enablePySide.QtCore.bool

Calling this function with enable set to true will enable a feature that makes propagation of invalidation up to ancestor layout items to be done in one go. It will propagate up the PySide.QtGui.QGraphicsLayoutItem.parentLayoutItem() hierarchy until it has reached the root. If the root item is a PySide.QtGui.QGraphicsWidget , it will *post* a layout request to it. When the layout request is consumed it will traverse down the hierarchy of layouts and widgets and activate all layouts that is invalid (not activated). This is the recommended behaviour.

If not set it will also propagate up the PySide.QtGui.QGraphicsLayoutItem.parentLayoutItem() hierarchy, but it will stop at the first widget it encounters, and post a layout request to the widget. When the layout request is consumed, this might cause it to continue propagation up to the PySide.QtGui.QGraphicsLayoutItem.parentLayoutItem() of the widget. It will continue in this fashion until it has reached a widget with no PySide.QtGui.QGraphicsLayoutItem.parentLayoutItem() . This strategy might cause drawing artifacts, since it is not done in one go, and the consumption of layout requests might be interleaved by consumption of paint events, which might cause significant flicker. Note, this is not the recommended behavior, but for compatibility reasons this is the default behaviour.

PySide.QtGui.QGraphicsLayout.widgetEvent(e)
Parameters:ePySide.QtCore.QEvent

This virtual event handler receives all events for the managed widget. PySide.QtGui.QGraphicsLayout uses this event handler to listen for layout related events such as geometry changes, layout changes or layout direction changes.

e is a pointer to the event.

You can reimplement this event handler to track similar events for your own custom layout.

See also

QGraphicsWidget.event() QGraphicsItem.sceneEvent()