Qt 4.6: QGraphicsAnchorLayout Class Reference

|  | Home · All Classes · Overviews |

# QGraphicsAnchorLayout Class Reference
[QtGui module]

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

     #include <QGraphicsAnchorLayout>

Inherits QGraphicsLayout.

This class was introduced in Qt 4.6.

- List of all members, including inherited members

## Public Functions

| QGraphicsAnchorLayout ( QGraphicsLayoutItem * parent = 0 )
virtual | ~QGraphicsAnchorLayout ()
void | addAllAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )
void | addAnchor ( QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge )
void | addCornerAnchors ( QGraphicsLayoutItem * firstItem, Qt::Corner firstCorner, QGraphicsLayoutItem * secondItem, Qt::Corner secondCorner )
void | addLeftAndRightAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )
void | addTopAndBottomAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )
qreal | anchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge ) const
qreal | horizontalSpacing () const
void | removeAnchor ( QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge )
void | setAnchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge, qreal spacing )
void | setHorizontalSpacing ( qreal spacing )
void | setSpacing ( qreal spacing )
void | setVerticalSpacing ( qreal spacing )
void | unsetAnchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge )
qreal | verticalSpacing () const

## Reimplemented Public Functions

virtual int | count () const
virtual void | invalidate ()
virtual QGraphicsLayoutItem * | itemAt ( int index ) const
virtual void | removeAt ( int index )
virtual void | setGeometry ( const QRectF & geom )

- 10 public functions inherited from QGraphicsLayout
- 35 public functions inherited from QGraphicsLayoutItem

## Reimplemented Protected Functions

virtual QSizeF | sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const

- 3 protected functions inherited from QGraphicsLayoutItem

## Detailed Description

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

The anchor layout is a layout where one can specify how widgets should be placed relative to each other. The specification is called an anchor, and it is set up by calling anchor(). Anchors are always set up between edges of an item, where the "center" is also considered to be an edge. Considering this example:

     QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
     QGraphicsWidget *a = new QGraphicsWidget;
     QGraphicsWidget *b = new QGraphicsWidget;
     l->anchor(a, Qt::AnchorRight, b, Qt::AnchorLeft);

Here is the right edge of item A anchored to the left edge of item B, with the result that item B will be placed to the right of item A, with a spacing between A and B. If the spacing is negative, the items will overlap to some extent. Items that are anchored are automatically added to the layout, and if items are removed, all their anchors will be automatically removed

### Size Hints and Size Policies in QGraphicsAnchorLayout

QGraphicsAnchorLayout respects each item's size hints and size policies. However it does not respect stretch factors currently. This might change in the future, so please refrain from using stretch factors in anchor layout to avoid any future regressions.

### Spacing within QGraphicsAnchorLayout

Between the items, the layout can distribute some space. If the spacing has not been explicitly specified, the actual amount of space will usually be 0, but if the first edge is the "opposite" of the second edge (i.e. Right is anchored to Left or vice-versa), the size of the anchor will be queried from the style through the pixelMetric PM_LayoutHorizontalSpacing (or PM_LayoutVerticalSpacing for vertical anchors).

## Member Function Documentation

### QGraphicsAnchorLayout::QGraphicsAnchorLayout ( QGraphicsLayoutItem * parent = 0 )

Constructs a QGraphicsAnchorLayout instance. parent is passed to QGraphicsLayout's constructor.

### QGraphicsAnchorLayout::~QGraphicsAnchorLayout () `[virtual]`

Destroys the QGraphicsAnchorLayout object.

### void QGraphicsAnchorLayout::addAllAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )

Anchors all edges (left, right, top and bottom) of firstItem to the same edges of secondItem.

This convenience function is equivalent to calling

     l->addLeftAndRightAnchors(firstItem, secondItem);
     l->addTopAndBottomAnchors(firstItem, secondItem);

### void 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 magnitude 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 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 with 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 setAnchorSpacing() method.

See also removeAnchor(), addCornerAnchors(), addLeftAndRightAnchors(), addTopAndBottomAnchors(), and addAllAnchors().

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

Creates two anchors between firstItem and secondItem, where one is for the horizontal edge and another one for the vertical edge that the corners firstCorner and secondCorner specifies. The magnitude of the anchors is picked up from the style.

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

     layout->addAnchor(layout, Qt::AnchorTop, b, Qt::AnchorTop);
     layout->addAnchor(layout, Qt::AnchorLeft, b, Qt::AnchorLeft);

has the same effect as

     layout->addCornerAnchors(layout, Qt::TopLeft, b, Qt::TopLeft);

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 with up to 2.

### void QGraphicsAnchorLayout::addLeftAndRightAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )

Anchors the left and right edges of firstItem to the same edges of secondItem.

This convenience function is equivalent to calling

     l->addAnchor(firstItem, Qt::AnchorLeft, secondItem, Qt::AnchorLeft);
     l->addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight);

### void QGraphicsAnchorLayout::addTopAndBottomAnchors ( QGraphicsLayoutItem * firstItem, QGraphicsLayoutItem * secondItem )

Anchors the top and bottom edges of firstItem to the same edges of secondItem.

This convenience function is equivalent to calling

     l->addAnchor(firstItem, Qt::AnchorTop, secondItem, Qt::AnchorTop);
     l->addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom);

### qreal QGraphicsAnchorLayout::anchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge ) const

Returns the spacing between the anchor point defined by firstItem and firstEdge and secondItem and secondEdge. The anchor must exist.

See also setAnchorSpacing().

### int QGraphicsAnchorLayout::count () const `[virtual]`

Reimplemented from QGraphicsLayout::count().

### qreal QGraphicsAnchorLayout::horizontalSpacing () const

Returns the default horizontal spacing for the anchor layout.

See also verticalSpacing() and setHorizontalSpacing().

### void QGraphicsAnchorLayout::invalidate () `[virtual]`

Reimplemented from QGraphicsLayout::invalidate().

### QGraphicsLayoutItem * QGraphicsAnchorLayout::itemAt ( int index ) const `[virtual]`

Reimplemented from QGraphicsLayout::itemAt().

### void QGraphicsAnchorLayout::removeAnchor ( QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge )

Removes the anchor between the edge firstEdge of item firstItem and the edge secondEdge of item secondItem. If such an anchor does not exist, the layout will be left unchanged.

### void QGraphicsAnchorLayout::removeAt ( int index ) `[virtual]`

Reimplemented from QGraphicsLayout::removeAt().

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() and count().

### void QGraphicsAnchorLayout::setAnchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge, qreal spacing )

Set the spacing between the anchor point defined by firstItem and firstEdge and secondItem and secondEdge to be spacing.

See also anchorSpacing().

### void QGraphicsAnchorLayout::setGeometry ( const QRectF & geom ) `[virtual]`

Reimplemented from QGraphicsLayoutItem::setGeometry().

### void QGraphicsAnchorLayout::setHorizontalSpacing ( qreal spacing )

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

See also horizontalSpacing(), setVerticalSpacing(), and 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.

See also setHorizontalSpacing() and setVerticalSpacing().

### void QGraphicsAnchorLayout::setVerticalSpacing ( qreal spacing )

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

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

### QSizeF QGraphicsAnchorLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const `[virtual protected]`

Reimplemented from QGraphicsLayoutItem::sizeHint().

### void QGraphicsAnchorLayout::unsetAnchorSpacing ( const QGraphicsLayoutItem * firstItem, Qt::AnchorPoint firstEdge, const QGraphicsLayoutItem * secondItem, Qt::AnchorPoint secondEdge )

Resets the spacing between the anchor point defined by firstItem and firstEdge and secondItem and secondEdge to be the default spacing. Depending on the anchor type, the default spacing is either 0 or a value returned from the style.

See also setAnchorSpacing(), anchorSpacing(), and addAnchor().

### qreal QGraphicsAnchorLayout::verticalSpacing () const

Returns the default vertical spacing for the anchor layout.

See also horizontalSpacing() and setVerticalSpacing().