QGraphicsWidget Class

Detailed Description

QGraphicsWidget is an extended base item that provides extra functionality over QGraphicsItem. It is similar to QWidget in many ways:

Unlike QGraphicsItem, QGraphicsWidget is not an abstract class; you can create instances of a QGraphicsWidget without having to subclass it. This approach is useful for widgets that only serve the purpose of organizing child widgets into a layout.

QGraphicsWidget can be used as a base item for your own custom item if you require advanced input focus handling, e.g., tab focus and activation, or layouts.

Since QGraphicsWidget resembles QWidget and has similar API, it is easier to port a widget from QWidget to QGraphicsWidget, instead of QGraphicsItem.

QWidget-based widgets can be directly embedded into a QGraphicsScene using QGraphicsProxyWidget.

Noticeable differences between QGraphicsWidget and QWidget are:

QGraphicsWidget

QWidget

Coordinates and geometry are defined with qreals (doubles or floats, depending on the platform).

QWidget uses integer geometry (QPoint, QRect).

The widget is already visible by default; you do not have to call show() to display the widget.

QWidget is hidden by default until you call show().

A subset of widget attributes are supported.

All widget attributes are supported.

A top-level item's style defaults to QGraphicsScene::style

A top-level widget's style defaults to QApplication::style

Graphics View provides a custom drag and drop framework, different from QWidget.

Standard drag and drop framework.

Widget items do not support modality.

Full modality support.

QGraphicsWidget supports a subset of Qt's widget attributes, (Qt::WidgetAttribute), as shown in the table below. Any attributes not listed in this table are unsupported, or otherwise unused.

Widget Attribute

Usage

Qt::WA_SetLayoutDirection

Set by setLayoutDirection(), cleared by unsetLayoutDirection(). You can test this attribute to check if the widget has been explicitly assigned a layoutDirection. If the attribute is not set, the layoutDirection() is inherited.

Qt::WA_RightToLeft

Toggled by setLayoutDirection(). Inherited from the parent/scene. If set, the widget's layout will order horizontally arranged widgets from right to left.

Qt::WA_SetStyle

Set and cleared by setStyle(). If this attribute is set, the widget has been explicitly assigned a style. If it is unset, the widget will use the scene's or the application's style.

Qt::WA_Resized

Set by setGeometry() and resize().

Qt::WA_SetPalette

Set by setPalette().

Qt::WA_SetFont

Set by setFont().

Qt::WA_WindowPropagation

Enables propagation to window widgets.

Although QGraphicsWidget inherits from both QObject and QGraphicsItem, you should use the functions provided by QGraphicsItem, not QObject, to manage the relationships between parent and child items. These functions control the stacking order of items as well as their ownership.

The QObject::parent() should always return nullptr for QGraphicsWidgets, but this policy is not strictly defined.

See Also

Member Type Documentation

 

enum QGraphicsWidget::anonymous

The value returned by the virtual type() function.

Constant

Value

Description

QGraphicsWidget::Type

11

A graphics widget item

Property Documentation

 

autoFillBackground : bool

This property holds whether the widget background is filled automatically

If enabled, this property will cause Qt to fill the background of the widget before invoking the paint() method. The color used is defined by the QPalette::Window color role from the widget's palette.

In addition, Windows are always filled with QPalette::Window, unless the WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.

By default, this property is false.

Access functions:

  • bool autoFillBackground() const

  • void setAutoFillBackground(bool enabled)

See Also

focusPolicy : Qt::FocusPolicy

This property holds the way the widget accepts keyboard focus

The focus policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing, Qt::ClickFocus if the widget accepts focus by clicking, Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all.

You must enable keyboard focus for a widget if it processes keyboard events. This is normally done from the widget's constructor. For instance, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus).

If you enable a focus policy (i.e., not Qt::NoFocus), QGraphicsWidget will automatically enable the ItemIsFocusable flag. Setting Qt::NoFocus on a widget will clear the ItemIsFocusable flag. If the widget currently has keyboard focus, the widget will automatically lose focus.

Access functions:

  • focusPolicy() const

  • void setFocusPolicy( policy)

See Also

font : QFont

This property holds the widgets' font

This property provides the widget's font.

QFont consists of font properties that have been explicitly defined and properties implicitly inherited from the widget's parent. Hence, font() can return a different font compared to the one set with setFont(). This scheme allows you to define single entries in a font without affecting the font's inherited entries.

When a widget's font changes, it resolves its entries against its parent widget. If the widget does not have a parent widget, it resolves its entries against the scene. The widget then sends itself a FontChange event and notifies all its descendants so that they can resolve their fonts as well.

By default, this property contains the application's default font.

Access functions:

  • font() const

  • void setFont(const &font)

See Also

geometry : QRectF

This property holds the geometry of the widget

Sets the item's geometry to rect. The item's position and size are modified as a result of calling this function. The item is first moved, then resized.

A side effect of calling this function is that the widget will receive a move event and a resize event. Also, if the widget has a layout assigned, the layout will activate.

Access functions:

  • virtual void setGeometry(const &rect) override

  • void setGeometry( x, y, w, h)

Notifier signal:

See Also

See also geometry(), resize()

layout : QGraphicsLayout*

This property holds the layout of the widget

Any existing layout manager is deleted before the new layout is assigned. If layout is nullptr, the widget is left without a layout. Existing subwidgets' geometries will remain unaffected.

QGraphicsWidget takes ownership of layout.

All widgets that are currently managed by layout or all of its sublayouts, are automatically reparented to this item. The layout is then invalidated, and the child widget geometries are adjusted according to this item's geometry() and contentsMargins(). Children who are not explicitly managed by layout remain unaffected by the layout after it has been assigned to this widget.

If no layout is currently managing this widget, layout() will return nullptr.

Access functions:

Notifier signal:

  • void layoutChanged()

layoutDirection : Qt::LayoutDirection

This property holds the layout direction for this widget.

This property modifies this widget's and all of its descendants' Qt::WA_RightToLeft attribute. It also sets this widget's Qt::WA_SetLayoutDirection attribute.

The widget's layout direction determines the order in which the layout manager horizontally arranges subwidgets of this widget. The default value depends on the language and locale of the application, and is typically in the same direction as words are read and written. With Qt::LeftToRight, the layout starts placing subwidgets from the left side of this widget towards the right. Qt::RightToLeft does the opposite - the layout will place widgets starting from the right edge moving towards the left.

Subwidgets inherit their layout direction from the parent. Top-level widget items inherit their layout direction from QGraphicsScene::layoutDirection. If you change a widget's layout direction by calling setLayoutDirection(), the widget will send itself a LayoutDirectionChange event, and then propagate the new layout direction to all its descendants.

Access functions:

  • layoutDirection() const

  • void setLayoutDirection( direction)

  • void unsetLayoutDirection()

See Also

See also