QGraphicsWidget Class
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene. More...
| Header: | #include <QGraphicsWidget> |
| qmake: | QT += widgets |
| Since: | Qt 4.4 |
| Inherits: | QGraphicsObject and QGraphicsLayoutItem |
| Inherited By: |
This class was introduced in Qt 4.4.
Public Types
| enum | anonymous { Type } |
Properties
|
|
Public Functions
| bool | autoFillBackground() const |
| Qt::FocusPolicy | focusPolicy() const |
| QFont | font() const |
| QGraphicsLayout * | layout() const |
| Qt::LayoutDirection | layoutDirection() const |
| QPalette | palette() const |
| void | resize(const QSizeF &size) |
| void | resize(qreal w, qreal h) |
| void | setAutoFillBackground(bool enabled) |
| void | setFocusPolicy(Qt::FocusPolicy policy) |
| void | setFont(const QFont &font) |
| void | setGeometry(qreal x, qreal y, qreal w, qreal h) |
| void | setLayout(QGraphicsLayout *layout) |
| void | setLayoutDirection(Qt::LayoutDirection direction) |
| void | setPalette(const QPalette &palette) |
| void | setWindowFlags(Qt::WindowFlags wFlags) |
| void | setWindowTitle(const QString &title) |
| QSizeF | size() const |
| void | unsetLayoutDirection() |
| Qt::WindowFlags | windowFlags() const |
| QString | windowTitle() const |
Reimplemented Public Functions
| virtual void | setGeometry(const QRectF &rect) override |
Signals
| void | geometryChanged() |
| void | layoutChanged() |
Detailed Description
QGraphicsWidget is an extended base item that provides extra functionality over QGraphicsItem. It is similar to QWidget in many ways:
- Provides a palette, a font and a style().
- Has a defined geometry().
- Supports layouts with setLayout() and layout().
- Supports shortcuts and actions with grabShortcut() and insertAction()
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.
Note: 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.
Note: The QObject::parent() should always return nullptr for QGraphicsWidgets, but this policy is not strictly defined.
See also QGraphicsProxyWidget, QGraphicsItem, and Widgets and Layouts.
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.
This property was introduced in Qt 4.7.
Access functions:
| bool | autoFillBackground() const |
| void | setAutoFillBackground(bool enabled) |
See also Qt::WA_OpaquePaintEvent and Qt::WA_NoSystemBackground.
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:
| Qt::FocusPolicy | focusPolicy() const |
| void | setFocusPolicy(Qt::FocusPolicy policy) |
See also focusInEvent(), focusOutEvent(), keyPressEvent(), keyReleaseEvent(), and enabled.
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:
| QFont | font() const |
| void | setFont(const QFont &font) |
See also QApplication::font(), QGraphicsScene::font, and QFont::resolve().
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 QRectF &rect) override |
| void | setGeometry(qreal x, qreal y, qreal w, qreal h) |
Notifier signal:
| void | geometryChanged() |
See also geometry() and 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:
| QGraphicsLayout * | layout() const |
| void | setLayout(QGraphicsLayout *layout) |
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:
| Qt::LayoutDirection | layoutDirection() const |
| void | setLayoutDirection(Qt::LayoutDirection direction) |
| void | unsetLayoutDirection() |
See also QWidget::layoutDirection and QApplication::layoutDirection.
maximumSize : const QSizeF
This property holds the maximum size of the widget
See also setMaximumSize(), maximumSize(), minimumSize, and preferredSize.
minimumSize : const QSizeF
This property holds the minimum size of the widget
See also setMinimumSize(), minimumSize(), preferredSize, and maximumSize.
palette : QPalette
This property holds the widget's palette
This property provides the widget's palette. The palette provides colors and brushes for color groups (e.g., QPalette::Button) and states (e.g., QPalette::Inactive), loosely defining the general look of the widget and its children.
QPalette consists of color groups that have been explicitly defined, and groups that are implicitly inherited from the widget's parent. Because of this, palette() can return a different palette than what has been set with setPalette(). This scheme allows you to define single entries in a palette without affecting the palette's inherited entries.
When a widget's palette changes, it resolves its entries against its parent widget, or if it doesn't have a parent widget, it resolves against the scene. It then sends itself a PaletteChange event, and notifies all its descendants so they can resolve their palettes as well.
By default, this property contains the application's default palette.
Access functions:
| QPalette | palette() const |
| void | setPalette(const QPalette &palette) |
See also QGuiApplication::palette(), QGraphicsScene::palette, and QPalette::resolve().
preferredSize : const QSizeF
This property holds the preferred size of the widget
See also setPreferredSize(), preferredSize(), minimumSize, and maximumSize.
size : QSizeF
This property holds the size of the widget
Calling resize() resizes the widget to a size bounded by minimumSize() and maximumSize(). This property only affects the widget's width and height (e.g., its right and bottom edges); the widget's position and top-left corner remains unaffected.
Resizing a widget triggers the widget to immediately receive a GraphicsSceneResize event with the widget's old and new size. If the widget has a layout assigned when this event arrives, the layout will be activated and it will automatically update any child widgets's geometry.
This property does not affect any layout of the parent widget. If the widget itself is managed by a parent layout; e.g., it has a parent widget with a layout assigned, that layout will not activate.
By default, this property contains a size with zero width and height.
Access functions:
| QSizeF | size() const |
| void | resize(const QSizeF &size) |
| void | resize(qreal w, qreal h) |
Notifier signal:
| void | geometryChanged() |
See also setGeometry(), QGraphicsSceneResizeEvent, and QGraphicsLayout.
sizePolicy : const QSizePolicy
This property holds the size policy for the widget
See also sizePolicy(), setSizePolicy(), and QWidget::sizePolicy().
windowFlags : Qt::WindowFlags
This property holds the widget's window flags
Window flags are a combination of a window type (e.g., Qt::Dialog) and several flags giving hints on the behavior of the window. The behavior is platform-dependent.
By default, this property contains no window flags.
Windows are panels. If you set the Qt::Window flag, the ItemIsPanel flag will be set automatically. If you clear the Qt::Window flag, the ItemIsPanel flag is also cleared. Note that the ItemIsPanel flag can be set independently of Qt::Window.
Access functions:
| Qt::WindowFlags | windowFlags() const |
| void | setWindowFlags(Qt::WindowFlags wFlags) |
See also isWindow() and isPanel().
windowTitle : QString
This property holds the window title (caption).
This property is only used for windows.
By default, if no title has been set, this property contains an empty string.
Access functions:
| QString | windowTitle() const |
| void | setWindowTitle(const QString &title) |