Qt 4.8
|
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View. More...
#include <qgraphicsanchorlayout.h>
Public Functions | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
int | count () const |
Reimplemented Function More... | |
qreal | horizontalSpacing () const |
Returns the default horizontal spacing for the anchor layout. More... | |
void | invalidate () |
Reimplemented Function More... | |
QGraphicsLayoutItem * | itemAt (int index) const |
Reimplemented Function More... | |
QGraphicsAnchorLayout (QGraphicsLayoutItem *parent=0) | |
Constructs a QGraphicsAnchorLayout instance. More... | |
void | removeAt (int index) |
Removes the layout item at index without destroying it. More... | |
void | setGeometry (const QRectF &rect) |
Reimplemented Function More... | |
void | setHorizontalSpacing (qreal spacing) |
Sets the default horizontal spacing for the anchor layout to spacing. More... | |
void | setSpacing (qreal spacing) |
Sets the default horizontal and the default vertical spacing for the anchor layout to spacing. More... | |
void | setVerticalSpacing (qreal spacing) |
Sets the default vertical spacing for the anchor layout to spacing. More... | |
qreal | verticalSpacing () const |
Returns the default vertical spacing for the anchor layout. More... | |
virtual | ~QGraphicsAnchorLayout () |
Destroys the QGraphicsAnchorLayout object. More... | |
Public Functions inherited from QGraphicsLayout | |
void | activate () |
Activates the layout, causing all items in the layout to be immediately rearranged. More... | |
void | getContentsMargins (qreal *left, qreal *top, qreal *right, qreal *bottom) const |
Reimplemented Function More... | |
bool | isActivated () const |
Returns true if the layout is currently being activated; otherwise, returns false. More... | |
QGraphicsLayout (QGraphicsLayoutItem *parent=0) | |
Contructs a QGraphicsLayout object. More... | |
void | setContentsMargins (qreal left, qreal top, qreal right, qreal bottom) |
Sets the contents margins to left, top, right and bottom. More... | |
virtual void | updateGeometry () |
Reimplemented Function More... | |
virtual void | widgetEvent (QEvent *e) |
This virtual event handler receives all events for the managed widget. More... | |
~QGraphicsLayout () | |
Destroys the QGraphicsLayout object. More... | |
Public Functions inherited from QGraphicsLayoutItem | |
QRectF | contentsRect () const |
Returns the contents rect in local coordinates. More... | |
QSizeF | effectiveSizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const |
Returns the effective size hint for this QGraphicsLayoutItem. More... | |
QRectF | geometry () const |
Returns the item's geometry (e. More... | |
QGraphicsItem * | graphicsItem () const |
Returns the QGraphicsItem that this layout item represents. More... | |
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. More... | |
qreal | maximumHeight () const |
Returns the maximum height. More... | |
QSizeF | maximumSize () const |
Returns the maximum size. More... | |
qreal | maximumWidth () const |
Returns the maximum width. More... | |
qreal | minimumHeight () const |
Returns the minimum height. More... | |
QSizeF | minimumSize () const |
Returns the minimum size. More... | |
qreal | minimumWidth () const |
Returns the minimum width. More... | |
bool | ownedByLayout () const |
Returns whether a layout should delete this item in its destructor. More... | |
QGraphicsLayoutItem * | parentLayoutItem () const |
Returns the parent of this QGraphicsLayoutItem, or 0 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). More... | |
qreal | preferredHeight () const |
Returns the preferred height. More... | |
QSizeF | preferredSize () const |
Returns the preferred size. More... | |
qreal | preferredWidth () const |
Returns the preferred width. More... | |
QGraphicsLayoutItem (QGraphicsLayoutItem *parent=0, bool isLayout=false) | |
Constructs the QGraphicsLayoutItem object. More... | |
void | setMaximumHeight (qreal height) |
Sets the maximum height to height. More... | |
void | setMaximumSize (const QSizeF &size) |
Sets the maximum size to size. More... | |
void | setMaximumSize (qreal w, qreal h) |
This convenience function is equivalent to calling setMaximumSize(QSizeF(w, h)). More... | |
void | setMaximumWidth (qreal width) |
Sets the maximum width to width. More... | |
void | setMinimumHeight (qreal height) |
Sets the minimum height to height. More... | |
void | setMinimumSize (const QSizeF &size) |
Sets the minimum size to size. More... | |
void | setMinimumSize (qreal w, qreal h) |
This convenience function is equivalent to calling setMinimumSize(QSizeF(w, h)). More... | |
void | setMinimumWidth (qreal width) |
Sets the minimum width to width. More... | |
void | setParentLayoutItem (QGraphicsLayoutItem *parent) |
Sets the parent of this QGraphicsLayoutItem to parent. More... | |
void | setPreferredHeight (qreal height) |
Sets the preferred height to height. More... | |
void | setPreferredSize (const QSizeF &size) |
Sets the preferred size to size. More... | |
void | setPreferredSize (qreal w, qreal h) |
This convenience function is equivalent to calling setPreferredSize(QSizeF(w, h)). More... | |
void | setPreferredWidth (qreal width) |
Sets the preferred width to width. More... | |
void | setSizePolicy (const QSizePolicy &policy) |
Sets the size policy to policy. More... | |
void | setSizePolicy (QSizePolicy::Policy hPolicy, QSizePolicy::Policy vPolicy, QSizePolicy::ControlType controlType=QSizePolicy::DefaultType) |
This function is equivalent to calling setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType)). More... | |
QSizePolicy | sizePolicy () const |
Returns the current size policy. More... | |
virtual | ~QGraphicsLayoutItem () |
Destroys the QGraphicsLayoutItem object. More... | |
Protected Functions | |
QSizeF | sizeHint (Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const |
Reimplemented Function More... | |
Protected Functions inherited from QGraphicsLayout | |
void | addChildLayoutItem (QGraphicsLayoutItem *layoutItem) |
This function is a convenience function provided for custom layouts, and will go through all items in the layout and reparent their graphics items to the closest QGraphicsWidget ancestor of the layout. More... | |
QGraphicsLayout (QGraphicsLayoutPrivate &, QGraphicsLayoutItem *) | |
Protected Functions inherited from QGraphicsLayoutItem | |
QGraphicsLayoutItem (QGraphicsLayoutItemPrivate &dd) | |
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. More... | |
void | setOwnedByLayout (bool ownedByLayout) |
Sets whether a layout should delete this item in its destructor or not. More... | |
Friends | |
class | QGraphicsAnchor |
Additional Inherited Members | |
Static Public Functions inherited from QGraphicsLayout | |
static bool | instantInvalidatePropagation () |
static void | setInstantInvalidatePropagation (bool enable) |
Protected Variables inherited from QGraphicsLayoutItem | |
QScopedPointer< QGraphicsLayoutItemPrivate > | d_ptr |
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics View.
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.
{class="float-left"}
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.
QGraphicsAnchorLayout respects each item's size hints and size policies. Note that there are some properties of QSizePolicy that are Known issues{not respected}.
The layout may distribute some space between the items. If the spacing has not been explicitly specified, the actual amount of space will usually be 0.
However, if the first edge is the opposite of the second edge (e.g., the right edge of the first widget is anchored to the left edge of the second widget), the size of the anchor will be queried from the style through a pixel metric: QStyle::PM_LayoutHorizontalSpacing for horizontal anchors and QStyle::PM_LayoutVerticalSpacing for vertical anchors.
If the spacing is negative, the items will overlap to some extent.
This might change in the future, so avoid using these features if you want to avoid any future regressions in behaviour:
Stretch factors are not respected.
QSizePolicy::ExpandFlag is not respected.
Height for width is not respected.
Definition at line 82 of file qgraphicsanchorlayout.h.
QGraphicsAnchorLayout::QGraphicsAnchorLayout | ( | QGraphicsLayoutItem * | parent = 0 | ) |
Constructs a QGraphicsAnchorLayout instance.
parent is passed to QGraphicsLayout's constructor.
Definition at line 227 of file qgraphicsanchorlayout.cpp.
|
virtual |
Destroys the QGraphicsAnchorLayout object.
Definition at line 237 of file qgraphicsanchorlayout.cpp.
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 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 283 of file qgraphicsanchorlayout.cpp.
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 362 of file qgraphicsanchorlayout.cpp.
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 326 of file qgraphicsanchorlayout.cpp.
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 297 of file qgraphicsanchorlayout.cpp.
|
virtual |
Reimplemented Function
Implements QGraphicsLayout.
Definition at line 488 of file qgraphicsanchorlayout.cpp.
Referenced by ~QGraphicsAnchorLayout().
qreal QGraphicsAnchorLayout::horizontalSpacing | ( | ) | const |
Returns the default horizontal spacing for the anchor layout.
Definition at line 429 of file qgraphicsanchorlayout.cpp.
|
virtual |
Reimplemented Function
Reimplemented from QGraphicsLayout.
Definition at line 506 of file qgraphicsanchorlayout.cpp.
Referenced by addAnchor(), addCornerAnchors(), removeAt(), setHorizontalSpacing(), setSpacing(), and setVerticalSpacing().
|
virtual |
Reimplemented Function
Implements QGraphicsLayout.
Definition at line 497 of file qgraphicsanchorlayout.cpp.
|
virtual |
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 467 of file qgraphicsanchorlayout.cpp.
Referenced by ~QGraphicsAnchorLayout().
|
virtual |
Reimplemented Function
Reimplemented from QGraphicsLayoutItem.
Definition at line 449 of file qgraphicsanchorlayout.cpp.
void QGraphicsAnchorLayout::setHorizontalSpacing | ( | qreal | spacing | ) |
Sets the default horizontal spacing for the anchor layout to spacing.
Definition at line 384 of file qgraphicsanchorlayout.cpp.
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.
Definition at line 416 of file qgraphicsanchorlayout.cpp.
void QGraphicsAnchorLayout::setVerticalSpacing | ( | qreal | spacing | ) |
Sets the default vertical spacing for the anchor layout to spacing.
Definition at line 397 of file qgraphicsanchorlayout.cpp.
|
protectedvirtual |
Reimplemented Function
Implements QGraphicsLayoutItem.
Definition at line 517 of file qgraphicsanchorlayout.cpp.
qreal QGraphicsAnchorLayout::verticalSpacing | ( | ) | const |
Returns the default vertical spacing for the anchor layout.
Definition at line 440 of file qgraphicsanchorlayout.cpp.
|
friend |
Definition at line 119 of file qgraphicsanchorlayout.h.