Qt 4.8
Public Functions | Static Public Functions | Protected Functions | Friends | List of all members
QGraphicsLayout Class Referenceabstract

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

#include <qgraphicslayout.h>

Inheritance diagram for QGraphicsLayout:
QGraphicsLayoutItem QGraphicsAnchorLayout QGraphicsGridLayout QGraphicsLinearLayout

Public Functions

void activate ()
 Activates the layout, causing all items in the layout to be immediately rearranged. More...
 
virtual int count () const =0
 This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return the number of items in the layout. More...
 
void getContentsMargins (qreal *left, qreal *top, qreal *right, qreal *bottom) const
 Reimplemented Function More...
 
virtual void invalidate ()
 Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event to the managed parent QGraphicsLayoutItem. More...
 
bool isActivated () const
 Returns true if the layout is currently being activated; otherwise, returns false. More...
 
virtual QGraphicsLayoutItemitemAt (int i) const =0
 This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return a pointer to the item at index i. More...
 
 QGraphicsLayout (QGraphicsLayoutItem *parent=0)
 Contructs a QGraphicsLayout object. More...
 
virtual void removeAt (int index)=0
 This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to remove the item at index. More...
 
void setContentsMargins (qreal left, qreal top, qreal right, qreal bottom)
 Sets the contents margins to left, top, right and bottom. More...
 
virtual void updateGeometry ()
 Reimplemented Function More...
 
virtual void widgetEvent (QEvent *e)
 This virtual event handler receives all events for the managed widget. More...
 
 ~QGraphicsLayout ()
 Destroys the QGraphicsLayout object. More...
 
- Public Functions inherited from QGraphicsLayoutItem
QRectF contentsRect () const
 Returns the contents rect in local coordinates. More...
 
QSizeF effectiveSizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
 Returns the effective size hint for this QGraphicsLayoutItem. More...
 
QRectF geometry () const
 Returns the item's geometry (e. More...
 
QGraphicsItemgraphicsItem () const
 Returns the QGraphicsItem that this layout item represents. More...
 
bool isLayout () const
 Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges other QGraphicsLayoutItem objects); otherwise returns false. More...
 
qreal maximumHeight () const
 Returns the maximum height. More...
 
QSizeF maximumSize () const
 Returns the maximum size. More...
 
qreal maximumWidth () const
 Returns the maximum width. More...
 
qreal minimumHeight () const
 Returns the minimum height. More...
 
QSizeF minimumSize () const
 Returns the minimum size. More...
 
qreal minimumWidth () const
 Returns the minimum width. More...
 
bool ownedByLayout () const
 Returns whether a layout should delete this item in its destructor. More...
 
QGraphicsLayoutItemparentLayoutItem () const
 Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not inherit from QGraphicsLayoutItem (QGraphicsLayoutItem is often used through multiple inheritance with QObject-derived classes). More...
 
qreal preferredHeight () const
 Returns the preferred height. More...
 
QSizeF preferredSize () const
 Returns the preferred size. More...
 
qreal preferredWidth () const
 Returns the preferred width. More...
 
 QGraphicsLayoutItem (QGraphicsLayoutItem *parent=0, bool isLayout=false)
 Constructs the QGraphicsLayoutItem object. More...
 
virtual void setGeometry (const QRectF &rect)
 This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordinates (e. More...
 
void setMaximumHeight (qreal height)
 Sets the maximum height to height. More...
 
void setMaximumSize (const QSizeF &size)
 Sets the maximum size to size. More...
 
void setMaximumSize (qreal w, qreal h)
 This convenience function is equivalent to calling setMaximumSize(QSizeF(w, h)). More...
 
void setMaximumWidth (qreal width)
 Sets the maximum width to width. More...
 
void setMinimumHeight (qreal height)
 Sets the minimum height to height. More...
 
void setMinimumSize (const QSizeF &size)
 Sets the minimum size to size. More...
 
void setMinimumSize (qreal w, qreal h)
 This convenience function is equivalent to calling setMinimumSize(QSizeF(w, h)). More...
 
void setMinimumWidth (qreal width)
 Sets the minimum width to width. More...
 
void setParentLayoutItem (QGraphicsLayoutItem *parent)
 Sets the parent of this QGraphicsLayoutItem to parent. More...
 
void setPreferredHeight (qreal height)
 Sets the preferred height to height. More...
 
void setPreferredSize (const QSizeF &size)
 Sets the preferred size to size. More...
 
void setPreferredSize (qreal w, qreal h)
 This convenience function is equivalent to calling setPreferredSize(QSizeF(w, h)). More...
 
void setPreferredWidth (qreal width)
 Sets the preferred width to width. More...
 
void setSizePolicy (const QSizePolicy &policy)
 Sets the size policy to policy. More...
 
void setSizePolicy (QSizePolicy::Policy hPolicy, QSizePolicy::Policy vPolicy, QSizePolicy::ControlType controlType=QSizePolicy::DefaultType)
 This function is equivalent to calling setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType)). More...
 
QSizePolicy sizePolicy () const
 Returns the current size policy. More...
 
virtual ~QGraphicsLayoutItem ()
 Destroys the QGraphicsLayoutItem object. More...
 

Static Public Functions

static bool instantInvalidatePropagation ()
 
static void setInstantInvalidatePropagation (bool enable)
 

Protected Functions

void addChildLayoutItem (QGraphicsLayoutItem *layoutItem)
 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 QGraphicsWidget ancestor of the layout. More...
 
 QGraphicsLayout (QGraphicsLayoutPrivate &, QGraphicsLayoutItem *)
 
- Protected Functions inherited from QGraphicsLayoutItem
 QGraphicsLayoutItem (QGraphicsLayoutItemPrivate &dd)
 
void setGraphicsItem (QGraphicsItem *item)
 If the QGraphicsLayoutItem represents a QGraphicsItem, and it wants to take advantage of the automatic reparenting capabilities of QGraphicsLayout it should set this value. More...
 
void setOwnedByLayout (bool ownedByLayout)
 Sets whether a layout should delete this item in its destructor or not. More...
 
virtual QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const =0
 This pure virtual function returns the size hint for which of the QGraphicsLayoutItem, using the width or height of constraint to constrain the output. More...
 

Friends

class QGraphicsWidget
 

Additional Inherited Members

- Protected Variables inherited from QGraphicsLayoutItem
QScopedPointer< QGraphicsLayoutItemPrivated_ptr
 

Detailed Description

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

Since
4.4

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

Writing a Custom Layout

You can use 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 - QGraphicsLinearLayout or 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 QGraphicsLayout provides convenience function addChildLayoutItem(). The function will take care of automatically reparenting graphics items, if needed.

Activating the Layout

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

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

Note
A QGraphicsLayout will have the same geometry as the 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 activate() or by calling invalidate(). Calling activate() activates the layout immediately. In contrast, calling invalidate() is delayed, as it posts a LayoutRequest event to the managed widget. Due to event compression, the 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 invalidate() function is a virtual function. So, you can invalidate your own cache in a subclass of QGraphicsLayout by reimplementing this function.

Event Handling

QGraphicsLayout listens to events for the widget it manages through the virtual widgetEvent() event handler. When the layout is assigned to a widget, all events delivered to the widget are first processed by 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 QGraphicsLayout can be modified by reimplementing setContentsMargins() and getContentsMargins().

Definition at line 59 of file qgraphicslayout.h.

Constructors and Destructors

◆ QGraphicsLayout() [1/2]

QGraphicsLayout::QGraphicsLayout ( QGraphicsLayoutItem parent = 0)

Contructs a QGraphicsLayout object.

parent is passed to QGraphicsLayoutItem's constructor and the QGraphicsLayoutItem's isLayout argument is set to true.

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

Definition at line 163 of file qgraphicslayout.cpp.

165 {
166  setParentLayoutItem(parent);
167  if (parent && !parent->isLayout()) {
168  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
169  QGraphicsItem *itemParent = parent->graphicsItem();
170  if (itemParent && itemParent->isWidget()) {
171  static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
172  } else {
173  qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
174  " neither a QGraphicsWidget nor QGraphicsLayout");
175  }
176  }
178  setOwnedByLayout(true);
179 }
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
Q_CORE_EXPORT void qWarning(const char *,...)
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
void setOwnedByLayout(bool ownedByLayout)
Sets whether a layout should delete this item in its destructor or not.
QGraphicsLayoutItem(QGraphicsLayoutItem *parent=0, bool isLayout=false)
Constructs the QGraphicsLayoutItem object.
bool isWidget() const
Returns true if this item is a widget (i.
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.

◆ ~QGraphicsLayout()

QGraphicsLayout::~QGraphicsLayout ( )

Destroys the QGraphicsLayout object.

Definition at line 205 of file qgraphicslayout.cpp.

206 {
207 }

◆ QGraphicsLayout() [2/2]

QGraphicsLayout::QGraphicsLayout ( QGraphicsLayoutPrivate dd,
QGraphicsLayoutItem parent 
)
protected
Warning
This function is not part of the public interface.

Definition at line 184 of file qgraphicslayout.cpp.

185  : QGraphicsLayoutItem(dd)
186 {
187  setParentLayoutItem(parent);
188  if (parent && !parent->isLayout()) {
189  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
190  QGraphicsItem *itemParent = parent->graphicsItem();
191  if (itemParent && itemParent->isWidget()) {
192  static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this);
193  } else {
194  qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is"
195  " neither a QGraphicsWidget nor QGraphicsLayout");
196  }
197  }
199  setOwnedByLayout(true);
200 }
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
Q_CORE_EXPORT void qWarning(const char *,...)
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
void setOwnedByLayout(bool ownedByLayout)
Sets whether a layout should delete this item in its destructor or not.
QGraphicsLayoutItem(QGraphicsLayoutItem *parent=0, bool isLayout=false)
Constructs the QGraphicsLayoutItem object.
bool isWidget() const
Returns true if this item is a widget (i.
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.

Functions

◆ activate()

void QGraphicsLayout::activate ( )

Activates the layout, causing all items in the layout to be immediately rearranged.

This function is based on calling count() and itemAt(), and then calling setGeometry() on all items sequentially. When activated, the layout will adjust its geometry to its parent's 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 invalidate() before calling activate().

See also
invalidate()

Definition at line 262 of file qgraphicslayout.cpp.

Referenced by widgetEvent().

263 {
265  if (d->activated)
266  return;
267 
268  d->activateRecursive(this);
269 
270  // we don't call activate on a sublayout, but somebody might.
271  // Therefore, we walk to the parentitem of the toplevel layout.
272  QGraphicsLayoutItem *parentItem = this;
273  while (parentItem && parentItem->isLayout())
274  parentItem = parentItem->parentLayoutItem();
275  if (!parentItem)
276  return;
277  Q_ASSERT(!parentItem->isLayout());
278 
279  setGeometry(parentItem->contentsRect()); // relayout children
282  }
283 }
double d
Definition: qnumeric_p.h:62
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
QRectF contentsRect() const
Returns the contents rect in local coordinates.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void updateGeometry()
This virtual function discards any cached size hint information.
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
static bool instantInvalidatePropagation()
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.

◆ addChildLayoutItem()

void QGraphicsLayout::addChildLayoutItem ( QGraphicsLayoutItem layoutItem)
protected

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 QGraphicsWidget ancestor of the layout.

Since
4.6

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.

See also
graphicsItem()

Definition at line 476 of file qgraphicslayout.cpp.

477 {
479  d->addChildLayoutItem(layoutItem);
480 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QGraphicsLayout class provides the base class for all layouts in Graphics View.

◆ count()

int QGraphicsLayout::count ( ) const
pure virtual

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

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

See also
itemAt(), removeAt()

Implemented in QGraphicsGridLayout, QGraphicsAnchorLayout, and QGraphicsLinearLayout.

Referenced by QGraphicsLayoutPrivate::activateRecursive(), QGraphicsLinearLayout::addStretch(), QGraphicsLayoutItemPrivate::hasHeightForWidth(), QGraphicsLayoutItemPrivate::hasWidthForHeight(), removeLayoutItemFromLayout(), and QGraphicsLayoutItem::~QGraphicsLayoutItem().

◆ getContentsMargins()

void QGraphicsLayout::getContentsMargins ( qreal left,
qreal top,
qreal right,
qreal bottom 
) const
virtual

Reimplemented Function

Reimplemented from QGraphicsLayoutItem.

Definition at line 237 of file qgraphicslayout.cpp.

Referenced by QGraphicsLinearLayout::setGeometry(), QGraphicsGridLayout::setGeometry(), QGraphicsLinearLayout::sizeHint(), QGraphicsAnchorLayout::sizeHint(), and QGraphicsGridLayout::sizeHint().

238 {
239  Q_D(const QGraphicsLayout);
240  d->getMargin(left, d->left, QStyle::PM_LayoutLeftMargin);
241  d->getMargin(top, d->top, QStyle::PM_LayoutTopMargin);
242  d->getMargin(right, d->right, QStyle::PM_LayoutRightMargin);
243  d->getMargin(bottom, d->bottom, QStyle::PM_LayoutBottomMargin);
244 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)

◆ instantInvalidatePropagation()

bool QGraphicsLayout::instantInvalidatePropagation ( )
static
Since
4.8
See also
setInstantInvalidatePropagation()

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

Definition at line 519 of file qgraphicslayout.cpp.

Referenced by activate(), QGraphicsLayoutPrivate::activateRecursive(), invalidate(), QGraphicsWidget::setGeometry(), updateGeometry(), and QGraphicsWidget::updateGeometry().

520 {
522 }
static bool g_instantInvalidatePropagation

◆ invalidate()

void QGraphicsLayout::invalidate ( )
virtual

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

See also
activate(), setGeometry()

Reimplemented in QGraphicsGridLayout, QGraphicsAnchorLayout, and QGraphicsLinearLayout.

Definition at line 306 of file qgraphicslayout.cpp.

Referenced by QGraphicsLayoutPrivate::activateRecursive(), QGraphicsLinearLayout::addStretch(), QGraphicsLinearLayout::invalidate(), QGraphicsAnchorLayout::invalidate(), QGraphicsGridLayout::invalidate(), setContentsMargins(), QGraphicsWidget::setLayout(), updateGeometry(), and widgetEvent().

307 {
309  updateGeometry();
310  } else {
311  // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event.
312  QGraphicsLayoutItem *layoutItem = this;
313  while (layoutItem && layoutItem->isLayout()) {
314  // we could call updateGeometry(), but what if that method
315  // does not call the base implementation? In addition, updateGeometry()
316  // does more than we need.
317  layoutItem->d_func()->sizeHintCacheDirty = true;
318  layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
319  layoutItem = layoutItem->parentLayoutItem();
320  }
321  if (layoutItem) {
322  layoutItem->d_func()->sizeHintCacheDirty = true;
323  layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
324  }
325 
326  bool postIt = layoutItem ? !layoutItem->isLayout() : false;
327  if (postIt) {
328  layoutItem = this;
329  while (layoutItem && layoutItem->isLayout()
330  && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
331  static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false;
332  layoutItem = layoutItem->parentLayoutItem();
333  }
334  if (layoutItem && !layoutItem->isLayout()) {
335  // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
336  QApplication::postEvent(static_cast<QGraphicsWidget *>(layoutItem), new QEvent(QEvent::LayoutRequest));
337  }
338  }
339  }
340 }
virtual void updateGeometry()
Reimplemented Function
static void postEvent(QObject *receiver, QEvent *event)
Adds the event event, with the object receiver as the receiver of the event, to an event queue and re...
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
static bool instantInvalidatePropagation()
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56

◆ isActivated()

bool QGraphicsLayout::isActivated ( ) const

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 activate() function has been called, and has not yet returned).

See also
activate(), invalidate()

Definition at line 293 of file qgraphicslayout.cpp.

Referenced by widgetEvent().

294 {
295  Q_D(const QGraphicsLayout);
296  return d->activated;
297 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QGraphicsLayout class provides the base class for all layouts in Graphics View.

◆ itemAt()

QGraphicsLayoutItem * QGraphicsLayout::itemAt ( int  i) const
pure virtual

This pure virtual function must be reimplemented in a subclass of 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 count()). Together with 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.

See also
count(), removeAt()

Implemented in QGraphicsGridLayout, QGraphicsAnchorLayout, and QGraphicsLinearLayout.

Referenced by QGraphicsLayoutPrivate::activateRecursive(), QGraphicsLinearLayout::addStretch(), QGraphicsLayoutItemPrivate::hasHeightForWidth(), QGraphicsLayoutItemPrivate::hasWidthForHeight(), removeLayoutItemFromLayout(), and QGraphicsLayoutItem::~QGraphicsLayoutItem().

◆ removeAt()

void QGraphicsLayout::removeAt ( int  index)
pure virtual

This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to remove the item at index.

The reimplementation can assume that index is valid (i.e., it respects the value of count()).

The implementation must ensure that the 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.

See also
itemAt(), count()

Implemented in QGraphicsGridLayout, QGraphicsAnchorLayout, and QGraphicsLinearLayout.

Referenced by QGraphicsLinearLayout::addStretch(), removeLayoutItemFromLayout(), and QGraphicsLayoutItem::~QGraphicsLayoutItem().

◆ setContentsMargins()

void QGraphicsLayout::setContentsMargins ( qreal  left,
qreal  top,
qreal  right,
qreal  bottom 
)

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.

See also
invalidate()

Definition at line 222 of file qgraphicslayout.cpp.

223 {
225  if (d->left == left && d->top == top && d->right == right && d->bottom == bottom)
226  return;
227  d->left = left;
228  d->right = right;
229  d->top = top;
230  d->bottom = bottom;
231  invalidate();
232 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)

◆ setInstantInvalidatePropagation()

void QGraphicsLayout::setInstantInvalidatePropagation ( bool  enable)
static
Since
4.8
See also
instantInvalidatePropagation()

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 parentLayoutItem() hierarchy until it has reached the root. If the root item is a 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 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 parentLayoutItem() of the widget. It will continue in this fashion until it has reached a widget with no 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.

Definition at line 508 of file qgraphicslayout.cpp.

509 {
511 }
static bool g_instantInvalidatePropagation

◆ updateGeometry()

void QGraphicsLayout::updateGeometry ( )
virtual

Reimplemented Function

Reimplemented from QGraphicsLayoutItem.

Definition at line 345 of file qgraphicslayout.cpp.

Referenced by invalidate().

346 {
349  d->activated = false;
351 
352  QGraphicsLayoutItem *parentItem = parentLayoutItem();
353  if (!parentItem)
354  return;
355 
356  if (parentItem->isLayout())
357  static_cast<QGraphicsLayout *>(parentItem)->invalidate();
358  else
359  parentItem->updateGeometry();
360  } else {
362  if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
363  if (parentItem->isLayout()) {
364  parentItem->updateGeometry();
365  } else {
366  invalidate();
367  }
368  }
369  }
370 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
virtual void updateGeometry()
This virtual function discards any cached size hint information.
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
static bool instantInvalidatePropagation()
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.

◆ widgetEvent()

void QGraphicsLayout::widgetEvent ( QEvent e)
virtual

This virtual event handler receives all events for the managed widget.

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()

Definition at line 385 of file qgraphicslayout.cpp.

386 {
387  switch (e->type()) {
389  if (isActivated()) {
391  } else {
392  activate(); // relies on that activate() will call updateGeometry()
393  }
394  break;
396  activate();
397  break;
399  invalidate();
400  break;
401  default:
402  break;
403  }
404 }
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
QRectF contentsRect() const
Returns the contents rect in local coordinates.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
bool isActivated() const
Returns true if the layout is currently being activated; otherwise, returns false.
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent, or if the parent does not...
void activate()
Activates the layout, causing all items in the layout to be immediately rearranged.
Type type() const
Returns the event type.
Definition: qcoreevent.h:303

Friends and Related Functions

◆ QGraphicsWidget

friend class QGraphicsWidget
friend

Definition at line 88 of file qgraphicslayout.h.


The documentation for this class was generated from the following files: