QLayoutItem Class

The QLayoutItem class provides an abstract item that a QLayout manipulates. More...

Header: #include <QLayoutItem>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
Inherited By:

QLayout, QSpacerItem, and QWidgetItem

Public Functions

QLayoutItem(Qt::Alignment alignment = Qt::Alignment())
virtual ~QLayoutItem()
Qt::Alignment alignment() const
virtual QSizePolicy::ControlTypes controlTypes() const
virtual Qt::Orientations expandingDirections() const = 0
virtual QRect geometry() const = 0
virtual bool hasHeightForWidth() const
virtual int heightForWidth(int) const
virtual void invalidate()
virtual bool isEmpty() const = 0
virtual QLayout *layout()
virtual QSize maximumSize() const = 0
virtual int minimumHeightForWidth(int w) const
virtual QSize minimumSize() const = 0
void setAlignment(Qt::Alignment alignment)
virtual void setGeometry(const QRect &r) = 0
virtual QSize sizeHint() const = 0
virtual QSpacerItem *spacerItem()
virtual QWidget *widget() const

Detailed Description

This is used by custom layouts.

Pure virtual functions are provided to return information about the layout, including, sizeHint(), minimumSize(), maximumSize() and expandingDirections().

The layout's geometry can be set and retrieved with setGeometry() and geometry(), and its alignment with setAlignment() and alignment().

isEmpty() returns whether the layout item is empty. If the concrete item is a QWidget, it can be retrieved using widget(). Similarly for layout() and spacerItem().

Some layouts have width and height interdependencies. These can be expressed using hasHeightForWidth(), heightForWidth(), and minimumHeightForWidth(). For more explanation see the Qt Quarterly article Trading Height for Width.

See also QLayout.

Member Function Documentation

[explicit] QLayoutItem::QLayoutItem(Qt::Alignment alignment = Qt::Alignment())

Constructs a layout item with an alignment. Not all subclasses support alignment.

[virtual noexcept] QLayoutItem::~QLayoutItem()

Destroys the QLayoutItem.

Qt::Alignment QLayoutItem::alignment() const

Returns the alignment of this item.

See also setAlignment().

[virtual] QSizePolicy::ControlTypes QLayoutItem::controlTypes() const

Returns the control type(s) for the layout item. For a QWidgetItem, the control type comes from the widget's size policy; for a QLayoutItem, the control types is derived from the layout's contents.

See also QSizePolicy::controlType().

[pure virtual] Qt::Orientations QLayoutItem::expandingDirections() const

Returns whether this layout item can make use of more space than sizeHint(). A value of Qt::Vertical or Qt::Horizontal means that it wants to grow in only one dimension, whereas Qt::Vertical | Qt::Horizontal means that it wants to grow in both dimensions.

[pure virtual] QRect QLayoutItem::geometry() const

Returns the rectangle covered by this layout item.

See also setGeometry().

[virtual] bool QLayoutItem::hasHeightForWidth() const

Returns true if this layout's preferred height depends on its width; otherwise returns false. The default implementation returns false.

Reimplement this function in layout managers that support height for width.

See also heightForWidth() and QWidget::heightForWidth().

[virtual] int QLayoutItem::heightForWidth(int) const

Returns the preferred height for this layout item, given the width, which is not used in this default implementation.

The default implementation returns -1, indicating that the preferred height is independent of the width of the item. Using the function hasHeightForWidth() will typically be much faster than calling this function and testing for -1.

Reimplement this function in layout managers that support height for width. A typical implementation will look like this:

 int MyLayout::heightForWidth(int w) const
 {
     if (cache_dirty || cached_width != w) {
         MyLayout *that = const_cast<MyLayout *>(this);
         int h = calculateHeightForWidth(w);
         that->cached_hfw = h;
         return h;
     }
     return cached_hfw;
 }

Caching is strongly recommended; without it layout will take exponential time.

See also hasHeightForWidth().

[virtual] void QLayoutItem::invalidate()

Invalidates any cached information in this layout item.

[pure virtual] bool QLayoutItem::isEmpty() const

Implemented in subclasses to return whether this item is empty, i.e. whether it contains any widgets.

[virtual] QLayout *QLayoutItem::layout()

If this item is a QLayout, it is returned as a QLayout; otherwise nullptr is returned. This function provides type-safe casting.

See also spacerItem() and widget().

[pure virtual] QSize QLayoutItem::maximumSize() const

Implemented in subclasses to return the maximum size of this item.

[virtual] int QLayoutItem::minimumHeightForWidth(int w) const

Returns the minimum height this widget needs for the given width, w. The default implementation simply returns heightForWidth(w).

[pure virtual] QSize QLayoutItem::minimumSize() const

Implemented in subclasses to return the minimum size of this item.

void QLayoutItem::setAlignment(Qt::Alignment alignment)

Sets the alignment of this item to alignment.

Note: Item alignment is only supported by QLayoutItem subclasses where it would have a visual effect. Except for QSpacerItem, which provides blank space for layouts, all public Qt classes that inherit QLayoutItem support item alignment.

See also alignment().

[pure virtual] void QLayoutItem::setGeometry(const QRect &r)

Implemented in subclasses to set this item's geometry to r.

See also geometry().

[pure virtual] QSize QLayoutItem::sizeHint() const

Implemented in subclasses to return the preferred size of this item.

[virtual] QSpacerItem *QLayoutItem::spacerItem()

If this item is a QSpacerItem, it is returned as a QSpacerItem; otherwise nullptr is returned. This function provides type-safe casting.

See also layout() and widget().

[virtual] QWidget *QLayoutItem::widget() const

If this item manages a QWidget, returns that widget. Otherwise, nullptr is returned.

Note: While the functions layout() and spacerItem() perform casts, this function returns another object: QLayout and QSpacerItem inherit QLayoutItem, while QWidget does not.

See also layout() and spacerItem().