CopperSpice API  1.9.2
QGraphicsLayoutItem Class Referenceabstract

The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts. More...

Inheritance diagram for QGraphicsLayoutItem:
QGraphicsLayout QGraphicsWidget QGraphicsAnchorLayout QGraphicsGridLayout QGraphicsLinearLayout QGraphicsProxyWidget QGraphicsWebView

Public Methods

 QGraphicsLayoutItem (QGraphicsLayoutItem *parent=nullptr, bool isLayout=false)
 
virtual ~QGraphicsLayoutItem ()
 
QRectF contentsRect () const
 
QSizeF effectiveSizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF ()) const
 
QRectF geometry () const
 
virtual void getContentsMargins (qreal *left, qreal *top, qreal *right, qreal *bottom) const
 
QGraphicsItemgraphicsItem () const
 
bool isLayout () const
 
qreal maximumHeight () const
 
QSizeF maximumSize () const
 
qreal maximumWidth () const
 
qreal minimumHeight () const
 
QSizeF minimumSize () const
 
qreal minimumWidth () const
 
bool ownedByLayout () const
 
QGraphicsLayoutItem * parentLayoutItem () const
 
qreal preferredHeight () const
 
QSizeF preferredSize () const
 
qreal preferredWidth () const
 
virtual void setGeometry (const QRectF &rect)
 
void setMaximumHeight (qreal height)
 
void setMaximumSize (const QSizeF &size)
 
void setMaximumSize (qreal w, qreal h)
 
void setMaximumWidth (qreal width)
 
void setMinimumHeight (qreal height)
 
void setMinimumSize (const QSizeF &size)
 
void setMinimumSize (qreal w, qreal h)
 
void setMinimumWidth (qreal width)
 
void setParentLayoutItem (QGraphicsLayoutItem *parent)
 
void setPreferredHeight (qreal height)
 
void setPreferredSize (const QSizeF &size)
 
void setPreferredSize (qreal w, qreal h)
 
void setPreferredWidth (qreal width)
 
void setSizePolicy (const QSizePolicy &policy)
 
void setSizePolicy (QSizePolicy::Policy hPolicy, QSizePolicy::Policy vPolicy, QSizePolicy::ControlType controlType=QSizePolicy::DefaultType)
 
QSizePolicy sizePolicy () const
 
virtual void updateGeometry ()
 

Protected Methods

void setGraphicsItem (QGraphicsItem *item)
 
void setOwnedByLayout (bool owner)
 
virtual QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF ()) const = 0
 

Friends

class QGraphicsLayout
 

Detailed Description

The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.

QGraphicsLayoutItem is an abstract class that defines a set of virtual functions describing sizes, size policies, and size hints for any object arranged by QGraphicsLayout. The API contains functions relevant for both the item itself and for the user of the item as most of QGraphicsLayoutItem's functions are also part of the subclass' public API.

In most cases, existing layout-aware classes such as QGraphicsWidget and QGraphicsLayout already provide the functionality you require. However, subclassing these classes will enable you to create both graphical elements that work well with layouts (QGraphicsWidget) or custom layouts (QGraphicsLayout).

Subclassing QGraphicsLayoutItem

If you create a subclass of QGraphicsLayoutItem and reimplement its virtual functions, you will enable the layout to resize and position your item along with other QGraphicsLayoutItems including QGraphicsWidget and QGraphicsLayout.

You can start by reimplementing important functions: the protected sizeHint() function, as well as the public setGeometry() function. If you want your items to be aware of immediate geometry changes, you can also reimplement updateGeometry().

The geometry, size hint, and size policy affect the item's size and position. Calling setGeometry() will always resize and reposition the item immediately. Normally, this function is called by QGraphicsLayout after the layout has been activated, but it can also be called by the item's user at any time.

The sizeHint() function returns the item' minimum, preferred and maximum size hints. You can override these properties by calling setMinimumSize(), setPreferredSize() or setMaximumSize(). You can also use functions such as setMinimumWidth() or setMaximumHeight() to set only the width or height component if desired.

The effectiveSizeHint() function, on the other hand, returns a size hint for any given Qt::SizeHint, and guarantees that the returned size is bound to the minimum and maximum sizes and size hints. You can set the item's vertical and horizontal size policy by calling setSizePolicy(). The sizePolicy property is used by the layout system to describe how this item prefers to grow or shrink.

Nesting QGraphicsLayoutItems

QGraphicsLayoutItems can be nested within other QGraphicsLayoutItems, similar to layouts that can contain sublayouts. This is done either by passing a QGraphicsLayoutItem pointer to QGraphicsLayoutItem's protected constructor, or by calling setParentLayoutItem(). The parentLayoutItem() function returns a pointer to the item's layoutItem parent. If the item's parent is a nullptr or if the parent does not inherit from QGraphicsItem, the parentLayoutItem() method then returns a nullptr. The methods isLayout() returns true if the QGraphicsLayoutItem subclass is itself a layout, or false otherwise.

CopperSpice uses QGraphicsLayoutItem to provide layout functionality in the Graphics View System.

See also
QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout, QGraphicsGridLayout

Constructor & Destructor Documentation

QGraphicsLayoutItem::QGraphicsLayoutItem ( QGraphicsLayoutItem *  parent = nullptr,
bool  isLayout = false 
)

Constructs a new QGraphicsLayoutItem object with the given parent. If isLayout is true the item is a layout, otherwise isLayout is false.

QGraphicsLayoutItem::~QGraphicsLayoutItem ( )
virtual

Destroys the QGraphicsLayoutItem object.

Method Documentation

QRectF QGraphicsLayoutItem::contentsRect ( ) const

Returns the contents rect in local coordinates. The contents rect defines the subrectangle used by an associated layout when arranging subitems. This method adjusts the item's geometry() by its contents margins.

See also
getContentsMargins(), geometry()
QSizeF QGraphicsLayoutItem::effectiveSizeHint ( Qt::SizeHint  which,
const QSizeF constraint = QSizeF() 
) const

Returns the effective size hint for this QGraphicsLayoutItem. The value for which is the size hint in question. constraint is an optional argument that defines a special constrain when calculating the effective size hint. By default, constraint is QSizeF(-1, -1), which means there is no constraint to the size hint.

If you want to specify the widget's size hint for a given width or height, you can provide the fixed dimension in constraint. This is useful for widgets that can grow only either vertically or horizontally, and need to set either their width or their height to a special value.

For example, a text paragraph item fit into a column width of 200 may grow vertically. You can pass QSizeF(200, -1) as a constraint to get a suitable minimum, preferred and maximum height).

You can adjust the effective size hint by reimplementing sizeHint() in a QGraphicsLayoutItem subclass, or by calling one of the following functions: setMinimumSize(), setPreferredSize, or setMaximumSize() (or a combination of both).

This method caches each of the size hints and guarantees that sizeHint() will be called only once for each value of which, unless constraint is not specified and updateGeometry() has been called.

See also
sizeHint()
QRectF QGraphicsLayoutItem::geometry ( ) const

Returns the item's geometry as a QRectF. Equivalent to QRectF(pos(), size()).

See also
setGeometry()
void QGraphicsLayoutItem::getContentsMargins ( qreal left,
qreal top,
qreal right,
qreal bottom 
) const
virtual

This virtual function provides the left, top, right and bottom contents margins for this QGraphicsLayoutItem. The default implementation assumes all contents margins are 0. If any of the pointers are a nullptr the corresponding value will not be updated.

See also
QGraphicsWidget::setContentsMargins()

Reimplemented in QGraphicsWidget::getContentsMargins(), QGraphicsLayout::getContentsMargins()

QGraphicsItem * QGraphicsLayoutItem::graphicsItem ( ) const

Returns the QGraphicsItem that this layout item represents. For QGraphicsWidget it will return itself. For custom items it can return an aggregated value.

See also
setGraphicsItem()
bool QGraphicsLayoutItem::isLayout ( ) const

Returns true if this QGraphicsLayoutItem is a layout, otherwise returns false.

See also
QGraphicsLayout
qreal QGraphicsLayoutItem::maximumHeight ( ) const
inline

Returns the maximum height.

See also
setMaximumHeight(), setMaximumSize(), maximumSize()
QSizeF QGraphicsLayoutItem::maximumSize ( ) const

Returns the maximum size.

See also
setMaximumSize(), minimumSize(), preferredSize(), sizeHint(), Qt::SizeHint
qreal QGraphicsLayoutItem::maximumWidth ( ) const
inline

Returns the maximum width.

See also
setMaximumWidth(), setMaximumSize(), maximumSize()
qreal QGraphicsLayoutItem::minimumHeight ( ) const
inline

Returns the minimum height.

See also
setMinimumHeight(), setMinimumSize(), minimumSize()
QSizeF QGraphicsLayoutItem::minimumSize ( ) const

Returns the minimum size.

See also
setMinimumSize(), preferredSize(), maximumSize(), sizeHint(), Qt::SizeHint
qreal QGraphicsLayoutItem::minimumWidth ( ) const
inline

Returns the minimum width.

See also
setMinimumWidth(), setMinimumSize(), minimumSize()
bool QGraphicsLayoutItem::ownedByLayout ( ) const

Returns whether a layout should delete this item in the destructor. If its true, then the layout will delete it. If its false, then it is assumed that another object has the ownership of it, and the layout will not delete this item.

If the item inherits both QGraphicsItem and QGraphicsLayoutItem (such as QGraphicsWidget does) the item is really part of two ownership hierarchies. This property informs what the layout should do with its child items when it is destructed. In the case of QGraphicsWidget, it is preferred that when the layout is deleted it will not delete its children (since they are also part of the graphics item hierarchy).

By default this value is initialized to false in QGraphicsLayoutItem, but it is overridden by QGraphicsLayout to return true. This is because QGraphicsLayout is not normally part of the QGraphicsItem hierarchy, so the parent layout should delete it. Subclasses might override this default behavior by calling setOwnedByLayout(true).

See also
setOwnedByLayout()
QGraphicsLayoutItem * QGraphicsLayoutItem::parentLayoutItem ( ) const

Returns the parent of this QGraphicsLayoutItem or a nullptr if there is no parent. If the parent does not inherit from QGraphicsLayoutItem the parent is treated as null.

See also
setParentLayoutItem()
qreal QGraphicsLayoutItem::preferredHeight ( ) const
inline

Returns the preferred height.

See also
setPreferredHeight(), setPreferredSize(), preferredSize()
QSizeF QGraphicsLayoutItem::preferredSize ( ) const

Returns the preferred size.

See also
setPreferredSize(), minimumSize(), maximumSize(), sizeHint(), Qt::SizeHint
qreal QGraphicsLayoutItem::preferredWidth ( ) const
inline

Returns the preferred width.

See also
setPreferredWidth(), setPreferredSize(), preferredSize()
void QGraphicsLayoutItem::setGeometry ( const QRectF rect)
virtual

Sets the geometry of the QGraphicsLayoutItem to rect, which is in coordinates relative to the parent. If rect is outside of the bounds of minimumSize and maximumSize, it will be adjusted to the closest size which is within the legal bounds.

Reimplement this method in a subclass of QGraphicsLayoutItem to receive geometry updates. The layout will call this method when it does a rearrangement.

See also
geometry()

Reimplemented in QGraphicsWidget::setGeometry(), QGraphicsAnchorLayout::setGeometry(), QGraphicsWidget::setGeometry(), QGraphicsWebView::setGeometry(), QGraphicsGridLayout::setGeometry(), QGraphicsLinearLayout::setGeometry(), QGraphicsProxyWidget::setGeometry()

void QGraphicsLayoutItem::setGraphicsItem ( QGraphicsItem item)
protected

If the QGraphicsLayoutItem represents a QGraphicsItem, and it wants to take advantage of the automatic reparenting capabilities of QGraphicsLayout it should set this value. If you delete item and do not delete the layout item, you are responsible for calling setGraphicsItem(nullptr).

See also
graphicsItem()
void QGraphicsLayoutItem::setMaximumHeight ( qreal  height)

Sets the maximum height to height.

See also
maximumHeight(), setMaximumSize(), maximumSize()
void QGraphicsLayoutItem::setMaximumSize ( const QSizeF size)

Sets the maximum size to size. This property overrides sizeHint() for Qt::MaximumSize and ensures that effectiveSizeHint() will never return a size larger than size. In order to unset the maximum size, use an invalid size.

See also
maximumSize(), minimumSize(), preferredSize(), sizeHint(), Qt::SizeHint
void QGraphicsLayoutItem::setMaximumSize ( qreal  w,
qreal  h 
)
inline
void QGraphicsLayoutItem::setMaximumWidth ( qreal  width)

Sets the maximum width to width.

See also
maximumWidth(), setMaximumSize(), maximumSize()
void QGraphicsLayoutItem::setMinimumHeight ( qreal  height)

Sets the minimum height to height.

See also
minimumHeight(), setMinimumSize(), minimumSize()
void QGraphicsLayoutItem::setMinimumSize ( const QSizeF size)

Sets the minimum size to size. This property overrides sizeHint() for Qt::MinimumSize and ensures that effectiveSizeHint() will never return a size smaller than size. In order to unset the minimum size, use an invalid size.

See also
minimumSize(), maximumSize(), preferredSize(), sizeHint(), setMinimumWidth(), setMinimumHeight(), Qt::SizeHint
void QGraphicsLayoutItem::setMinimumSize ( qreal  w,
qreal  h 
)
inline
void QGraphicsLayoutItem::setMinimumWidth ( qreal  width)

Sets the minimum width to width.

See also
minimumWidth(), setMinimumSize(), minimumSize()
void QGraphicsLayoutItem::setOwnedByLayout ( bool  owner)
protected

Sets whether a layout should delete this item in the destructor. If owner is true the layout will be responsible for deleting the item.

See also
ownedByLayout()
void QGraphicsLayoutItem::setParentLayoutItem ( QGraphicsLayoutItem *  parent)

Sets the parent of this QGraphicsLayoutItem to parent.

See also
parentLayoutItem()
void QGraphicsLayoutItem::setPreferredHeight ( qreal  height)

Sets the preferred height to height.

See also
preferredHeight(), preferredWidth(), setPreferredSize(), preferredSize()
void QGraphicsLayoutItem::setPreferredSize ( const QSizeF size)

Sets the preferred size to size. This property overrides sizeHint() for Qt::PreferredSize and provides the default value for effectiveSizeHint(). In order to unset the preferred size, use an invalid size.

See also
preferredSize(), minimumSize(), maximumSize(), sizeHint(), Qt::SizeHint
void QGraphicsLayoutItem::setPreferredSize ( qreal  w,
qreal  h 
)
inline
void QGraphicsLayoutItem::setPreferredWidth ( qreal  width)

Sets the preferred width to width.

See also
preferredWidth(), preferredHeight(), setPreferredSize(), preferredSize()
void QGraphicsLayoutItem::setSizePolicy ( const QSizePolicy policy)

Sets the size policy to policy. The size policy describes how the item should grow horizontally and vertically when arranged in a layout.

QGraphicsLayoutItem's default size policy is (QSizePolicy::Fixed, QSizePolicy::Fixed, QSizePolicy::DefaultType), but it is common for subclasses to change the default. For example, QGraphicsWidget defaults to (QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::DefaultType).

See also
sizePolicy(), QWidget::sizePolicy()
void QGraphicsLayoutItem::setSizePolicy ( QSizePolicy::Policy  hPolicy,
QSizePolicy::Policy  vPolicy,
QSizePolicy::ControlType  controlType = QSizePolicy::DefaultType 
)

Equivalent to calling setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType)).

See also
sizePolicy(), QWidget::sizePolicy()
QSizeF QGraphicsLayoutItem::sizeHint ( Qt::SizeHint  which,
const QSizeF constraint = QSizeF() 
) const
protectedpure virtual

Returns the size hint based on which using the width or height from constraint. Override this method in a subclass of QGraphicsLayoutItem to provide the necessary size hints for your items.

See also
effectiveSizeHint()

Implemented in QGraphicsWidget::sizeHint(), QGraphicsAnchorLayout::sizeHint(), QGraphicsProxyWidget::sizeHint(), QGraphicsGridLayout::sizeHint(), QGraphicsLinearLayout::sizeHint(), QGraphicsWebView::sizeHint()

QSizePolicy QGraphicsLayoutItem::sizePolicy ( ) const

Returns the current size policy.

See also
setSizePolicy(), QWidget::sizePolicy()
void QGraphicsLayoutItem::updateGeometry ( )
virtual

Discards any cached size hint information. Call this method when changing the return value from sizeHint(). Subclasses must call the base implementation when overriding this method.

See also
effectiveSizeHint()

Reimplemented in QGraphicsWidget::updateGeometry(), QGraphicsWebView::updateGeometry(), QGraphicsLayout::updateGeometry()