Qt 4.8
Public Functions | Protected Variables | List of all members
QWidgetItem Class Reference

The QWidgetItem class is a layout item that represents a widget. More...

#include <qlayoutitem.h>

Inheritance diagram for QWidgetItem:
QLayoutItem QDockWidgetItem QToolBarItem QWidgetItemV2

Public Functions

Qt::Orientations expandingDirections () const
 Reimplemented Function More...
 
QRect geometry () const
 Reimplemented Function More...
 
bool hasHeightForWidth () const
 Reimplemented Function More...
 
int heightForWidth (int) const
 Reimplemented Function More...
 
bool isEmpty () const
 Returns true if the widget is hidden; otherwise returns false. More...
 
QSize maximumSize () const
 Reimplemented Function More...
 
QSize minimumSize () const
 Reimplemented Function More...
 
 QWidgetItem (QWidget *w)
 Creates an item containing the given widget. More...
 
void setGeometry (const QRect &)
 Reimplemented Function More...
 
QSize sizeHint () const
 Reimplemented Function More...
 
virtual QWidgetwidget ()
 Returns the widget managed by this item. More...
 
- Public Functions inherited from QLayoutItem
Qt::Alignment alignment () const
 Returns the alignment of this item. More...
 
QSizePolicy::ControlTypes controlTypes () const
 Returns the control type(s) for the layout item. More...
 
virtual void invalidate ()
 Invalidates any cached information in this layout item. More...
 
virtual QLayoutlayout ()
 If this item is a QLayout, it is returned as a QLayout; otherwise 0 is returned. More...
 
virtual int minimumHeightForWidth (int) const
 Returns the minimum height this widget needs for the given width, w. More...
 
 QLayoutItem (Qt::Alignment alignment=0)
 Constructs a layout item with an alignment. More...
 
void setAlignment (Qt::Alignment a)
 Sets the alignment of this item to alignment. More...
 
virtual QSpacerItemspacerItem ()
 If this item is a QSpacerItem, it is returned as a QSpacerItem; otherwise 0 is returned. More...
 
virtual ~QLayoutItem ()
 Destroys the QLayoutItem. More...
 

Protected Variables

QWidgetwid
 
- Protected Variables inherited from QLayoutItem
Qt::Alignment align
 

Detailed Description

The QWidgetItem class is a layout item that represents a widget.

Normally, you don't need to use this class directly. Qt's built-in layout managers provide the following functions for manipulating widgets in layouts:

Class Functions
QBoxLayout addWidget(), insertWidget(), setStretchFactor()
QGridLayout addWidget()
QStackedLayout addWidget(), insertWidget(), currentWidget(), setCurrentWidget(), widget()
See also
QLayout, QSpacerItem, QLayoutItem::widget()

Definition at line 122 of file qlayoutitem.h.

Constructors and Destructors

◆ QWidgetItem()

QWidgetItem::QWidgetItem ( QWidget w)
inlineexplicit

Creates an item containing the given widget.

Definition at line 127 of file qlayoutitem.h.

127 : wid(w) { }
QWidget * wid
Definition: qlayoutitem.h:141

Functions

◆ expandingDirections()

Qt::Orientations QWidgetItem::expandingDirections ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Definition at line 603 of file qlayoutitem.cpp.

Referenced by QToolBarLayout::updateGeomArray().

604 {
605  if (isEmpty())
606  return Qt::Orientations(0);
607 
608  Qt::Orientations e = wid->sizePolicy().expandingDirections();
609  /*
610  ### Qt 4.0:
611  If the layout is expanding, we make the widget expanding, even if
612  its own size policy isn't expanding. This behavior should be
613  reconsidered.
614  */
615  if (wid->layout()) {
618  e |= Qt::Horizontal;
621  e |= Qt::Vertical;
622  }
623 
625  e &= ~Qt::Horizontal;
627  e &= ~Qt::Vertical;
628  return e;
629 }
Policy horizontalPolicy() const
Definition: qsizepolicy.h:118
Qt::Orientations expandingDirections() const
Returns whether this layout can make use of more space than sizeHint().
Definition: qlayout.cpp:1198
Qt::Orientations expandingDirections() const
Definition: qsizepolicy.h:126
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition: qwidget.h:171
Policy verticalPolicy() const
Definition: qsizepolicy.h:119
Definition: qnamespace.h:54
QLayout * layout() const
Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed...
Definition: qwidget.cpp:10073
QWidget * wid
Definition: qlayoutitem.h:141
Qt::Alignment align
Definition: qlayoutitem.h:90

◆ geometry()

QRect QWidgetItem::geometry ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Definition at line 542 of file qlayoutitem.cpp.

Referenced by QPushButtonPrivate::adjustedMenuPosition().

543 {
545  ? toLayoutItemRect(wid->d_func(), wid->geometry())
546  : wid->geometry();
547 }
static QRect toLayoutItemRect(QWidgetPrivate *priv, const QRect &rect)
Definition: qlayoutitem.cpp:66
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
QWidget * wid
Definition: qlayoutitem.h:141
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition: qwidget.h:158

◆ hasHeightForWidth()

bool QWidgetItem::hasHeightForWidth ( ) const
virtual

Reimplemented Function

Reimplemented from QLayoutItem.

Definition at line 553 of file qlayoutitem.cpp.

554 {
555  if (isEmpty())
556  return false;
557  return wid->d_func()->hasHeightForWidth();
558 }
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
QWidget * wid
Definition: qlayoutitem.h:141

◆ heightForWidth()

int QWidgetItem::heightForWidth ( int  w) const
virtual

Reimplemented Function

Reimplemented from QLayoutItem.

Reimplemented in QWidgetItemV2.

Definition at line 563 of file qlayoutitem.cpp.

Referenced by QWidgetItemV2::heightForWidth().

564 {
565  if (isEmpty())
566  return -1;
567 
569  ? fromLayoutItemSize(wid->d_func(), QSize(w, 0)).width()
570  : w;
571 
572  int hfw;
573  if (wid->layout())
574  hfw = wid->layout()->totalHeightForWidth(w);
575  else
576  hfw = wid->heightForWidth(w);
577 
578  if (hfw > wid->maximumHeight())
579  hfw = wid->maximumHeight();
580  if (hfw < wid->minimumHeight())
581  hfw = wid->minimumHeight();
582 
584  ? toLayoutItemSize(wid->d_func(), QSize(0, hfw)).height()
585  : hfw;
586 
587  if (hfw < 0)
588  hfw = 0;
589  return hfw;
590 }
int totalHeightForWidth(int w) const
Also takes contentsMargins and menu bar into account.
Definition: qlayout.cpp:823
virtual int heightForWidth(int) const
Returns the preferred height for this widget, given the width w.
Definition: qwidget.cpp:10241
int minimumHeight
the widget&#39;s minimum height in pixels
Definition: qwidget.h:175
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
Definition: qlayoutitem.cpp:72
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
int maximumHeight
the widget&#39;s maximum height in pixels
Definition: qwidget.h:177
QLayout * layout() const
Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed...
Definition: qwidget.cpp:10073
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QWidget * wid
Definition: qlayoutitem.h:141
static QSize fromLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
Definition: qlayoutitem.cpp:61

◆ isEmpty()

bool QWidgetItem::isEmpty ( ) const
virtual

Returns true if the widget is hidden; otherwise returns false.

See also
QWidget::isHidden()

Implements QLayoutItem.

Reimplemented in QToolBarItem.

Definition at line 718 of file qlayoutitem.cpp.

Referenced by QWidgetItemV2::heightForWidth(), QWidgetItemV2::maximumSize(), QWidgetItemV2::minimumSize(), and QWidgetItemV2::sizeHint().

719 {
720  return wid->isHidden() || wid->isWindow();
721 }
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
QWidget * wid
Definition: qlayoutitem.h:141

◆ maximumSize()

QSize QWidgetItem::maximumSize ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Reimplemented in QDockWidgetItem, and QWidgetItemV2.

Definition at line 664 of file qlayoutitem.cpp.

Referenced by QMainWindowLayout::fixSizeInUnifiedToolbar(), make_widget_eventUPP(), QWidgetItemV2::maximumSize(), and QToolBarLayout::updateGeomArray().

665 {
666  if (isEmpty()) {
667  return QSize(0, 0);
668  } else {
670  ? toLayoutItemSize(wid->d_func(), qSmartMaxSize(this, align))
671  : qSmartMaxSize(this, align);
672  }
673 }
Q_GUI_EXPORT QSize qSmartMaxSize(const QSize &sizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy, Qt::Alignment align)
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
Definition: qlayoutitem.cpp:72
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QWidget * wid
Definition: qlayoutitem.h:141
Qt::Alignment align
Definition: qlayoutitem.h:90

◆ minimumSize()

QSize QWidgetItem::minimumSize ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Reimplemented in QDockWidgetItem, and QWidgetItemV2.

Definition at line 643 of file qlayoutitem.cpp.

Referenced by QMainWindowLayout::fixSizeInUnifiedToolbar(), make_widget_eventUPP(), QWidgetItemV2::minimumSize(), and QToolBarLayout::updateGeomArray().

644 {
645  if (isEmpty())
646  return QSize(0, 0);
648  ? toLayoutItemSize(wid->d_func(), qSmartMinSize(this))
649  : qSmartMinSize(this);
650 }
Q_GUI_EXPORT QSize qSmartMinSize(const QSize &sizeHint, const QSize &minSizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy)
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
Definition: qlayoutitem.cpp:72
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QWidget * wid
Definition: qlayoutitem.h:141

◆ setGeometry()

void QWidgetItem::setGeometry ( const QRect rect)
virtual

Reimplemented Function

Implements QLayoutItem.

Definition at line 474 of file qlayoutitem.cpp.

Referenced by QToolBarLayout::layoutActions().

475 {
476  if (isEmpty())
477  return;
478 
480  ? fromLayoutItemRect(wid->d_func(), rect)
481  : rect;
482  const QSize widgetRectSurplus = r.size() - rect.size();
483 
484  /*
485  For historical reasons, this code is done using widget rect
486  coordinates, not layout item rect coordinates. However,
487  QWidgetItem's sizeHint(), maximumSize(), and heightForWidth()
488  all work in terms of layout item rect coordinates, so we have to
489  add or subtract widgetRectSurplus here and there. The code could
490  be much simpler if we did everything using layout item rect
491  coordinates and did the conversion right before the call to
492  QWidget::setGeometry().
493  */
494 
495  QSize s = r.size().boundedTo(maximumSize() + widgetRectSurplus);
496  int x = r.x();
497  int y = r.y();
499  QSize pref(sizeHint());
500  QSizePolicy sp = wid->sizePolicy();
502  pref.setWidth(wid->sizeHint().expandedTo(wid->minimumSize()).width());
504  pref.setHeight(wid->sizeHint().expandedTo(wid->minimumSize()).height());
505  pref += widgetRectSurplus;
507  s.setWidth(qMin(s.width(), pref.width()));
509  if (hasHeightForWidth())
510  s.setHeight(qMin(s.height(),
511  heightForWidth(s.width() - widgetRectSurplus.width())
512  + widgetRectSurplus.height()));
513  else
514  s.setHeight(qMin(s.height(), pref.height()));
515  }
516  }
517  Qt::Alignment alignHoriz = QStyle::visualAlignment(wid->layoutDirection(), align);
518  if (alignHoriz & Qt::AlignRight)
519  x = x + (r.width() - s.width());
520  else if (!(alignHoriz & Qt::AlignLeft))
521  x = x + (r.width() - s.width()) / 2;
522 
523  if (align & Qt::AlignBottom)
524  y = y + (r.height() - s.height());
525  else if (!(align & Qt::AlignTop))
526  y = y + (r.height() - s.height()) / 2;
527 
528  wid->setGeometry(x, y, s.width(), s.height());
529 }
QSize maximumSize() const
Reimplemented Function
QSize minimumSize
the widget&#39;s minimum size
Definition: qwidget.h:172
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QSize sizeHint() const
Reimplemented Function
Policy horizontalPolicy() const
Definition: qsizepolicy.h:118
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
int heightForWidth(int) const
Reimplemented Function
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
int width() const
Returns the width.
Definition: qsize.h:126
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
Qt::LayoutDirection layoutDirection
the layout direction for this widget
Definition: qwidget.h:216
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition: qstyle.cpp:2149
static QRect fromLayoutItemRect(QWidgetPrivate *priv, const QRect &rect)
Definition: qlayoutitem.cpp:55
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition: qwidget.h:171
Policy verticalPolicy() const
Definition: qsizepolicy.h:119
bool hasHeightForWidth() const
Reimplemented Function
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSize sizeHint
the recommended size for the widget
Definition: qwidget.h:195
QWidget * wid
Definition: qlayoutitem.h:141
Qt::Alignment align
Definition: qlayoutitem.h:90

◆ sizeHint()

QSize QWidgetItem::sizeHint ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Reimplemented in QDockWidgetItem, and QWidgetItemV2.

Definition at line 686 of file qlayoutitem.cpp.

Referenced by QToolBarLayout::expandedSize(), QToolBarLayout::layoutActions(), QWidgetItemV2::sizeHint(), QDockWidgetItem::sizeHint(), and QToolBarLayout::updateGeomArray().

687 {
688  QSize s(0, 0);
689  if (!isEmpty()) {
691  s = s.boundedTo(wid->maximumSize())
692  .expandedTo(wid->minimumSize());
694  ? toLayoutItemSize(wid->d_func(), s)
695  : s;
696 
698  s.setWidth(0);
700  s.setHeight(0);
701  }
702  return s;
703 }
QSize maximumSize
the widget&#39;s maximum size in pixels
Definition: qwidget.h:173
QSize minimumSize
the widget&#39;s minimum size
Definition: qwidget.h:172
Policy horizontalPolicy() const
Definition: qsizepolicy.h:118
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
Definition: qlayoutitem.cpp:72
QSize minimumSizeHint
the recommended minimum size for the widget
Definition: qwidget.h:196
bool isEmpty() const
Returns true if the widget is hidden; otherwise returns false.
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition: qwidget.h:171
Policy verticalPolicy() const
Definition: qsizepolicy.h:119
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSize sizeHint
the recommended size for the widget
Definition: qwidget.h:195
QWidget * wid
Definition: qlayoutitem.h:141

◆ widget()

QWidget * QWidgetItem::widget ( )
virtual

Returns the widget managed by this item.

Reimplemented from QLayoutItem.

Definition at line 385 of file qlayoutitem.cpp.

Referenced by defaultWidgetAction(), QToolBarLayout::layoutActions(), QToolBarLayout::takeAt(), QToolBarLayout::updateGeomArray(), and QToolBarLayout::~QToolBarLayout().

386 {
387  return wid;
388 }
QWidget * wid
Definition: qlayoutitem.h:141

Properties

◆ wid

QWidget* QWidgetItem::wid
protected

The documentation for this class was generated from the following files: