QGraphicsLayoutItem 类

QGraphicsLayoutItem 类可以被继承以允许自定义项由布局进行管理。 更多...

头: #include <QGraphicsLayoutItem>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承者: QGraphicsLayout and QGraphicsWidget

公共函数

  QGraphicsLayoutItem (QGraphicsLayoutItem * parent = nullptr, bool isLayout = false)
virtual ~QGraphicsLayoutItem ()
QRectF contentsRect () const
QSizeF effectiveSizeHint (Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const
QRectF geometry () const
virtual void getContentsMargins (qreal * left , qreal * top , qreal * right , qreal * bottom ) const
QGraphicsItem * graphicsItem () const
virtual bool isEmpty () const
bool isLayout () const
qreal maximumHeight () const
QSizeF maximumSize () const
qreal maximumWidth () const
qreal minimumHeight () const
QSizeF minimumSize () const
qreal minimumWidth () const
bool ownedByLayout () const
QGraphicsLayoutItem * parentLayoutItem () const
qreal preferredHeight () const
QSizeF preferredSize () const
qreal preferredWidth () const
virtual void setGeometry (const QRectF & rect )
void setMaximumHeight (qreal height )
void setMaximumSize (const QSizeF & size )
void setMaximumSize (qreal w , qreal h )
void setMaximumWidth (qreal width )
void setMinimumHeight (qreal height )
void setMinimumSize (const QSizeF & size )
void setMinimumSize (qreal w , qreal h )
void setMinimumWidth (qreal width )
void setParentLayoutItem (QGraphicsLayoutItem * parent )
void setPreferredHeight (qreal height )
void setPreferredSize (const QSizeF & size )
void setPreferredSize (qreal w , qreal h )
void setPreferredWidth (qreal width )
void setSizePolicy (const QSizePolicy & policy )
void setSizePolicy (QSizePolicy::Policy hPolicy , QSizePolicy::Policy vPolicy , QSizePolicy::ControlType controlType = QSizePolicy::DefaultType)
QSizePolicy sizePolicy () const
virtual void updateGeometry ()

保护函数

void setGraphicsItem (QGraphicsItem * item )
void setOwnedByLayout (bool ownership )
virtual QSizeF sizeHint (Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const = 0

详细描述

QGraphicsLayoutItem is an abstract class that defines a set of virtual functions describing sizes, size policies, and size hints for any object arranged by QGraphicsLayout . The API contains functions relevant for both the item itself and for the user of the item as most of QGraphicsLayoutItem's functions are also part of the subclass' public API.

In most cases, existing layout-aware classes such as QGraphicsWidget and QGraphicsLayout already provide the functionality you require. However, subclassing these classes will enable you to create both graphical elements that work well with layouts ( QGraphicsWidget ) or custom layouts ( QGraphicsLayout ).

子类化 QGraphicsLayoutItem

If you create a subclass of QGraphicsLayoutItem and reimplement its virtual functions, you will enable the layout to resize and position your item along with other QGraphicsLayoutItems including QGraphicsWidget and QGraphicsLayout .

You can start by reimplementing important functions: the protected sizeHint () function, as well as the public setGeometry () function. If you want your items to be aware of immediate geometry changes, you can also reimplement updateGeometry ().

The geometry, size hint, and size policy affect the item's size and position. Calling setGeometry () will always resize and reposition the item immediately. Normally, this function is called by QGraphicsLayout after the layout has been activated, but it can also be called by the item's user at any time.

sizeHint () function returns the item' minimum, preferred and maximum size hints. You can override these properties by calling setMinimumSize (), setPreferredSize () 或 setMaximumSize (). You can also use functions such as setMinimumWidth () 或 setMaximumHeight () to set only the width or height component if desired.

effectiveSizeHint () function, on the other hand, returns a size hint for any given Qt::SizeHint , and guarantees that the returned size is bound to the minimum and maximum sizes and size hints. You can set the item's vertical and horizontal size policy by calling setSizePolicy ()。 sizePolicy property is used by the layout system to describe how this item prefers to grow or shrink.

嵌套 QGraphicsLayoutItems

QGraphicsLayoutItems can be nested within other QGraphicsLayoutItems, similar to layouts that can contain sublayouts. This is done either by passing a QGraphicsLayoutItem pointer to QGraphicsLayoutItem's protected constructor, or by calling setParentLayoutItem ()。 parentLayoutItem () function returns a pointer to the item's layoutItem parent. If the item's parent is nullptr or if the parent does not inherit from QGraphicsItem parentLayoutItem () function then returns nullptr . isLayout () 返回 true if the QGraphicsLayoutItem subclass is itself a layout, or false otherwise.

Qt uses QGraphicsLayoutItem to provide layout functionality in the 图形视图框架 , but in the future its use may spread throughout Qt itself.

另请参阅 QGraphicsWidget , QGraphicsLayout , QGraphicsLinearLayout ,和 QGraphicsGridLayout .

成员函数文档编制

QGraphicsLayoutItem:: QGraphicsLayoutItem ( QGraphicsLayoutItem * parent = nullptr, bool isLayout = false)

Constructs the QGraphicsLayoutItem object. parent becomes the object's parent. If isLayout is true the item is a layout, otherwise isLayout 为 false。

[虚拟] QGraphicsLayoutItem:: ~QGraphicsLayoutItem ()

销毁 QGraphicsLayoutItem 对象。

QRectF QGraphicsLayoutItem:: contentsRect () const

Returns the contents rect in local coordinates.

The contents rect defines the subrectangle used by an associated layout when arranging subitems. This function is a convenience function that adjusts the item's geometry () by its contents margins. Note that getContentsMargins () is a virtual function that you can reimplement to return the item's contents margins.

另请参阅 getContentsMargins () 和 geometry ().

QSizeF QGraphicsLayoutItem:: effectiveSizeHint ( Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const

Returns the effective size hint for this QGraphicsLayoutItem .

which is the size hint in question. constraint is an optional argument that defines a special constrain when calculating the effective size hint. By default, constraint is QSizeF (-1, -1), which means there is no constraint to the size hint.

If you want to specify the widget's size hint for a given width or height, you can provide the fixed dimension in constraint . This is useful for widgets that can grow only either vertically or horizontally, and need to set either their width or their height to a special value.

For example, a text paragraph item fit into a column width of 200 may grow vertically. You can pass QSizeF (200, -1) as a constraint to get a suitable minimum, preferred and maximum height).

You can adjust the effective size hint by reimplementing sizeHint () 在 QGraphicsLayoutItem subclass, or by calling one of the following functions: setMinimumSize (), setPreferredSize ,或 setMaximumSize () (or a combination of both).

This function caches each of the size hints and guarantees that sizeHint () will be called only once for each value of which - unless constraint is not specified and updateGeometry () 被调用。

另请参阅 sizeHint ().

QRectF QGraphicsLayoutItem:: geometry () const

Returns the item's geometry (e.g., position and size) as a QRectF . This function is equivalent to QRectF (pos(), size()).

另请参阅 setGeometry ().

[虚拟] void QGraphicsLayoutItem:: 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 . The default implementation assumes all contents margins are 0. The parameters point to values stored in qreals. If any of the pointers is nullptr , that value will not be updated.

另请参阅 QGraphicsWidget::setContentsMargins ().

QGraphicsItem *QGraphicsLayoutItem:: graphicsItem () const

返回 QGraphicsItem that this layout item represents. For QGraphicsWidget it will return itself. For custom items it can return an aggregated value.

另请参阅 setGraphicsItem ().

[virtual, since 6.0] bool QGraphicsLayoutItem:: isEmpty () const

返回 true if this item is empty, i.e whether it has no content and should not occupy any space.

默认实现返回 true true if the item has been hidden, unless its size policy has retainSizeWhenHidden set to true

该函数在 Qt 6.0 引入。

另请参阅 sizePolicy ().

bool QGraphicsLayoutItem:: isLayout () const

返回 true 若此 QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges other QGraphicsLayoutItem objects); otherwise returns false .

另请参阅 QGraphicsLayout .

qreal QGraphicsLayoutItem:: maximumHeight () const

Returns the maximum height.

另请参阅 setMaximumHeight (), setMaximumSize (),和 maximumSize ().

QSizeF QGraphicsLayoutItem:: maximumSize () const

Returns the maximum size.

另请参阅 setMaximumSize (), minimumSize (), preferredSize (), Qt::MaximumSize ,和 sizeHint ().

qreal QGraphicsLayoutItem:: maximumWidth () const

Returns the maximum width.

另请参阅 setMaximumWidth (), setMaximumSize (),和 maximumSize ().

qreal QGraphicsLayoutItem:: minimumHeight () const

Returns the minimum height.

另请参阅 setMinimumHeight (), setMinimumSize (),和 minimumSize ().

QSizeF QGraphicsLayoutItem:: minimumSize () const

Returns the minimum size.

另请参阅 setMinimumSize (), preferredSize (), maximumSize (), Qt::MinimumSize ,和 sizeHint ().

qreal QGraphicsLayoutItem:: minimumWidth () const

Returns the minimum width.

另请参阅 setMinimumWidth (), setMinimumSize (),和 minimumSize ().

bool QGraphicsLayoutItem:: ownedByLayout () const

Returns whether a layout should delete this item in its destructor. If its true, then the layout will delete it. If its false, then it is assumed that another object has the ownership of it, and the layout won't delete this item.

If the item inherits both QGraphicsItem and QGraphicsLayoutItem (譬如 QGraphicsWidget does) the item is really part of two ownership hierarchies. This property informs what the layout should do with its child items when it is destructed. In the case of QGraphicsWidget , it is preferred that when the layout is deleted it won't delete its children (since they are also part of the graphics item hierarchy).

By default this value is initialized to false in QGraphicsLayoutItem , but it is overridden by QGraphicsLayout to return true. This is because QGraphicsLayout is not normally part of the QGraphicsItem hierarchy, so the parent layout should delete it. Subclasses might override this default behaviour by calling setOwnedByLayout (true)。

另请参阅 setOwnedByLayout ().

QGraphicsLayoutItem *QGraphicsLayoutItem:: parentLayoutItem () const

Returns the parent of this QGraphicsLayoutItem ,或 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).

另请参阅 setParentLayoutItem ().

qreal QGraphicsLayoutItem:: preferredHeight () const

Returns the preferred height.

另请参阅 setPreferredHeight (), setPreferredSize (),和 preferredSize ().

QSizeF QGraphicsLayoutItem:: preferredSize () const

Returns the preferred size.

另请参阅 setPreferredSize (), minimumSize (), maximumSize (), Qt::PreferredSize ,和 sizeHint ().

qreal QGraphicsLayoutItem:: preferredWidth () const

Returns the preferred width.

另请参阅 setPreferredWidth (), setPreferredSize (),和 preferredSize ().

[虚拟] void QGraphicsLayoutItem:: 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).

You must reimplement this function in a subclass of QGraphicsLayoutItem to receive geometry updates. The layout will call this function when it does a rearrangement.

rect is outside of the bounds of minimumSize and maximumSize , it will be adjusted to its closest size so that it is within the legal bounds.

另请参阅 geometry ().

[protected] void QGraphicsLayoutItem:: setGraphicsItem ( QGraphicsItem * item )

QGraphicsLayoutItem represents a QGraphicsItem , and it wants to take advantage of the automatic reparenting capabilities of QGraphicsLayout it should set this value. Note that if you delete item and not delete the layout item, you are responsible of calling setGraphicsItem( nullptr ) in order to avoid having a dangling pointer.

另请参阅 graphicsItem ().

void QGraphicsLayoutItem:: setMaximumHeight ( qreal height )

Sets the maximum height to height .

另请参阅 maximumHeight (), setMaximumSize (),和 maximumSize ().

void QGraphicsLayoutItem:: setMaximumSize (const QSizeF & size )

Sets the maximum size to size . This property overrides sizeHint () 对于 Qt::MaximumSize and ensures that effectiveSizeHint () will never return a size larger than size . In order to unset the maximum size, use an invalid size.

另请参阅 maximumSize (), minimumSize (), preferredSize (), Qt::MaximumSize ,和 sizeHint ().

void QGraphicsLayoutItem:: setMaximumSize ( qreal w , qreal h )

This convenience function is equivalent to calling setMaximumSize( QSizeF ( w , h )).

另请参阅 maximumSize (), setMinimumSize (), setPreferredSize (),和 sizeHint ().

void QGraphicsLayoutItem:: setMaximumWidth ( qreal width )

Sets the maximum width to width .

另请参阅 maximumWidth (), setMaximumSize (),和 maximumSize ().

void QGraphicsLayoutItem:: setMinimumHeight ( qreal height )

Sets the minimum height to height .

另请参阅 minimumHeight (), setMinimumSize (),和 minimumSize ().

void QGraphicsLayoutItem:: setMinimumSize (const QSizeF & size )

Sets the minimum size to size . This property overrides sizeHint () 对于 Qt::MinimumSize and ensures that effectiveSizeHint () will never return a size smaller than size . In order to unset the minimum size, use an invalid size.

另请参阅 minimumSize (), maximumSize (), preferredSize (), Qt::MinimumSize , sizeHint (), setMinimumWidth (),和 setMinimumHeight ().

void QGraphicsLayoutItem:: setMinimumSize ( qreal w , qreal h )

This convenience function is equivalent to calling setMinimumSize( QSizeF ( w , h )).

另请参阅 minimumSize (), setMaximumSize (), setPreferredSize (),和 sizeHint ().

void QGraphicsLayoutItem:: setMinimumWidth ( qreal width )

Sets the minimum width to width .

另请参阅 minimumWidth (), setMinimumSize (),和 minimumSize ().

[protected] void QGraphicsLayoutItem:: setOwnedByLayout ( bool ownership )

Sets whether a layout should delete this item in its destructor or not. ownership must be true to in order for the layout to delete it.

另请参阅 ownedByLayout ().

void QGraphicsLayoutItem:: setParentLayoutItem ( QGraphicsLayoutItem * parent )

Sets the parent of this QGraphicsLayoutItem to parent .

另请参阅 parentLayoutItem ().

void QGraphicsLayoutItem:: setPreferredHeight ( qreal height )

Sets the preferred height to height .

另请参阅 preferredHeight (), preferredWidth (), setPreferredSize (),和 preferredSize ().

void QGraphicsLayoutItem:: setPreferredSize (const QSizeF & size )

Sets the preferred size to size . This property overrides sizeHint () 对于 Qt::PreferredSize and provides the default value for effectiveSizeHint (). In order to unset the preferred size, use an invalid size.

另请参阅 preferredSize (), minimumSize (), maximumSize (), Qt::PreferredSize ,和 sizeHint ().

void QGraphicsLayoutItem:: setPreferredSize ( qreal w , qreal h )

This convenience function is equivalent to calling setPreferredSize( QSizeF ( w , h )).

另请参阅 preferredSize (), setMaximumSize (), setMinimumSize (),和 sizeHint ().

void QGraphicsLayoutItem:: setPreferredWidth ( qreal width )

Sets the preferred width to width .

另请参阅 preferredWidth (), preferredHeight (), setPreferredSize (),和 preferredSize ().

void QGraphicsLayoutItem:: setSizePolicy (const QSizePolicy & policy )

Sets the size policy to policy . The size policy describes how the item should grow horizontally and vertically when arranged in a layout.

QGraphicsLayoutItem 's default size policy is ( QSizePolicy::Fixed , QSizePolicy::Fixed , QSizePolicy::DefaultType ), but it is common for subclasses to change the default. For example, QGraphicsWidget defaults to ( QSizePolicy::Preferred , QSizePolicy::Preferred , QSizePolicy::DefaultType ).

另请参阅 sizePolicy () 和 QWidget::sizePolicy ().

void QGraphicsLayoutItem:: setSizePolicy ( QSizePolicy::Policy hPolicy , QSizePolicy::Policy vPolicy , QSizePolicy::ControlType controlType = QSizePolicy::DefaultType)

这是重载函数。

This function is equivalent to calling setSizePolicy( QSizePolicy ( hPolicy , vPolicy , controlType )).

另请参阅 sizePolicy () 和 QWidget::sizePolicy ().

[pure virtual protected] QSizeF QGraphicsLayoutItem:: sizeHint ( Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const

This pure virtual function returns the size hint for which QGraphicsLayoutItem , using the width or height of constraint to constrain the output.

Reimplement this function in a subclass of QGraphicsLayoutItem to provide the necessary size hints for your items.

另请参阅 effectiveSizeHint ().

QSizePolicy QGraphicsLayoutItem:: sizePolicy () const

Returns the current size policy.

另请参阅 setSizePolicy () 和 QWidget::sizePolicy ().

[虚拟] void QGraphicsLayoutItem:: updateGeometry ()

This virtual function discards any cached size hint information. You should always call this function if you change the return value of the sizeHint () function. Subclasses must always call the base implementation when reimplementing this function.

另请参阅 effectiveSizeHint ().