Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 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 39 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 178 of file qgraphicsanchorlayout.cpp.

References d.

◆ ~QGraphicsAnchorLayout()

QGraphicsAnchorLayout::~QGraphicsAnchorLayout ( )
virtual

Destroys the QGraphicsAnchorLayout object.

Definition at line 188 of file qgraphicsanchorlayout.cpp.

References count(), d, Qt::Horizontal, i, item, Q_ASSERT, removeAt(), and Qt::Vertical.

+ Here is the call graph for this function:

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 234 of file qgraphicsanchorlayout.cpp.

References d, and invalidate().

Referenced by addAnchors().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 313 of file qgraphicsanchorlayout.cpp.

References addAnchor(), Qt::AnchorBottom, Qt::AnchorLeft, Qt::AnchorRight, Qt::AnchorTop, Qt::Horizontal, ok, and Qt::Vertical.

+ Here is the call graph for this function:

◆ 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 277 of file qgraphicsanchorlayout.cpp.

References Qt::AnchorBottom, Qt::AnchorLeft, Qt::AnchorRight, Qt::AnchorTop, d, and invalidate().

+ Here is the call graph for this function:

◆ 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 248 of file qgraphicsanchorlayout.cpp.

References d.

◆ count()

int QGraphicsAnchorLayout::count ( ) const
overridevirtual

\reimp

Implements QGraphicsLayout.

Definition at line 439 of file qgraphicsanchorlayout.cpp.

References d.

Referenced by ~QGraphicsAnchorLayout().

+ Here is the caller graph for this function:

◆ horizontalSpacing()

qreal QGraphicsAnchorLayout::horizontalSpacing ( ) const

Returns the default horizontal spacing for the anchor layout.

See also
verticalSpacing(), setHorizontalSpacing()

Definition at line 380 of file qgraphicsanchorlayout.cpp.

References d, and Qt::Horizontal.

◆ invalidate()

void QGraphicsAnchorLayout::invalidate ( )
overridevirtual

\reimp

Reimplemented from QGraphicsLayout.

Definition at line 457 of file qgraphicsanchorlayout.cpp.

References d, and QGraphicsLayout::invalidate().

Referenced by addAnchor(), addCornerAnchors(), removeAt(), setHorizontalSpacing(), setSpacing(), and setVerticalSpacing().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ itemAt()

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

\reimp

Implements QGraphicsLayout.

Definition at line 448 of file qgraphicsanchorlayout.cpp.

References d.

◆ 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 418 of file qgraphicsanchorlayout.cpp.

References d, Qt::Horizontal, invalidate(), item, and Qt::Vertical.

Referenced by ~QGraphicsAnchorLayout().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setGeometry()

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

\reimp

Reimplemented from QGraphicsLayoutItem.

Definition at line 400 of file qgraphicsanchorlayout.cpp.

References d, Qt::Horizontal, QGraphicsLayoutItem::setGeometry(), and Qt::Vertical.

+ Here is the call graph for this function:

◆ 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 335 of file qgraphicsanchorlayout.cpp.

References d, Qt::Horizontal, invalidate(), and spacing.

+ Here is the call graph for this function:

◆ 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 367 of file qgraphicsanchorlayout.cpp.

References d, invalidate(), and spacing.

+ Here is the call graph for this function:

◆ 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 348 of file qgraphicsanchorlayout.cpp.

References d, invalidate(), spacing, and Qt::Vertical.

+ Here is the call graph for this function:

◆ sizeHint()

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

\reimp

Implements QGraphicsLayoutItem.

Definition at line 468 of file qgraphicsanchorlayout.cpp.

References d, QGraphicsLayout::getContentsMargins(), Qt::Horizontal, Q_UNUSED, and Qt::Vertical.

+ Here is the call graph for this function:

◆ verticalSpacing()

qreal QGraphicsAnchorLayout::verticalSpacing ( ) const

Returns the default vertical spacing for the anchor layout.

See also
horizontalSpacing(), setVerticalSpacing()

Definition at line 391 of file qgraphicsanchorlayout.cpp.

References d, and Qt::Vertical.

Friends And Related Symbol Documentation

◆ QGraphicsAnchor

friend class QGraphicsAnchor
friend

Definition at line 76 of file qgraphicsanchorlayout.h.


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