Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QGraphicsAnchorLayout Class Reference

The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View. More...

#include <qgraphicsanchorlayout.h>

Inheritance diagram for QGraphicsAnchorLayout:
Collaboration diagram for QGraphicsAnchorLayout:

Public Member Functions

 QGraphicsAnchorLayout (QGraphicsLayoutItem *parent=nullptr)
 Constructs a QGraphicsAnchorLayout instance.
virtual ~QGraphicsAnchorLayout ()
 Destroys the QGraphicsAnchorLayout object.
QGraphicsAnchoraddAnchor (QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
 Creates an anchor between the edge firstEdge of item firstItem and the edge secondEdge of item secondItem.
QGraphicsAnchoranchor (QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
 Returns the anchor between the anchor points defined by firstItem and firstEdge and secondItem and secondEdge.
void addCornerAnchors (QGraphicsLayoutItem *firstItem, Qt::Corner firstCorner, QGraphicsLayoutItem *secondItem, Qt::Corner secondCorner)
 Creates two anchors between firstItem and secondItem specified by the corners, firstCorner and secondCorner, where one is for the horizontal edge and another one for the vertical edge.
void addAnchors (QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem, Qt::Orientations orientations=Qt::Horizontal|Qt::Vertical)
 Anchors two or four edges of firstItem with the corresponding edges of secondItem, so that firstItem has the same size as secondItem in the dimensions specified by orientations.
void setHorizontalSpacing (qreal spacing)
 Sets the default horizontal spacing for the anchor layout to spacing.
void setVerticalSpacing (qreal spacing)
 Sets the default vertical spacing for the anchor layout to spacing.
void setSpacing (qreal spacing)
 Sets the default horizontal and the default vertical spacing for the anchor layout to spacing.
qreal horizontalSpacing () const
 Returns the default horizontal spacing for the anchor layout.
qreal verticalSpacing () const
 Returns the default vertical spacing for the anchor layout.
void removeAt (int index) override
 Removes the layout item at index without destroying it.
void setGeometry (const QRectF &rect) override
 \reimp
int count () const override
 \reimp
QGraphicsLayoutItemitemAt (int index) const override
 \reimp
void invalidate () override
 \reimp
Public Member Functions inherited from QGraphicsLayout
 QGraphicsLayout (QGraphicsLayoutItem *parent=nullptr)
 Constructs a QGraphicsLayout object.
 ~QGraphicsLayout ()
 Destroys the QGraphicsLayout object.
void setContentsMargins (qreal left, qreal top, qreal right, qreal bottom)
 Sets the contents margins to left, top, right and bottom.
void getContentsMargins (qreal *left, qreal *top, qreal *right, qreal *bottom) const override
 \reimp
void activate ()
 Activates the layout, causing all items in the layout to be immediately rearranged.
bool isActivated () const
 Returns true if the layout is currently being activated; otherwise, returns false.
virtual void updateGeometry () override
 \reimp
virtual void widgetEvent (QEvent *e)
 This virtual event handler receives all events for the managed widget.
Public Member Functions inherited from QGraphicsLayoutItem
 QGraphicsLayoutItem (QGraphicsLayoutItem *parent=nullptr, bool isLayout=false)
 Constructs the QGraphicsLayoutItem object.
virtual ~QGraphicsLayoutItem ()
 Destroys the QGraphicsLayoutItem object.
void setSizePolicy (const QSizePolicy &policy)
 Sets the size policy to policy.
void setSizePolicy (QSizePolicy::Policy hPolicy, QSizePolicy::Policy vPolicy, QSizePolicy::ControlType controlType=QSizePolicy::DefaultType)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This function is equivalent to calling setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType)).
QSizePolicy sizePolicy () const
 Returns the current size policy.
void setMinimumSize (const QSizeF &size)
 Sets the minimum size to size.
void setMinimumSize (qreal w, qreal h)
 This convenience function is equivalent to calling setMinimumSize(QSizeF(w, h)).
QSizeF minimumSize () const
 Returns the minimum size.
void setMinimumWidth (qreal width)
 Sets the minimum width to width.
qreal minimumWidth () const
 Returns the minimum width.
void setMinimumHeight (qreal height)
 Sets the minimum height to height.
qreal minimumHeight () const
 Returns the minimum height.
void setPreferredSize (const QSizeF &size)
 Sets the preferred size to size.
void setPreferredSize (qreal w, qreal h)
 This convenience function is equivalent to calling setPreferredSize(QSizeF(w, h)).
QSizeF preferredSize () const
 Returns the preferred size.
void setPreferredWidth (qreal width)
 Sets the preferred width to width.
qreal preferredWidth () const
 Returns the preferred width.
void setPreferredHeight (qreal height)
 Sets the preferred height to height.
qreal preferredHeight () const
 Returns the preferred height.
void setMaximumSize (const QSizeF &size)
 Sets the maximum size to size.
void setMaximumSize (qreal w, qreal h)
 This convenience function is equivalent to calling setMaximumSize(QSizeF(w, h)).
QSizeF maximumSize () const
 Returns the maximum size.
void setMaximumWidth (qreal width)
 Sets the maximum width to width.
qreal maximumWidth () const
 Returns the maximum width.
void setMaximumHeight (qreal height)
 Sets the maximum height to height.
qreal maximumHeight () const
 Returns the maximum height.
QRectF geometry () const
 Returns the item's geometry (e.g., position and size) as a QRectF.
QRectF contentsRect () const
 Returns the contents rect in local coordinates.
QSizeF effectiveSizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
 Returns the effective size hint for this QGraphicsLayoutItem.
virtual bool isEmpty () const
QGraphicsLayoutItemparentLayoutItem () const
 Returns the parent of this QGraphicsLayoutItem, or \nullptr 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).
void setParentLayoutItem (QGraphicsLayoutItem *parent)
 Sets the parent of this QGraphicsLayoutItem to parent.
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.
QGraphicsItemgraphicsItem () const
 Returns the QGraphicsItem that this layout item represents.
bool ownedByLayout () const

Protected Member Functions

QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
 \reimp
Protected Member Functions inherited from QGraphicsLayout
 QGraphicsLayout (QGraphicsLayoutPrivate &, QGraphicsLayoutItem *)
void addChildLayoutItem (QGraphicsLayoutItem *layoutItem)
Protected Member Functions inherited from QGraphicsLayoutItem
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.
void setOwnedByLayout (bool ownedByLayout)
 QGraphicsLayoutItem (QGraphicsLayoutItemPrivate &dd)

Friends

class QGraphicsAnchor

Additional Inherited Members

Static Public Member Functions inherited from QGraphicsLayout
static void setInstantInvalidatePropagation (bool enable)
static bool instantInvalidatePropagation ()
Protected Attributes inherited from QGraphicsLayoutItem
QScopedPointer< QGraphicsLayoutItemPrivated_ptr

Detailed Description

The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View.

Since
4.6

\inmodule QtWidgets

The anchor layout allows developers to specify how widgets should be placed relative to each other, and to the layout itself. The specification is made by adding anchors to the layout by calling addAnchor(), addAnchors() or addCornerAnchors().

Existing anchors in the layout can be accessed with the anchor() function. Items that are anchored are automatically added to the layout, and if items are removed, all their anchors will be automatically removed.

\div {class="float-left"} \inlineimage simpleanchorlayout-example.png {Three widgets using anchor layout to align to the widget edges} \caption Using an anchor layout to align simple colored widgets. \enddiv

Anchors are always set up between edges of an item, where the "center" is also considered to be an edge. Consider the following example:

Here, the right edge of item a is anchored to the left edge of item b and the bottom edge of item a is anchored to the top edge of item b, with the result that item b will be placed diagonally to the right and below item b.

The addCornerAnchors() function provides a simpler way of anchoring the corners of two widgets than the two individual calls to addAnchor() shown in the code above. Here, we see how a widget can be anchored to the top-left corner of the enclosing layout:

In cases where anchors are used to match the widths or heights of widgets, it is convenient to use the addAnchors() function. As with the other functions for specifying anchors, it can also be used to anchor a widget to a layout.

Definition at line 40 of file qgraphicsanchorlayout.h.

Constructor & Destructor Documentation

◆ QGraphicsAnchorLayout()

QGraphicsAnchorLayout::QGraphicsAnchorLayout ( QGraphicsLayoutItem * parent = nullptr)

Constructs a QGraphicsAnchorLayout instance.

parent is passed to QGraphicsLayout's constructor.

Definition at line 181 of file qgraphicsanchorlayout.cpp.

◆ ~QGraphicsAnchorLayout()

QGraphicsAnchorLayout::~QGraphicsAnchorLayout ( )
virtual

Destroys the QGraphicsAnchorLayout object.

Definition at line 191 of file qgraphicsanchorlayout.cpp.

Member Function Documentation

◆ addAnchor()

QGraphicsAnchor * QGraphicsAnchorLayout::addAnchor ( QGraphicsLayoutItem * firstItem,
Qt::AnchorPoint firstEdge,
QGraphicsLayoutItem * secondItem,
Qt::AnchorPoint secondEdge )

Creates an anchor between the edge firstEdge of item firstItem and the edge secondEdge of item secondItem.

The spacing of the anchor is picked up from the style. Anchors between a layout edge and an item edge will have a size of 0. If there is already an anchor between the edges, the new anchor will replace the old one.

firstItem and secondItem are automatically added to the layout if they are not part of the layout. This means that count() can increase by up to 2.

The spacing an anchor will get depends on the type of anchor. For instance, anchors from the Right edge of one item to the Left edge of another (or vice versa) will use the default horizontal spacing. The same behaviour applies to Bottom to Top anchors, (but they will use the default vertical spacing). For all other anchor combinations, the spacing will be 0. All anchoring functions will follow this rule.

The spacing can also be set manually by using QGraphicsAnchor::setSpacing() method.

Calling this function where firstItem or secondItem are ancestors of the layout have undefined behaviour.

See also
addAnchors(), addCornerAnchors()

Definition at line 237 of file qgraphicsanchorlayout.cpp.

◆ addAnchors()

void QGraphicsAnchorLayout::addAnchors ( QGraphicsLayoutItem * firstItem,
QGraphicsLayoutItem * secondItem,
Qt::Orientations orientations = Qt::Horizontal | Qt::Vertical )

Anchors two or four edges of firstItem with the corresponding edges of secondItem, so that firstItem has the same size as secondItem in the dimensions specified by orientations.

For example, the following example anchors the left and right edges of two items to match their widths:

This can also be achieved using the following line of code:

See also
addAnchor(), addCornerAnchors()

Definition at line 316 of file qgraphicsanchorlayout.cpp.

◆ addCornerAnchors()

void QGraphicsAnchorLayout::addCornerAnchors ( QGraphicsLayoutItem * firstItem,
Qt::Corner firstCorner,
QGraphicsLayoutItem * secondItem,
Qt::Corner secondCorner )

Creates two anchors between firstItem and secondItem specified by the corners, firstCorner and secondCorner, where one is for the horizontal edge and another one for the vertical edge.

This is a convenience function, since anchoring corners can be expressed as anchoring two edges. For instance:

This can also be achieved with the following line of code:

If there is already an anchor between the edge pairs, it will be replaced by the anchors that this function specifies.

firstItem and secondItem are automatically added to the layout if they are not part of the layout. This means that count() can increase by up to 2.

See also
addAnchor(), addAnchors()

Definition at line 280 of file qgraphicsanchorlayout.cpp.

◆ anchor()

QGraphicsAnchor * QGraphicsAnchorLayout::anchor ( QGraphicsLayoutItem * firstItem,
Qt::AnchorPoint firstEdge,
QGraphicsLayoutItem * secondItem,
Qt::AnchorPoint secondEdge )

Returns the anchor between the anchor points defined by firstItem and firstEdge and secondItem and secondEdge.

If there is no such anchor, the function will return 0.

Definition at line 251 of file qgraphicsanchorlayout.cpp.

◆ count()

int QGraphicsAnchorLayout::count ( ) const
overridevirtual

\reimp

Implements QGraphicsLayout.

Definition at line 442 of file qgraphicsanchorlayout.cpp.

◆ horizontalSpacing()

qreal QGraphicsAnchorLayout::horizontalSpacing ( ) const

Returns the default horizontal spacing for the anchor layout.

See also
verticalSpacing(), setHorizontalSpacing()

Definition at line 383 of file qgraphicsanchorlayout.cpp.

◆ invalidate()

void QGraphicsAnchorLayout::invalidate ( )
overridevirtual

\reimp

Reimplemented from QGraphicsLayout.

Definition at line 460 of file qgraphicsanchorlayout.cpp.

◆ itemAt()

QGraphicsLayoutItem * QGraphicsAnchorLayout::itemAt ( int index) const
overridevirtual

\reimp

Implements QGraphicsLayout.

Definition at line 451 of file qgraphicsanchorlayout.cpp.

◆ removeAt()

void QGraphicsAnchorLayout::removeAt ( int index)
overridevirtual

Removes the layout item at index without destroying it.

Ownership of the item is transferred to the caller.

Removing an item will also remove any of the anchors associated with it.

See also
itemAt(), count()

Implements QGraphicsLayout.

Definition at line 421 of file qgraphicsanchorlayout.cpp.

◆ setGeometry()

void QGraphicsAnchorLayout::setGeometry ( const QRectF & rect)
overridevirtual

\reimp

Reimplemented from QGraphicsLayoutItem.

Definition at line 403 of file qgraphicsanchorlayout.cpp.

◆ setHorizontalSpacing()

void QGraphicsAnchorLayout::setHorizontalSpacing ( qreal spacing)

Sets the default horizontal spacing for the anchor layout to spacing.

See also
horizontalSpacing(), setVerticalSpacing(), setSpacing()

Definition at line 338 of file qgraphicsanchorlayout.cpp.

◆ setSpacing()

void QGraphicsAnchorLayout::setSpacing ( qreal spacing)

Sets the default horizontal and the default vertical spacing for the anchor layout to spacing.

If an item is anchored with no spacing associated with the anchor, it will use the default spacing.

QGraphicsAnchorLayout does not support negative spacings. Setting a negative value will unset the previous spacing and make the layout use the spacing provided by the current widget style.

See also
setHorizontalSpacing(), setVerticalSpacing()

Definition at line 370 of file qgraphicsanchorlayout.cpp.

◆ setVerticalSpacing()

void QGraphicsAnchorLayout::setVerticalSpacing ( qreal spacing)

Sets the default vertical spacing for the anchor layout to spacing.

See also
verticalSpacing(), setHorizontalSpacing(), setSpacing()

Definition at line 351 of file qgraphicsanchorlayout.cpp.

◆ sizeHint()

QSizeF QGraphicsAnchorLayout::sizeHint ( Qt::SizeHint which,
const QSizeF & constraint = QSizeF() ) const
overrideprotectedvirtual

\reimp

Implements QGraphicsLayoutItem.

Definition at line 471 of file qgraphicsanchorlayout.cpp.

◆ verticalSpacing()

qreal QGraphicsAnchorLayout::verticalSpacing ( ) const

Returns the default vertical spacing for the anchor layout.

See also
horizontalSpacing(), setVerticalSpacing()

Definition at line 394 of file qgraphicsanchorlayout.cpp.

◆ QGraphicsAnchor

friend class QGraphicsAnchor
friend

Definition at line 77 of file qgraphicsanchorlayout.h.


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