![]() |
Qt 6.x
The Qt SDK
|
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View. More...
#include <qgraphicsanchorlayout.h>
Public Member Functions | |
QGraphicsAnchorLayout (QGraphicsLayoutItem *parent=nullptr) | |
Constructs a QGraphicsAnchorLayout instance. | |
virtual | ~QGraphicsAnchorLayout () |
Destroys the QGraphicsAnchorLayout object. | |
QGraphicsAnchor * | 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. | |
QGraphicsAnchor * | 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. | |
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 | |
QGraphicsLayoutItem * | itemAt (int index) const override |
\reimp | |
void | invalidate () override |
\reimp | |
![]() | |
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 | invalidate () |
Clears any cached geometry and size hint information in the layout, and posts a \l{QEvent::LayoutRequest}{LayoutRequest} event to the managed parent QGraphicsLayoutItem. | |
virtual void | updateGeometry () override |
\reimp | |
virtual void | widgetEvent (QEvent *e) |
This virtual event handler receives all events for the managed widget. | |
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. | |
virtual QGraphicsLayoutItem * | itemAt (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. | |
virtual void | removeAt (int index)=0 |
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to remove the item at index. | |
![]() | |
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. | |
virtual void | setGeometry (const QRectF &rect) |
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordinates (e.g., the top-left corner of rect is equivalent to the item's position in parent coordinates). | |
QRectF | geometry () const |
Returns the item's geometry (e.g., position and size) as a QRectF. | |
virtual void | getContentsMargins (qreal *left, qreal *top, qreal *right, qreal *bottom) const |
This virtual function provides the left, top, right and bottom contents margins for this QGraphicsLayoutItem. | |
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 void | updateGeometry () |
This virtual function discards any cached size hint information. | |
virtual bool | isEmpty () const |
QGraphicsLayoutItem * | parentLayoutItem () 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 . | |
QGraphicsItem * | graphicsItem () 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 | |
![]() | |
QGraphicsLayout (QGraphicsLayoutPrivate &, QGraphicsLayoutItem *) | |
void | addChildLayoutItem (QGraphicsLayoutItem *layoutItem) |
![]() | |
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) | |
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. | |
Friends | |
class | QGraphicsAnchor |
Additional Inherited Members | |
![]() | |
static void | setInstantInvalidatePropagation (bool enable) |
static bool | instantInvalidatePropagation () |
![]() | |
QScopedPointer< QGraphicsLayoutItemPrivate > | d_ptr |
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View.
\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.
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.
|
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.
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.
Definition at line 234 of file qgraphicsanchorlayout.cpp.
References d, and invalidate().
Referenced by 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:
Definition at line 313 of file qgraphicsanchorlayout.cpp.
References addAnchor(), Qt::AnchorBottom, Qt::AnchorLeft, Qt::AnchorRight, Qt::AnchorTop, Qt::Horizontal, ok, and Qt::Vertical.
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.
Definition at line 277 of file qgraphicsanchorlayout.cpp.
References Qt::AnchorBottom, Qt::AnchorLeft, Qt::AnchorRight, Qt::AnchorTop, d, and invalidate().
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.
|
overridevirtual |
\reimp
Implements QGraphicsLayout.
Definition at line 439 of file qgraphicsanchorlayout.cpp.
References d.
Referenced by ~QGraphicsAnchorLayout().
qreal QGraphicsAnchorLayout::horizontalSpacing | ( | ) | const |
Returns the default horizontal spacing for the anchor layout.
Definition at line 380 of file qgraphicsanchorlayout.cpp.
References d, and Qt::Horizontal.
|
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().
|
overridevirtual |
\reimp
Implements QGraphicsLayout.
Definition at line 448 of file qgraphicsanchorlayout.cpp.
References d.
|
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.
Implements QGraphicsLayout.
Definition at line 418 of file qgraphicsanchorlayout.cpp.
References d, Qt::Horizontal, invalidate(), item, and Qt::Vertical.
Referenced by ~QGraphicsAnchorLayout().
\reimp
Reimplemented from QGraphicsLayoutItem.
Definition at line 400 of file qgraphicsanchorlayout.cpp.
References d, Qt::Horizontal, QGraphicsLayoutItem::setGeometry(), and Qt::Vertical.
Sets the default horizontal spacing for the anchor layout to spacing.
Definition at line 335 of file qgraphicsanchorlayout.cpp.
References d, Qt::Horizontal, invalidate(), and 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.
Definition at line 367 of file qgraphicsanchorlayout.cpp.
References d, invalidate(), and spacing.
Sets the default vertical spacing for the anchor layout to spacing.
Definition at line 348 of file qgraphicsanchorlayout.cpp.
References d, invalidate(), spacing, and Qt::Vertical.
|
overrideprotectedvirtual |
\reimp
Implements QGraphicsLayoutItem.
Definition at line 468 of file qgraphicsanchorlayout.cpp.
References d, QGraphicsLayout::getContentsMargins(), Qt::Horizontal, Q_UNUSED, and Qt::Vertical.
qreal QGraphicsAnchorLayout::verticalSpacing | ( | ) | const |
Returns the default vertical spacing for the anchor layout.
Definition at line 391 of file qgraphicsanchorlayout.cpp.
References d, and Qt::Vertical.
|
friend |
Definition at line 76 of file qgraphicsanchorlayout.h.