内容表

上一话题

QGraphicsView

下一话题

QGridLayout

QGraphicsWidget

QGraphicsWidget class is the base class for all widget items in a QGraphicsScene . 更多

Inheritance diagram of PySide2.QtWidgets.QGraphicsWidget

继承者: QtCharts.QChart , QtCharts.QLegend , QtCharts.QPolarChart , QGraphicsProxyWidget

概要

函数

虚函数

静态函数

详细描述

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

不像 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 使用 QGraphicsProxyWidget .

Noticeable differences between QGraphicsWidget and QWidget 是:

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 style

A top-level widget’s style defaults to 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, ( WidgetAttribute ), as shown in the table below. Any attributes not listed in this table are unsupported, or otherwise unused.

Widget Attribute

用法

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.

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.

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.

WA_Resized

Set by setGeometry() and resize() .

WA_SetPalette

Set by setPalette() .

WA_SetFont

Set by setFont() .

WA_WindowPropagation

Enables propagation to window widgets.

尽管 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.

注意

parent() should always return None for QGraphicsWidgets, but this policy is not strictly defined.

class QGraphicsWidget ( [ parent=None [ , wFlags=Qt.WindowFlags() ] ] )
param parent

QGraphicsItem

param wFlags

WindowFlags

构造 QGraphicsWidget instance. The optional parent 自变量会被传递给 QGraphicsItem ‘s constructor. The optional wFlags argument specifies the widget’s window flags (e.g., whether the widget should be a window, a tool, a popup, etc).

PySide2.QtWidgets.QGraphicsWidget. actions ( )
返回类型

返回此 Widget 的动作列表 (可能为空)。

另请参阅

insertAction() removeAction() actions() associatedWidgets() associatedGraphicsWidgets()

PySide2.QtWidgets.QGraphicsWidget. addAction ( action )
参数

action QAction

追加动作 action 到此 Widget 的动作列表。

All QGraphicsWidgets have a list of QAction ,无论如何,可以按多种不同方式图形表示它们。默认使用 QAction 列表 (返回通过 actions() ) is to create a context QMenu .

A QGraphicsWidget 只应每动作有一个,且已添加动作不会导致同一动作在 Widget 中出现两次。

PySide2.QtWidgets.QGraphicsWidget. addActions ( actions )
参数

actions

追加动作 actions 到此 Widget 的动作列表。

PySide2.QtWidgets.QGraphicsWidget. adjustSize ( )

Adjusts the size of the widget to its effective preferred size hint.

This function is called implicitly when the item is shown for the first time.

另请参阅

effectiveSizeHint() MinimumSize

PySide2.QtWidgets.QGraphicsWidget. autoFillBackground ( )
返回类型

bool

PySide2.QtWidgets.QGraphicsWidget. changeEvent ( event )
参数

event QEvent

此事件处理程序可以被重实现以处理状态改变。

The state being changed in this event can be retrieved through event .

改变事件包括: ActivationChange , EnabledChange , FontChange , StyleChange , PaletteChange , ParentChange , LayoutDirectionChange ,和 ContentsRectChange .

PySide2.QtWidgets.QGraphicsWidget. close ( )
返回类型

bool

Call this function to close the widget.

返回 true 若 Widget 被关闭;否则返回 false . This slot will first send a QCloseEvent to the widget, which may or may not accept the event. If the event was ignored, nothing happens. If the event was accepted, it will hide() the widget.

若 Widget 拥有 WA_DeleteOnClose attribute set it will be deleted.

PySide2.QtWidgets.QGraphicsWidget. closeEvent ( event )
参数

event QCloseEvent

This event handler, for event , can be reimplemented in a subclass to receive widget close events. The default implementation accepts the event.

PySide2.QtWidgets.QGraphicsWidget. focusNextPrevChild ( next )
参数

next bool

返回类型

bool

查找新的 Widget 以给予键盘聚焦,如适合 Tab 和 Shift+Tab,并返回 true if it can find a new widget; returns false otherwise. If next is true, this function searches forward; if next 为 False,向后搜索。

Sometimes, you will want to reimplement this function to provide special focus handling for your widget and its subwidgets. For example, a web browser might reimplement it to move its current active link forward or backward, and call the base implementation only when it reaches the last or first link on the page.

Child widgets call on their parent widgets, but only the window that contains the child widgets decides where to redirect focus. By reimplementing this function for an object, you gain control of focus traversal for all child widgets.

另请参阅

focusPolicy()

PySide2.QtWidgets.QGraphicsWidget. focusPolicy ( )
返回类型

FocusPolicy

另请参阅

setFocusPolicy()

PySide2.QtWidgets.QGraphicsWidget. focusWidget ( )
返回类型

QGraphicsWidget

If this widget, a child or descendant of this widget currently has input focus, this function will return a pointer to that widget. If no descendant widget has input focus, None 被返回。

PySide2.QtWidgets.QGraphicsWidget. font ( )
返回类型

QFont

另请参阅

setFont()

PySide2.QtWidgets.QGraphicsWidget. geometryChanged ( )
PySide2.QtWidgets.QGraphicsWidget. getWindowFrameMargins ( )

Gets the widget’s window frame margins. The margins are stored in left , top , right and bottom as pointers to qreals. Each argument can be omitted by passing None .

PySide2.QtWidgets.QGraphicsWidget. grabKeyboardEvent ( event )
参数

event QEvent

This event handler, for event , can be reimplemented in a subclass to receive notifications for GrabKeyboard 事件。

另请参阅

grabKeyboard() grabMouse()

PySide2.QtWidgets.QGraphicsWidget. grabMouseEvent ( event )
参数

event QEvent

This event handler, for event , can be reimplemented in a subclass to receive notifications for GrabMouse 事件。

另请参阅

grabMouse() grabKeyboard()

PySide2.QtWidgets.QGraphicsWidget. grabShortcut ( sequence [ , context=Qt.WindowShortcut ] )
参数
  • sequence QKeySequence

  • context ShortcutContext

返回类型

int

Adds a shortcut to Qt’s shortcut system that watches for the given key sequence 以给定 context 。若 context is ApplicationShortcut ,快捷方式适用于整个应用程序。否则,要么本地到此 Widget, WidgetShortcut ,或到窗口本身, WindowShortcut . For widgets that are not part of a window (i.e., top-level widgets and their children), WindowShortcut shortcuts apply to the scene.

If the same key sequence has been grabbed by several widgets, when the key sequence occurs a Shortcut event is sent to all the widgets to which it applies in a non-deterministic order, but with the ``ambiguous’’ flag set to true.

警告

通常不需要使用此函数;而是创建 QAction s with the shortcut key sequences you require (if you also want equivalent menu options and toolbar buttons), or create QShortcut s if you just need key sequences. Both QAction and QShortcut handle all the event filtering for you, and provide signals which are triggered when the user triggers the key sequence, so are much easier to use than this low-level function.

PySide2.QtWidgets.QGraphicsWidget. hideEvent ( event )
参数

event QHideEvent

This event handler, for Hide events, is delivered after the widget has been hidden, for example, setVisible (false) has been called for the widget or one of its ancestors when the widget was previously shown.

You can reimplement this event handler to detect when your widget is hidden. Calling accept() or ignore() on event 不起作用。

另请参阅

showEvent() hideEvent() ItemVisibleChange

PySide2.QtWidgets.QGraphicsWidget. initStyleOption ( option )
参数

option QStyleOption

Populates a style option object for this widget based on its current state, and stores the output in option . The default implementation populates option with the following properties.

Style Option Property

state & State_Enabled

Corresponds to isEnabled() .

state & State_HasFocus

Corresponds to hasFocus() .

state & State_MouseOver

Corresponds to isUnderMouse() .

direction

Corresponds to layoutDirection() .

rect

Corresponds to rect() .toRect().

palette

Corresponds to palette() .

fontMetrics

Corresponds to QFontMetrics ( font() ).

子类化的 QGraphicsWidget should call the base implementation, and then test the type of option 使用 qstyleoption_cast <>() or test Type before storing widget-specific options.

例如:

class MyGroupBoxWidget (QStyleOptionGroupBox):
    # ...
    def initStyleOption(option):
        QGraphicsWidget.initStyleOption(self, option)
        if isinstance(option, QStyleOptionGroupBox):
            # Add group box specific state.
            box.flat = isFlat()
            # ...
											

另请参阅

initFrom()

PySide2.QtWidgets.QGraphicsWidget. insertAction ( before , action )
参数

插入动作 action to this widget’s list of actions, before the action before 。它追加动作,若 before is None or before 不是有效动作 (对于此 Widget)。

A QGraphicsWidget 每动作只应有一个。

PySide2.QtWidgets.QGraphicsWidget. insertActions ( before , actions )
参数

插入动作 actions to this widget’s list of actions, before the action before 。它追加动作,若 before is None or before 不是有效动作 (对于此 Widget)。

A QGraphicsWidget 每动作最多能有一个。

PySide2.QtWidgets.QGraphicsWidget. isActiveWindow ( )
返回类型

bool

返回 true if this widget’s window is in the active window, or if the widget does not have a window but is in an active scene (i.e., a scene that currently has focus).

The active window is the window that either contains a child widget that currently has input focus, or that itself has input focus.

PySide2.QtWidgets.QGraphicsWidget. layout ( )
返回类型

QGraphicsLayout

Returns this widget’s layout, or None if no layout is currently managing this widget.

另请参阅

setLayout()

PySide2.QtWidgets.QGraphicsWidget. layoutChanged ( )
PySide2.QtWidgets.QGraphicsWidget. layoutDirection ( )
返回类型

LayoutDirection

PySide2.QtWidgets.QGraphicsWidget. moveEvent ( event )
参数

event QGraphicsSceneMoveEvent

This event handler, for GraphicsSceneMove events, is delivered after the widget has moved (e.g., its local position has changed).

This event is only delivered when the item is moved locally. Calling setTransform() or moving any of the item’s ancestors does not affect the item’s local position.

You can reimplement this event handler to detect when your widget has moved. Calling accept() or ignore() on event 不起作用。

另请参阅

ItemPositionChange ItemPositionHasChanged

PySide2.QtWidgets.QGraphicsWidget. paintWindowFrame ( painter , option [ , widget=None ] )
参数

此虚拟函数被调用由 QGraphicsScene to draw the window frame for windows using painter , option ,和 widget , in local coordinates. The base implementation uses the current style to render the frame and title bar.

You can reimplement this function in a subclass of QGraphicsWidget to provide custom rendering of the widget’s window frame.

另请参阅

paint()

PySide2.QtWidgets.QGraphicsWidget. palette ( )
返回类型

QPalette

另请参阅

setPalette()

PySide2.QtWidgets.QGraphicsWidget. polishEvent ( )

This event is delivered to the item by the scene at some point after it has been constructed, but before it is shown or otherwise accessed through the scene. You can use this event handler to do last-minute initializations of the widget which require the item to be fully constructed.

The base implementation does nothing.

PySide2.QtWidgets.QGraphicsWidget. propertyChange ( propertyName , value )
参数
  • propertyName – unicode

  • value – object

返回类型

object

This virtual function is used to notify changes to any property (both dynamic properties, and registered with Q_PROPERTY ) in the widget. Depending on the property itself, the notification can be delivered before or after the value has changed.

propertyName is the name of the property (e.g., “size” or “font”), and value is the (proposed) new value of the property. The function returns the new value, which may be different from value if the notification supports adjusting the property value. The base implementation simply returns value for any propertyName .

QGraphicsWidget delivers notifications for the following properties:

propertyName

特性

layoutDirection

layoutDirection

size

size

font

font

palette

palette

另请参阅

itemChange()

PySide2.QtWidgets.QGraphicsWidget. rect ( )
返回类型

QRectF

Returns the item’s local rect as a QRectF . This function is equivalent to QRectF (QPointF(), size() ).

PySide2.QtWidgets.QGraphicsWidget. releaseShortcut ( id )
参数

id int

删除快捷方式采用给定 id from Qt’s shortcut system. The widget will no longer receive Shortcut events for the shortcut’s key sequence (unless it has other shortcuts with the same key sequence).

警告

You should not normally need to use this function since Qt’s shortcut system removes shortcuts automatically when their parent widget is destroyed. It is best to use QAction or QShortcut to handle shortcuts, since they are easier to use than this low-level function. Note also that this is an expensive operation.

PySide2.QtWidgets.QGraphicsWidget. removeAction ( action )
参数

action QAction

移除动作 action from this widget’s list of actions.

PySide2.QtWidgets.QGraphicsWidget. resize ( size )
参数

size QSizeF

PySide2.QtWidgets.QGraphicsWidget. resize ( w , h )
参数
  • w qreal

  • h qreal

这是重载函数。

Constructs a resize with the given width ( w ) and height ( h ). This convenience function is equivalent to calling resize( QSizeF (w, h)).

另请参阅

setGeometry() setTransform()

PySide2.QtWidgets.QGraphicsWidget. resizeEvent ( event )
参数

event QGraphicsSceneResizeEvent

This event handler, for GraphicsSceneResize events, is delivered after the widget has been resized (i.e., its local size has changed). event contains both the old and the new size.

This event is only delivered when the widget is resized locally; calling setTransform() on the widget or any of its ancestors or view, does not affect the widget’s local size.

You can reimplement this event handler to detect when your widget has been resized. Calling accept() or ignore() on event 不起作用。

PySide2.QtWidgets.QGraphicsWidget. setAttribute ( attribute [ , on=true ] )
参数
  • attribute WidgetAttribute

  • on bool

on is true, this function enables attribute ;否则 attribute 是禁用的。

See the class documentation for QGraphicsWidget for a complete list of which attributes are supported, and what they are for.

PySide2.QtWidgets.QGraphicsWidget. setAutoFillBackground ( enabled )
参数

enabled bool

PySide2.QtWidgets.QGraphicsWidget. setContentsMargins ( margins )
参数

margins QMarginsF

Sets the widget’s contents margins to margins .

Contents margins are used by the assigned layout to define the placement of subwidgets and layouts. Margins are particularly useful for widgets that constrain subwidgets to only a section of its own geometry. For example, a group box with a layout will place subwidgets inside its frame, but below the title.

Changing a widget’s contents margins will always trigger an update() , and any assigned layout will be activated automatically. The widget will then receive a ContentsRectChange 事件。

另请参阅

getContentsMargins() setGeometry()

PySide2.QtWidgets.QGraphicsWidget. setContentsMargins ( left , top , right , bottom )
参数
  • left qreal

  • top qreal

  • right qreal

  • bottom qreal

这是重载函数。

Sets the widget’s contents margins to left , top , right and bottom .

PySide2.QtWidgets.QGraphicsWidget. setFocusPolicy ( policy )
参数

policy FocusPolicy

另请参阅

focusPolicy()

PySide2.QtWidgets.QGraphicsWidget. setFont ( font )
参数

font QFont

另请参阅

font()

PySide2.QtWidgets.QGraphicsWidget. setGeometry ( x , y , w , h )
参数
  • x qreal

  • y qreal

  • w qreal

  • h qreal

This convenience function is equivalent to calling setGeometry ( QRectF ( x , y , w , h )).

PySide2.QtWidgets.QGraphicsWidget. setLayout ( layout )
参数

layout QGraphicsLayout

Sets the layout for this widget to layout . Any existing layout manager is deleted before the new layout is assigned. If layout is None , the widget is left without a layout. Existing subwidgets’ geometries will remain unaffected.

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.

QGraphicsWidget takes ownership of layout .

PySide2.QtWidgets.QGraphicsWidget. setLayoutDirection ( direction )
参数

direction LayoutDirection

另请参阅

layoutDirection()

PySide2.QtWidgets.QGraphicsWidget. setPalette ( palette )
参数

palette QPalette

另请参阅

palette()

PySide2.QtWidgets.QGraphicsWidget. setShortcutAutoRepeat ( id [ , enabled=true ] )
参数
  • id int

  • enabled bool

enabled is true, auto repeat of the shortcut with the given id 被启用;否则它被禁用。

PySide2.QtWidgets.QGraphicsWidget. setShortcutEnabled ( id [ , enabled=true ] )
参数
  • id int

  • enabled bool

enabled 为 True,快捷方式采用给定 id 被启用;否则快捷方式被禁用。

警告

You should not normally need to use this function since Qt’s shortcut system enables/disables shortcuts automatically as widgets become hidden/visible and gain or lose focus. It is best to use QAction or QShortcut to handle shortcuts, since they are easier to use than this low-level function.

PySide2.QtWidgets.QGraphicsWidget. setStyle ( style )
参数

style QStyle

Sets the widget’s style to style . QGraphicsWidget does not take ownership of style .

If no style is assigned, or style is None , the widget will use style() (if this has been set). Otherwise the widget will use style() .

此函数设置 WA_SetStyle attribute if style 不是 None ; otherwise it clears the attribute.

另请参阅

style()

static PySide2.QtWidgets.QGraphicsWidget. setTabOrder ( first , second )
参数

Moves the second widget around the ring of focus widgets so that keyboard focus moves from the first widget to the second widget when the Tab key is pressed.

Note that since the tab order of the second widget is changed, you should order a chain like this:

setTabOrder(a, b) # a to b
setTabOrder(b, c) # a to b to c
setTabOrder(c, d) # a to b to c to d
											

not 像这样:

# WRONG
setTabOrder(c, d) # c to d
setTabOrder(a, b) # a to b AND c to d
setTabOrder(b, c) # a to b to c, but not c to d
											

first is None , this indicates that second should be the first widget to receive input focus should the scene gain Tab focus (i.e., the user hits Tab so that focus passes into the scene). If second is None , this indicates that first should be the first widget to gain focus if the scene gained BackTab focus.

By default, tab order is defined implicitly using widget creation order.

PySide2.QtWidgets.QGraphicsWidget. setWindowFlags ( wFlags )
参数

wFlags WindowFlags

另请参阅

windowFlags()

PySide2.QtWidgets.QGraphicsWidget. setWindowFrameMargins ( margins )
参数

margins QMarginsF

Sets the widget’s window frame margins to margins . The default frame margins are provided by the style, and they depend on the current window flags.

If you would like to draw your own window decoration, you can set your own frame margins to override the default margins.

PySide2.QtWidgets.QGraphicsWidget. setWindowFrameMargins ( left , top , right , bottom )
参数
  • left qreal

  • top qreal

  • right qreal

  • bottom qreal

这是重载函数。

Sets the widget’s window frame margins to left , top , right and bottom .

PySide2.QtWidgets.QGraphicsWidget. setWindowTitle ( title )
参数

title – unicode

另请参阅

windowTitle()

PySide2.QtWidgets.QGraphicsWidget. showEvent ( event )
参数

event QShowEvent

This event handler, for Show events, is delivered before the widget has been shown, for example, setVisible (true) has been called for the widget or one of its ancestors when the widget was previously hidden.

You can reimplement this event handler to detect when your widget is shown. Calling accept() or ignore() on event 不起作用。

另请参阅

hideEvent() showEvent() ItemVisibleChange

PySide2.QtWidgets.QGraphicsWidget. size ( )
返回类型

QSizeF

PySide2.QtWidgets.QGraphicsWidget. style ( )
返回类型

QStyle

Returns a pointer to the widget’s style. If this widget does not have any explicitly assigned style, the scene’s style is returned instead. In turn, if the scene does not have any assigned style, this function returns style() .

另请参阅

setStyle()

PySide2.QtWidgets.QGraphicsWidget. testAttribute ( attribute )
参数

attribute WidgetAttribute

返回类型

bool

返回 true if attribute is enabled for this widget; otherwise, returns false .

另请参阅

setAttribute()

PySide2.QtWidgets.QGraphicsWidget. ungrabKeyboardEvent ( event )
参数

event QEvent

This event handler, for event , can be reimplemented in a subclass to receive notifications for UngrabKeyboard 事件。

另请参阅

ungrabKeyboard() ungrabMouse()

PySide2.QtWidgets.QGraphicsWidget. ungrabMouseEvent ( event )
参数

event QEvent

This event handler, for event , can be reimplemented in a subclass to receive notifications for UngrabMouse 事件。

另请参阅

ungrabMouse() ungrabKeyboard()

PySide2.QtWidgets.QGraphicsWidget. unsetLayoutDirection ( )
PySide2.QtWidgets.QGraphicsWidget. unsetWindowFrameMargins ( )

Resets the window frame margins to the default value, provided by the style.

PySide2.QtWidgets.QGraphicsWidget. windowFlags ( )
返回类型

WindowFlags

另请参阅

setWindowFlags()

PySide2.QtWidgets.QGraphicsWidget. windowFrameEvent ( e )
参数

e QEvent

返回类型

bool

This event handler, for event , receives events for the window frame if this widget is a window. Its base implementation provides support for default window frame interaction such as moving, resizing, etc.

You can reimplement this handler in a subclass of QGraphicsWidget to provide your own custom window frame interaction support.

返回 true if event 已被识别并处理;否则,返回 false .

另请参阅

event()

PySide2.QtWidgets.QGraphicsWidget. windowFrameGeometry ( )
返回类型

QRectF

Returns the widget’s geometry in parent coordinates including any window frame.

PySide2.QtWidgets.QGraphicsWidget. windowFrameRect ( )
返回类型

QRectF

Returns the widget’s local rect including any window frame.

PySide2.QtWidgets.QGraphicsWidget. windowFrameSectionAt ( pos )
参数

pos QPointF

返回类型

WindowFrameSection

Returns the window frame section at position pos ,或 NoSection if there is no window frame section at this position.

This function is used in QGraphicsWidget ‘s base implementation for window frame interaction.

You can reimplement this function if you want to customize how a window can be interactively moved or resized. For instance, if you only want to allow a window to be resized by the bottom right corner, you can reimplement this function to return NoSection for all sections except BottomRightSection .

PySide2.QtWidgets.QGraphicsWidget. windowTitle ( )
返回类型

unicode

另请参阅

setWindowTitle()

PySide2.QtWidgets.QGraphicsWidget. windowType ( )
返回类型

WindowType

Returns the widgets window type.

另请参阅

windowFlags() isWindow() isPanel()