Qt Graphics View Framework: QGraphicsWidget, QGraphicsLayoutItem

QGraphicsWidget

1. Description

1. QGraphicsWidget is an extended basic item that provides additional functions on top of QGraphicsItem. It is similar to QWidget in many ways:

Provides palettes, fonts, styles.

There is geometry().

Layout is supported using setLayout() and layout().

Shortcuts and QActions using grabShortcut() and insertAction() are supported.

Unlike QGraphicsItem, QGraphicsWidget is not an abstract class.

2. Since QGraphicsWidget is similar to QWidget and has a similar API, it is easier to port widgets from QWidget to QGraphicsWidget than QGraphicsItem.

3. The Qt widget properties supported by QGraphicsWidget by default are as follows:

  • Qt::WA_RightToLeft: Indicates that the layout direction of the widget is from right to left.
  • Qt::WA_SetStyle: Indicates that the widget has its own style. If this property is set (set by setStyle()), the widget has been explicitly assigned a style. If not set, the widget will use the scene or application's style.
  • Qt::WA_Resized: Indicates that the widget has an explicit size.
  • Qt::WA_SetPalette: Indicates that the widget has its own palette.
  • Qt::WA_SetFont: Indicates that the widget has its own font.
  • Qt::WA_WindowPropagation: Causes the top-level window to inherit font, palette and locale from its parent.

4. Although QGraphicsWidget inherits from QObject and QGraphicsItem (both of which have their own system for managing child items), you should use the functions provided by QGraphicsItem (instead of QObject) to manage the relationship between parent and child items. The stacking order of graphics items and their ownership are controlled by the functions of QGraphicsItem.

Two, attribute members

1、autoFillBackground : bool

Whether to autofill the widget background. If enabled, this property will cause Qt to fill the widget's background before calling the paint() method. The colors used are defined by the QPalette::Window color role in the widget palette. QGraphicsWidget is always filled with QPalette::Window unless the WA_OpaquePaintEvent attribute (property) is set. By default this property is false.

2、focusPolicy : Qt::FocusPolicy

Policy for accepting keyboard focus (focus policy Qt::FocusPolicy).

If the widget handles keyboard events, keyboard focus must be enabled for the widget For example, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus).

If you enable focus policy (ie not Qt::NoFocus), QGraphicsWidget will automatically enable ItemIsFocusable flag (graphic item flag). Setting Qt::NoFocus on a widget will clear the ItemIsFocusable flag.

3、font: QFont

The widget's font. This property consists of explicitly defined font properties and properties implicitly inherited from the widget's parent. Therefore, font() can return a different font than the one set with setFont(). This scheme allows you to define a single entry 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 has no parent, it resolves its entries based on the scene. The widget then sends itself a FontChange() event and notifies all of its descendants so they can also resolve their fonts. By default, this property contains the application's default font.

4、geometry : QRectF

The geometry of the widget. Sets the geometry of the graphics item to a rectangle. Calling this function modifies the position and size of the graphic item. The graphic item is first moved and 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 to it, the layout will be activated.

5、layout : QGraphicsLayout*

The widget's layout. QGraphicsWidget takes ownership of the layout.

6、maximumSize : const QSizeF / minimumSize : const QSizeF / preferredSize : const QSizeF

The widget's max/min/preferred size.

7、palette : QPalette

The widget's palette. Palettes provide colors and brushes for color groups, defining the general appearance of a widget and its children. A QPalette consists of explicitly defined color groups and implicitly inherited groups from the widget's parent. Therefore, palette() can return a different palette than set by setPalette(). When a widget's palette changes, it resolves its entries against its parent widget, or against the scene if it has no parent. It then sends itself a PaletteChange() event and notifies all of its descendants so they can resolve their palettes too. By default, this property contains the application's default palette.

8、size : QSizeF

The size of the widget. This property only affects the widget's width and height. The widget's position and top-left corner are unaffected. Resizing the widget triggers the GraphicsSceneResize() event.

9、sizePolicy : QSizePolicy

size strategy.

10、windowFlags : Qt::WindowFlags

window sign. Window flags are a combination of a window type (such as Qt::Dialog) and several flags that provide hints about the window's behavior. By default, this property contains no window flags. Windows are panels, and the ItemIsPanel (graphic item flag) flag is automatically set if the Qt::Window flag is set. If the Qt::Window flag is cleared, the ItemIsPanel flag is also cleared. Note that the ItemIsPanel flag can be set independently of Qt::Window.

11、windowTitle : QString

window title. This property is only used for windows.

3. Member functions

1、void addAction(QAction *action) / void addActions(QList<QAction *> actions)

Add action to QAction list. QGraphicsWidgets have a list of QActions.

2、void adjustSize()

Resizes the widget to its effective preferred size (effectiveSizeHint(Qt::PreferredSize)(QGraphicsLayoutItem)). This function is called implicitly when the graphics item is displayed for the first time.

3、bool close()

Close the widget. Returns true if the widget was closed successfully, false otherwise. First send a QCloseEvent to the widget, which may or may not accept the event. Does nothing if the event is ignored. If the event is accepted, it will close the widget. If the widget has the Qt::WA_DeleteOnClose attribute set, it will be deleted.

4. [Virtual function] bool focusNextPrevChild(bool next)

Pressing Tab and Shift+Tab, looks for a new widget to give keyboard focus to, returns true if the new widget can be found otherwise returns false. If next is true, search forward otherwise search backward.

5、QGraphicsWidget *focusWidget()

This function returns a pointer to the widget if this widget, a child or descendant of this widget currently has input focus.

6. [Signal] void geometryChanged()

This signal is emitted whenever the geometry is changed in setGeometry().

7、void getWindowFrameMargins(qreal *left, qreal *top, qreal *right, qreal *bottom)

Get the widget's window margins.

8、int grabShortcut(const QKeySequence &sequence, Qt::ShortcutContext context = Qt::WindowShortcut)

Adds a shortcut key to Qt's shortcut system (QKeySequence is a class that encapsulates shortcut key information), which monitors a given key sequence in a given environment (parameter 2).

  • Qt::ShortcutContext: The environment in which the shortcut key is located.
  • Qt::WidgetShortcut: The shortcut is active when the parent widget has focus.
  • Qt::WidgetWithChildrenShortcut: Shortcut is active when the parent widget or any of its children has focus.
  • Qt::WindowShortcut: For widgets that are not part of a window (that is, the top-level widget and its children), the shortcut key applies to the scene.
  • Qt::ApplicationShortcut: The shortcut applies to the entire application.

9、void initStyleOption(QStyleOption *option)

Sets the widget style of the QGraphicsWidget object according to the style setting object (QStyleOption). The QStyleOption object has the following members:

 

By default, these members are used to set the following properties of QGraphicsWidget:

  • state & QStyle::State_Enabled:isEnabled()
  • state & QStyle::State_HasFocus:hasFocus()
  • state & QStyle::State_MouseOver:isUnderMouse()
  • direction:layoutDirection()
  • rect:rect().toRect()
  • palette:palette()
  • fontMetrics:QFontMetrics(font())

10、void insertAction(QAction *before, QAction *action) / void insertActions(QAction *before, QList<QAction *> actions)

Insert the action of parameter 2 before the action of parameter 1. Append after action list if before == nullptr or before is not an action of current widget.

11、bool isActiveWindow()

Returns true if the widget's window is in the active window, or if the widget has no window but is in the active scene (i.e. the scene that currently has focus). (An active window is a window that contains a child widget that currently has input focus or that itself has input focus)

 

12. [Virtual function] void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)

Called by the scene to draw the window border for the window in local coordinates. The basic implementation uses the current styles to render borders and title bars.

13、void removeAction(QAction *action)

Remove action.

14、void resize(qreal w, qreal h)

Reset width and height.

15、void setAttribute(Qt::WidgetAttribute attribute, bool on = true)

If on is true, this function enables the property, otherwise the property is disabled. practical attributes

16、void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)

Set the left, top, right, and bottom content margins. Layouts use content margins to define the placement of child widgets and layouts.

17、void setLayout(QGraphicsLayout *layout)

Set the layout. Any existing layout managers are removed before a new layout is assigned. QGraphicsWidget takes ownership of the layout.

18、[static] void setTabOrder(QGraphicsWidget * A, QGraphicsWidget * B)

Set the focus to move from A to B when the tab key is pressed.

Set focus a->b->c->d to move:

setTabOrder(a, b); // a -> b
setTabOrder(b, c); // a -> b -> c
setTabOrder(c, d); // a -> b -> c -> d

19、void setWindowFrameMargins(QMarginsF margins)

Set widget window margins. Default margins are provided by the style, depending on the current window flags. If you want to draw your own window styles, you can set your own frame margins to override the default margins.

20、bool testAttribute(Qt::WidgetAttribute attribute)

Returns whether the attribute is enabled for the widget.

21、void unsetWindowFrameMargins()

Resets the border margins to the default values ​​provided by the style.

Fourth, the event member function

1、void changeEvent(QEvent *event)

State change event. include:

  • QEvent::ActivationChange
  • QEvent::EnabledChange
  • QEvent::FontChange
  • QEvent::StyleChange
  • QEvent::PaletteChange
  • QEvent::ParentChange
  • QEvent::LayoutDirectionChange
  • QEvent::ContentsRectChange

2、void closeEvent(QCloseEvent *event)

Widget close event. The default implementation accepts events.

3、bool event(QEvent *event)

Handle the event. Can handle the following events:

  • QEvent::Polish
  • QEvent::GraphicsSceneMove: The position in the scene is changed.
  • QEvent::GraphicsSceneResize: The size of the scene is changed.
  • QEvent::Show: The widget is shown on the screen.
  • QEvent::Hide: The widget is hidden.
  • QEvent::PaletteChange: The palette is changed.
  • QEvent::FontChange: Font changed.
  • QEvent::EnabledChange: Enable state change.
  • QEvent::StyleChange: Style changed.
  • QEvent::LayoutDirectionChange: The layout direction changes.
  • QEvent::ContentsRectChange: content margin/range change.

4、void grabKeyboardEvent(QEvent *event)

Handle the QEvent::GrabKeyboard (graphic item gets keyboard grab) event.

5、void grabMouseEvent(QEvent *event)

Handle the QEvent::GrabMouse (graphic item gets mouse grab) event.

6、void hideEvent(QHideEvent *event)

Hide events. Calling QEvent::accept() or QEvent::ignore() on an event has no effect.

7、void moveEvent(QGraphicsSceneMoveEvent *event)

Mobile events. This event is only delivered when the graphics item itself is moved. Calling setTransform() or moving any graphics item's ancestors will not affect the graphics item's local position. Calling QEvent::accept() or QEvent::ignore() on an event has no effect.

8、void polishEvent()

This event is passed to the graphic item by the scene at a certain time point after the graphic item is constructed. The function of this event is that the graphic item performs the final initialization operation before displaying.

9、void resizeEvent(QGraphicsSceneResizeEvent *event)

Resize event. This event is only delivered when the widget is resized locally. Calling setTransform() on the widget or any of its ancestors or views does not affect the widget's native size. Calling QEvent::accept() or QEvent::ignore() on an event has no effect.

10、bool sceneEvent(QEvent *event)

Reimplemented from QGraphicsItem::sceneEvent(). The sceneEvent() implementation of QGraphicsWidget simply passes the event to QGraphicsWidget::event(). This function should not be reimplemented in subclasses of QGraphicsWidget.

11、void showEvent(QShowEvent *event)

Show events. This event is passed before the widget is shown. Calling QEvent::accept() or QEvent::ignore() on an event has no effect.

12、void ungrabKeyboardEvent(QEvent *event)

Handle the lost keyboard grab event QEvent::UngrabKeyboard.

13、void ungrabMouseEvent(QEvent *event)

Handle the lost mouse grab event QEvent::UngrabMouse.

14、bool windowFrameEvent(QEvent *event)

Handle events for the window frame. Its basic implementation provides support for default window frame interactions such as moving, resizing, etc.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QT embedded development, Quick module, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

QGraphicsLayoutItem (layout item)

1. Description

1. Graphics view frame layout item QGraphicsLayoutItem is an abstract class that defines a set of virtual functions for describing the size, size policy and size hint of any object arranged by QGraphicsLayout.

2. Subclassing QGraphicsLayoutItem: If you create a subclass of QGraphicsLayoutItem and reimplement its virtual functions, you can enable layout to adjust the size and position of items and other QGraphicsLayoutItems, including QGraphicsWidget and QGraphicsLayout.

3. Nested QGraphicsLayout items: QGraphicsLayoutItems can be nested in other QGraphicsLayoutItems, similar to layouts that can contain sub-layouts. This is done by passing a QGraphicsLayoutItem pointer to QGraphicsLayoutItem's constructor or by calling setParentLayoutItem().

2. Member functions

1、QRectF contentsRect()

Returns the context rectangle extents in local coordinates. Use this function to define a rectangular extent when laying out layout items.

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

Returns the effective size hint for this layout item.

3、QRectF geometry()

Returns the geometry of a layout item. This function is equivalent to QRectF(pos(), size()).

4、[virtual] void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom)

Get the left, top, right and bottom content margins. The default implementation assumes all content margins are 0.

5、QGraphicsItem *graphicsItem() / void setGraphicsItem(QGraphicsItem *item)

the QGraphicsItem represented by this layout item.

6、bool isLayout()

Returns true if this QGraphicsLayoutItem is a layout (QGraphicsLayout also inherits QGraphicsLayoutItem).

7、bool ownedByLayout() / void setOwnedByLayout(bool ownership)

Whether the layout should delete the current layout item in its destructor.

If true, the layout will remove it. If false, another object is assumed to have ownership of it, and the layout will not delete the layout item.

If the layout item inherits both QGraphicsItem and QGraphicsLayoutItem (such as QGraphicsWidget), the layout item is actually contained in two ownership hierarchies, and who should destroy the layout item can be customized by the user. This property informs the layout what should be done to its children when destroyed. For QGraphicsWidget it is preferable that its children are not deleted when the layout is deleted (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 (it manages graphics items outside of the QGraphicsItem hierarchy), so the parent layout should remove it.

8、QGraphicsLayoutItem *parentLayoutItem() / void setParentLayoutItem(QGraphicsLayoutItem *parent)

Returns the parent of this QGraphicsLayoutItem, or nullptr if there is no parent, or if the parent does not inherit from QGraphicsLayoutItem.

9、[virtual] void setGeometry(const QRectF &rect)

Sets the geometry of a layout item. It's in parent coordinates (eg, the upper left corner of the rect is equivalent to the item's position in parent coordinates). This function must be reimplemented in subclasses of QGraphicsLayoutItem. The layout will call this function when it rearranges. If the rect is outside the range of minimumSize and maximumSize, it will be resized to the nearest size so that it is within the legal range.

10、setMaximumHeight()、setMaximumSize()、setMaximumWidth()、setMinimumHeight()、setMinimumSize()、setMinimumWidth()

Sets the maximum and minimum dimensions of a layout item.

The minimum size overrides sizeHint(Qt::MinimumSize) and provides a default value for effectiveSizeHint(Qt::MinimumSize). To unset the minimum size, use an invalid size (-1).

The maximum size overrides sizeHint(Qt::MaximumSize) and provides a default value for effectiveSizeHint(Qt::MaximumSize). To unset the maximum size, use an invalid size (-1).

11、setPreferredHeight()、setPreferredSize()、setPreferredWidth()

Sets the preferred size for layout items. The preferred size overrides sizeHint(Qt::PreferredSize) and provides a default value for effectiveSizeHint(Qt::PreferredSize). To unset the preferred size, use an invalid size (-1).

12、void setSizePolicy(const QSizePolicy &policy) / QSizePolicy sizePolicy()

Set the size policy to policy. A size policy describes how items should grow horizontally and vertically when arranged in a layout.

The default size policy for layout items is (QSizePolicy::Fixed, QSizePolicy::Fixed).

13. [Pure virtual function] QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF())

Returns a size hint for a layout item, using constraints width and height.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QT embedded development, Quick module, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Copyright statement: This article is the original article of CSDN blogger "Friendly, friend"

Original link: https://blog.csdn.net/kenfan1647/article/details/117403792

Guess you like

Origin blog.csdn.net/hw5230/article/details/131872450