QGraphicsWidget 类是基类用于所有 Widget 项在 QGraphicsScene . 更多...
头: | #include <QGraphicsWidget> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
继承: | QGraphicsObject and QGraphicsLayoutItem |
继承者: |
enum | anonymous { Type } |
|
|
QGraphicsWidget (QGraphicsItem * parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags()) | |
virtual | ~QGraphicsWidget () |
QList<QAction *> | actions () const |
void | addAction (QAction * action ) |
void | addActions (const QList<QAction *> & actions ) |
void | adjustSize () |
bool | autoFillBackground () const |
Qt::FocusPolicy | focusPolicy () const |
QGraphicsWidget * | focusWidget () const |
QFont | font () const |
void | getWindowFrameMargins (qreal * left , qreal * top , qreal * right , qreal * bottom ) const |
int | grabShortcut (const QKeySequence & sequence , Qt::ShortcutContext context = Qt::WindowShortcut) |
void | insertAction (QAction * before , QAction * action ) |
void | insertActions (QAction * before , const QList<QAction *> & actions ) |
bool | isActiveWindow () const |
QGraphicsLayout * | layout () const |
Qt::LayoutDirection | layoutDirection () const |
virtual void | paintWindowFrame (QPainter * painter , const QStyleOptionGraphicsItem * option , QWidget * widget = nullptr) |
QPalette | palette () const |
QRectF | rect () const |
void | releaseShortcut (int id ) |
void | removeAction (QAction * action ) |
void | resize (const QSizeF & size ) |
void | resize (qreal w , qreal h ) |
void | setAttribute (Qt::WidgetAttribute 属性 , bool on = true) |
void | setAutoFillBackground (bool enabled ) |
void | setContentsMargins (QMarginsF margins ) |
void | setContentsMargins (qreal left , qreal top , qreal right , qreal bottom ) |
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 | setShortcutAutoRepeat (int id , bool enabled = true) |
void | setShortcutEnabled (int id , bool enabled = true) |
void | setStyle (QStyle * style ) |
void | setWindowFlags (Qt::WindowFlags wFlags ) |
void | setWindowFrameMargins (QMarginsF margins ) |
void | setWindowFrameMargins (qreal left , qreal top , qreal right , qreal bottom ) |
void | setWindowTitle (const QString & title ) |
QSizeF | size () const |
QStyle * | style () const |
bool | testAttribute (Qt::WidgetAttribute 属性 ) const |
void | unsetLayoutDirection () |
void | unsetWindowFrameMargins () |
Qt::WindowFlags | windowFlags () const |
QRectF | windowFrameGeometry () const |
QRectF | windowFrameRect () const |
QString | windowTitle () const |
Qt::WindowType | windowType () const |
virtual QRectF | boundingRect () const override |
virtual void | getContentsMargins (qreal * left , qreal * top , qreal * right , qreal * bottom ) const override |
virtual void | paint (QPainter * painter , const QStyleOptionGraphicsItem * option , QWidget * widget = nullptr) override |
virtual void | setGeometry (const QRectF & rect ) override |
virtual QPainterPath | shape () const override |
virtual int | type () const override |
bool | close () |
void | geometryChanged () |
void | layoutChanged () |
void | setTabOrder (QGraphicsWidget * first , QGraphicsWidget * second ) |
virtual void | changeEvent (QEvent * event ) |
virtual void | closeEvent (QCloseEvent * event ) |
virtual bool | focusNextPrevChild (bool next ) |
virtual void | grabKeyboardEvent (QEvent * event ) |
virtual void | grabMouseEvent (QEvent * event ) |
virtual void | hideEvent (QHideEvent * event ) |
virtual void | initStyleOption (QStyleOption * option ) const |
virtual void | moveEvent (QGraphicsSceneMoveEvent * event ) |
virtual void | polishEvent () |
virtual void | resizeEvent (QGraphicsSceneResizeEvent * event ) |
virtual void | showEvent (QShowEvent * event ) |
virtual void | ungrabKeyboardEvent (QEvent * event ) |
virtual void | ungrabMouseEvent (QEvent * event ) |
virtual bool | windowFrameEvent (QEvent * event ) |
virtual Qt::WindowFrameSection | windowFrameSectionAt (const QPointF & pos ) const |
virtual bool | event (QEvent * event ) override |
virtual void | focusInEvent (QFocusEvent * event ) override |
virtual void | focusOutEvent (QFocusEvent * event ) override |
virtual void | hoverLeaveEvent (QGraphicsSceneHoverEvent * event ) override |
virtual void | hoverMoveEvent (QGraphicsSceneHoverEvent * event ) override |
virtual QVariant | itemChange (QGraphicsItem::GraphicsItemChange change , const QVariant & value ) override |
virtual bool | sceneEvent (QEvent * event ) override |
virtual QSizeF | sizeHint (Qt::SizeHint which , const QSizeF & constraint = QSizeF()) const override |
virtual void | updateGeometry () override |
QGraphicsWidget 是扩展基项,提供的额外功能超过 QGraphicsItem 。类似于 QWidget 在很多方面:
不像 QGraphicsItem ,QGraphicsWidget 不是抽象类;可以创建 QGraphicsWidget 实例,不必子类化它。此方式对仅将子级 Widget 组织到布局的服务目的小部件很有用。
QGraphicsWidget 可以用作自己的自定义项的基项,若要求处理高级输入聚焦,如:Tab 聚焦和激活,或布局。
由于 QGraphicsWidget 类似于 QWidget 且拥有同样的 API,更容易于移植 Widget 从 QWidget 到 QGraphicsWidget,而不是 QGraphicsItem .
注意: QWidget 基 Widget 可以直接嵌入 QGraphicsScene 使用 QGraphicsProxyWidget .
明显差异在 QGraphicsWidget 和 QWidget 是:
QGraphicsWidget | QWidget |
---|---|
坐标和几何体按 qreal 定义 (双精度或浮点数,从属平台)。 | QWidget 使用整数几何体 ( QPoint , QRect ). |
小部件默认情况下已可见;不必调用 show () 来显示 Widget。 | QWidget 默认情况下是隐藏的,直到调用 show (). |
支持 Widget 属性子集。 | 支持所有 Widget 属性。 |
顶层项的样式默认为 QGraphicsScene::style | 顶层 Widget 的样式默认为 QApplication::style |
Graphics View 提供自定义拖放框架,不同于 QWidget . | 标准拖放框架。 |
Widget 项不支持模态。 | 完整模态支持。 |
QGraphicsWidget 支持 Qt 小部件属性子集 ( Qt::WidgetAttribute ),如下表所示。此表中未列出的任何属性,不受支持或不使用。
Widget 属性 | 用法 |
---|---|
Qt::WA_SetLayoutDirection | 设置通过 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 | 设置通过 setGeometry () 和 resize (). |
Qt::WA_SetPalette | 设置通过 setPalette (). |
Qt::WA_SetFont | 设置通过 setFont (). |
Qt::WA_WindowPropagation | 允许传播到窗口 Widget。 |
尽管 QGraphicsWidget 继承自 QObject and QGraphicsItem ,应使用函数提供通过 QGraphicsItem , not QObject ,来管理父级和子级项之间的关系。这些函数控制项的堆叠次序及它们的所有权。
注意:
The
QObject::parent
() 应始终返回
nullptr
对于 QGraphicsWidgets,但此策略未严格定义。
另请参阅 QGraphicsProxyWidget , QGraphicsItem ,和 Widget 和布局 .
值的返回通过虚拟 type () 函数。
常量 | 值 | 描述 |
---|---|---|
QGraphicsWidget::Type
|
11
|
图形 Widget 项 |
此特性保持是否自动填充 Widget 背景
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 颜色角色来自 Widget 的 palette .
此外,填充窗口总是采用 QPalette::Window ,除非有设置 WA_OpaquePaintEvent (或 WA_NoSystemBackground) 属性。
默认情况下,此特性为
false
.
访问函数:
bool | autoFillBackground () const |
void | setAutoFillBackground (bool enabled ) |
另请参阅 Qt::WA_OpaquePaintEvent and Qt::WA_NoSystemBackground .
此特性保持 Widget 接受键盘聚焦的方式
The focus policy is Qt::TabFocus 若 Widget 通过 Tab 键接受键盘聚焦, Qt::ClickFocus 若 Widget 通过点击接受聚焦, Qt::StrongFocus 若它接受两者,而 Qt::NoFocus (默认) 若它根本不接受聚焦。
必须为 Widget 启用键盘聚焦,若它处理键盘事件。通常这是由 Widget 构造函数完成的。例如, QLineEdit 构造函数调用 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.
访问函数:
Qt::FocusPolicy | focusPolicy () const |
void | setFocusPolicy (Qt::FocusPolicy policy ) |
另请参阅 focusInEvent (), focusOutEvent (), keyPressEvent (), keyReleaseEvent (),和 enabled .
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.
默认情况下,此特性包含应用程序默认字体。
访问函数:
QFont | font () const |
void | setFont (const QFont & font ) |
另请参阅 QApplication::font (), QGraphicsScene::font ,和 QFont::resolve ().
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.
访问函数:
virtual void | setGeometry (const QRectF & rect ) override |
void | setGeometry (qreal x , qreal y , qreal w , qreal h ) |
通知程序信号:
void | geometryChanged () |
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 拥有所有权对于 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
.
访问函数:
QGraphicsLayout * | layout () const |
void | setLayout (QGraphicsLayout * layout ) |
通知程序信号:
void | layoutChanged () |
此特性保持此 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 属性。
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.
访问函数:
Qt::LayoutDirection | layoutDirection () const |
void | setLayoutDirection (Qt::LayoutDirection direction ) |
void | unsetLayoutDirection () |
另请参阅 QWidget::layoutDirection and QApplication::layoutDirection .
This property holds the maximum size of the widget
另请参阅 setMaximumSize (), maximumSize (), minimumSize ,和 preferredSize .
This property holds the minimum size of the widget
另请参阅 setMinimumSize (), minimumSize (), preferredSize ,和 maximumSize .
此特性保持 Widget 的调色板
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.
访问函数:
QPalette | palette () const |
void | setPalette (const QPalette & palette ) |
另请参阅 QGuiApplication::palette (), QGraphicsScene::palette ,和 QPalette::resolve ().
This property holds the preferred size of the widget
另请参阅 setPreferredSize (), preferredSize (), minimumSize ,和 maximumSize .
This property holds the size of the widget
Calling resize() resizes the widget to a size bounded by minimumSize () 和 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.
默认情况下,此特性包含 0 大小宽度和高度。
访问函数:
QSizeF | size () const |
void | resize (const QSizeF & size ) |
void | resize (qreal w , qreal h ) |
通知程序信号:
void | geometryChanged () |
另请参阅 setGeometry (), QGraphicsSceneResizeEvent ,和 QGraphicsLayout .
This property holds the size policy for the widget
另请参阅 sizePolicy (), setSizePolicy (),和 QWidget::sizePolicy ().
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 .
访问函数:
Qt::WindowFlags | windowFlags () const |
void | setWindowFlags (Qt::WindowFlags wFlags ) |
另请参阅 isWindow () 和 isPanel ().
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.
访问函数:
QString | windowTitle () const |
void | setWindowTitle (const QString & title ) |
Constructs a 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).
[virtual noexcept]
QGraphicsWidget::
~QGraphicsWidget
()
销毁 QGraphicsWidget 实例。
返回此 Widget 的动作列表 (可能为空)。
另请参阅 insertAction (), removeAction (), QWidget::actions (), QAction::associatedWidgets (),和 QAction::associatedGraphicsWidgets ().
追加动作 action 到此 Widget 的动作列表。
All QGraphicsWidgets have a list of QAction ,不管怎样,可以按多种不同方式图形表示它们。默认使用 QAction 列表 (作为返回通过 actions ()) 创建上下文 QMenu .
A QGraphicsWidget 只应每动作有一个,且已添加动作不会导致同一动作在 Widget 中出现 2 次。
另请参阅 removeAction (), insertAction (), actions (),和 QWidget::addAction ().
追加动作 actions 到此 Widget 的动作列表。
另请参阅 removeAction (), QMenu , addAction (),和 QWidget::addActions ().
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 () 和 Qt::MinimumSize .
[override virtual]
QRectF
QGraphicsWidget::
boundingRect
() const
重实现: QGraphicsItem::boundingRect() const .
[virtual protected]
void
QGraphicsWidget::
changeEvent
(
QEvent
*
event
)
此事件处理程序可以被重实现以处理状态改变。
The state being changed in this event can be retrieved through event .
改变事件包括: QEvent::ActivationChange , QEvent::EnabledChange , QEvent::FontChange , QEvent::StyleChange , QEvent::PaletteChange , QEvent::ParentChange , QEvent::LayoutDirectionChange ,和 QEvent::ContentsRectChange .
[slot]
bool
QGraphicsWidget::
close
()
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 拥有 Qt::WA_DeleteOnClose attribute set it will be deleted.
[virtual protected]
void
QGraphicsWidget::
closeEvent
(
QCloseEvent
*
event
)
此事件处理程序用于 event , can be reimplemented in a subclass to receive widget close events. The default implementation accepts the event.
另请参阅 close () 和 QCloseEvent .
[override virtual protected]
bool
QGraphicsWidget::
event
(
QEvent
*
event
)
重实现: QGraphicsObject::event (QEvent *ev).
处理 event . QGraphicsWidget handles the following events:
Event | 用法 |
---|---|
波兰语 | Delivered to the widget some time after it has been shown. |
GraphicsSceneMove | Delivered to the widget after its local position has changed. |
GraphicsSceneResize | Delivered to the widget after its size has changed. |
Show | Delivered to the widget before it has been shown. |
Hide | Delivered to the widget after it has been hidden. |
PaletteChange | Delivered to the widget after its palette has changed. |
FontChange | Delivered to the widget after its font has changed. |
EnabledChange | Delivered to the widget after its enabled state has changed. |
StyleChange | Delivered to the widget after its style has changed. |
LayoutDirectionChange | Delivered to the widget after its layout direction has changed. |
ContentsRectChange | Delivered to the widget after its contents margins/ contents rect has changed. |
[override virtual protected]
void
QGraphicsWidget::
focusInEvent
(
QFocusEvent
*
event
)
重实现: QGraphicsItem::focusInEvent (QFocusEvent *event).
[virtual protected]
bool
QGraphicsWidget::
focusNextPrevChild
(
bool
next
)
查找新的 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 focusNextPrevChild() 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 ().
[override virtual protected]
void
QGraphicsWidget::
focusOutEvent
(
QFocusEvent
*
event
)
重实现: QGraphicsItem::focusOutEvent (QFocusEvent *event).
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,
nullptr
被返回。
另请参阅 QGraphicsItem::focusItem () 和 QWidget::focusWidget ().
[signal]
void
QGraphicsWidget::
geometryChanged
()
This signal gets emitted whenever the geometry is changed in setGeometry ().
注意: 通知程序信号对于特性 geometry 。通知程序信号对于特性 size .
[override virtual]
void
QGraphicsWidget::
getContentsMargins
(
qreal
*
left
,
qreal
*
top
,
qreal
*
right
,
qreal
*
bottom
) const
重实现: QGraphicsLayoutItem::getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const .
Gets the widget's contents margins. The margins are stored in
left
,
top
,
right
and
bottom
, as pointers to qreals. Each argument can be
omitted
by passing
nullptr
.
另请参阅 setContentsMargins ().
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
nullptr
.
另请参阅 setWindowFrameMargins () 和 windowFrameRect ().
[virtual protected]
void
QGraphicsWidget::
grabKeyboardEvent
(
QEvent
*
event
)
此事件处理程序用于 event ,可以在子类中重实现以接收通知为 QEvent::GrabKeyboard 事件。
另请参阅 grabKeyboard () 和 grabMouse ().
[virtual protected]
void
QGraphicsWidget::
grabMouseEvent
(
QEvent
*
event
)
此事件处理程序用于 event ,可以在子类中重实现以接收通知为 QEvent::GrabMouse 事件。
另请参阅 grabMouse () 和 grabKeyboard ().
Adds a shortcut to Qt's shortcut system that watches for the given key sequence 以给定 context 。若 context is Qt::ApplicationShortcut ,快捷方式适用于整个应用程序。否则,要么本地到此小部件 Qt::WidgetShortcut ,或到窗口自身 Qt::WindowShortcut . For widgets that are not part of a window (i.e., top-level widgets and their children), Qt::WindowShortcut shortcuts apply to the scene.
If the same key sequence has been grabbed by several widgets, when the key sequence occurs a QEvent::Shortcut 事件以不确定次序发送给应用它的所有 Widget,但将 "ambiguous (歧义)" 标志设为 true。
警告: 通常不需要使用此函数;而是创建 QAction 采用要求的快捷键序列 (若还想要等效菜单选项和工具栏按钮),或创建 QShortcut 若仅仅需要键序列。 QAction and QShortcut 会为您处理所有事件过滤,并提供当用户触发键序列时触发的信号,所以比此低级函数更易于使用。
另请参阅 releaseShortcut (), setShortcutEnabled (),和 QWidget::grabShortcut ().
[virtual protected]
void
QGraphicsWidget::
hideEvent
(
QHideEvent
*
event
)
此事件处理程序用于 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 QEvent::accept () 或 QEvent::ignore () 在 event 不起作用。
另请参阅 showEvent (), QWidget::hideEvent (),和 ItemVisibleChange .
[override virtual protected]
void
QGraphicsWidget::
hoverLeaveEvent
(
QGraphicsSceneHoverEvent
*
event
)
重实现: QGraphicsItem::hoverLeaveEvent (QGraphicsSceneHoverEvent *event).
[override virtual protected]
void
QGraphicsWidget::
hoverMoveEvent
(
QGraphicsSceneHoverEvent
*
event
)
重实现: QGraphicsItem::hoverMoveEvent (QGraphicsSceneHoverEvent *event).
[virtual protected]
void
QGraphicsWidget::
initStyleOption
(
QStyleOption
*
option
) const
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 & QStyle::State_Enabled | Corresponds to QGraphicsItem::isEnabled (). |
state & QStyle::State_HasFocus | Corresponds to QGraphicsItem::hasFocus (). |
state & QStyle::State_MouseOver | Corresponds to QGraphicsItem::isUnderMouse (). |
direction | Corresponds to QGraphicsWidget::layoutDirection (). |
rect | Corresponds to QGraphicsWidget::rect ().toRect(). |
palette | Corresponds to QGraphicsWidget::palette (). |
fontMetrics | Corresponds to QFontMetrics ( QGraphicsWidget::font ()). |
子类化的 QGraphicsWidget should call the base implementation, and then test the type of option 使用 qstyleoption_cast <>() or test QStyleOption::Type before storing widget-specific options.
例如:
void MyGroupBoxWidget::initStyleOption(QStyleOption *option) const { QGraphicsWidget::initStyleOption(option); if (QStyleOptionGroupBox *box = qstyleoption_cast<QStyleOptionGroupBox *>(option)) { // Add group box specific state. box->flat = isFlat(); ... } }
另请参阅 QStyleOption::initFrom ().
插入动作
action
到此 Widget 的动作列表,前置于动作
before
。它追加动作,若
before
is
nullptr
or
before
不是有效动作 (对于此 Widget)。
A QGraphicsWidget 每动作只应有一个。
另请参阅 removeAction (), addAction (), QMenu , actions (),和 QWidget::insertActions ().
插入动作
actions
到此 Widget 的动作列表,前置于动作
before
。它追加动作,若
before
is
nullptr
or
before
不是有效动作 (对于此 Widget)。
A QGraphicsWidget 每动作最多能有一个。
另请参阅 removeAction (), QMenu , insertAction (),和 QWidget::insertActions ().
返回
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.
另请参阅 QGraphicsScene::activeWindow (), QGraphicsScene::setActiveWindow (),和 isActive ().
[override virtual protected]
QVariant
QGraphicsWidget::
itemChange
(
QGraphicsItem::GraphicsItemChange
change
, const
QVariant
&
value
)
重实现: QGraphicsItem::itemChange (QGraphicsItem::GraphicsItemChange change, const QVariant &value).
QGraphicsWidget uses the base implementation of this function to catch and deliver events related to state changes in the item. Because of this, it is very important that subclasses call the base implementation.
change specifies the type of change, and value is the new value.
例如, QGraphicsWidget uses ItemVisibleChange to deliver Show and Hide events, ItemPositionHasChanged to deliver Move events, and ItemParentChange both to deliver ParentChange events, and for managing the focus chain.
QGraphicsWidget enables the ItemSendsGeometryChanges flag by default in order to track position changes.
另请参阅 QGraphicsItem::itemChange ().
Returns this widget's layout, or
nullptr
if no layout is currently managing this widget.
注意: Getter function for property layout.
另请参阅 setLayout ().
[virtual protected]
void
QGraphicsWidget::
moveEvent
(
QGraphicsSceneMoveEvent
*
event
)
此事件处理程序用于 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 QEvent::accept () 或 QEvent::ignore () 在 event 不起作用。
另请参阅 ItemPositionChange and ItemPositionHasChanged .
[override virtual]
void
QGraphicsWidget::
paint
(
QPainter
*
painter
, const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
= nullptr)
重实现: QGraphicsItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget).
[虚拟]
void
QGraphicsWidget::
paintWindowFrame
(
QPainter
*
painter
, const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
= nullptr)
此虚函数被调用由 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.
可以重实现此函数在子类 QGraphicsWidget to provide custom rendering of the widget's window frame.
另请参阅 QGraphicsItem::paint ().
[virtual protected]
void
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.
Returns the item's local rect as a QRectF . This function is equivalent to QRectF (QPointF(), size ()).
另请参阅 setGeometry () 和 resize ().
删除快捷方式采用给定 id 从 Qt 的快捷方式系统。Widget 将不再接收 QEvent::Shortcut 事件对于快捷方式键序列 (除非它有具有相同键序列的其它快捷方式)。
警告: 通常应该不需要使用此函数,因为 Qt 的快捷方式系统会自动移除快捷方式,当它们的父级 Widget 被销毁时。最好使用 QAction or QShortcut 处理快捷方式,因为它们比此低级函数更易于使用。另请注意,此操作很昂贵。
另请参阅 grabShortcut (), setShortcutEnabled (),和 QWidget::releaseShortcut ().
移除动作 action 从此 Widget 的动作列表。
另请参阅 insertAction (), actions (), insertAction (),和 QWidget::removeAction ().
这是重载函数。
Constructs a resize with the given
width
(
w
) 和
height
(
h
). This convenience function is equivalent to calling resize(
QSizeF
(w, h)).
注意: setter 函数对于特性 size .
另请参阅 setGeometry () 和 setTransform ().
[virtual protected]
void
QGraphicsWidget::
resizeEvent
(
QGraphicsSceneResizeEvent
*
event
)
此事件处理程序用于 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 QEvent::accept () 或 QEvent::ignore () 在 event 不起作用。
另请参阅 geometry () 和 setGeometry ().
[override virtual protected]
bool
QGraphicsWidget::
sceneEvent
(
QEvent
*
event
)
重实现: QGraphicsItem::sceneEvent (QEvent *event).
QGraphicsWidget 's implementation of sceneEvent() simply passes event to QGraphicsWidget::event (). You can handle all events for your widget in event () or in any of the convenience functions; you should not have to reimplement this function in a subclass of QGraphicsWidget .
返回
true
if
event
已被识别并处理;否则,返回
false
.
另请参阅 QGraphicsItem::sceneEvent ().
若 on is true, this function enables 属性 ;否则 属性 被禁用。
See the class documentation for QGraphicsWidget for a complete list of which attributes are supported, and what they are for.
另请参阅 testAttribute () 和 QWidget::setAttribute ().
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 ().
这是重载函数。
Sets the widget's contents margins to left , top , right and bottom .
This convenience function is equivalent to calling setGeometry( QRectF ( x , y , w , h )).
注意: setter 函数对于特性 geometry .
Sets the layout for this widget to
layout
. 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.
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 拥有所有权对于 layout .
注意: setter 函数对于特性 layout .
另请参阅 layout (), QGraphicsLinearLayout::addItem (),和 QGraphicsLayout::invalidate ().
若 enabled 为 true,快捷方式的自动重复采用给定 id 被启用;否则被禁用。
另请参阅 grabShortcut (), releaseShortcut (),和 QWidget::setShortcutAutoRepeat ().
若 enabled 为 True,快捷方式采用给定 id 被启用;否则快捷方式被禁用。
警告: 应该通常不需要使用此函数,因为 Qt 的快捷方式系统会自动启用/禁用快捷方式当 Widget 变为隐藏/可见并获得 (或失去) 聚焦时。最好使用 QAction or QShortcut 处理快捷方式,因为它们比此低级函数更易于使用。
另请参阅 grabShortcut (), releaseShortcut (),和 QWidget::setShortcutEnabled ().
将 Widget 样式设为 style . QGraphicsWidget does not 拥有所有权对于 style .
若未赋值样式,或
style
is
nullptr
,Widget 将使用
QGraphicsScene::style
() 若有设置。否则 Widget 将使用
QApplication::style
().
此函数设置
Qt::WA_SetStyle
属性若
style
不是
nullptr
;否则,它清零属性。
另请参阅 style ().
[static]
void
QGraphicsWidget::
setTabOrder
(
QGraphicsWidget
*
first
,
QGraphicsWidget
*
second
)
移动 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.
注意,由于选项卡次序对于 second 小部件已改变,应排序链像这样:
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
nullptr
, 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
nullptr
, 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.
另请参阅 focusPolicy and Widget 中的键盘焦点 .
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.
另请参阅 unsetWindowFrameMargins (), getWindowFrameMargins (),和 windowFrameRect ().
这是重载函数。
Sets the widget's window frame margins to left , top , right and bottom .
[override virtual]
QPainterPath
QGraphicsWidget::
shape
() const
重实现: QGraphicsItem::shape() const .
[virtual protected]
void
QGraphicsWidget::
showEvent
(
QShowEvent
*
event
)
此事件处理程序用于 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 QEvent::accept () 或 QEvent::ignore () 在 event 不起作用。
另请参阅 hideEvent (), QWidget::showEvent (),和 ItemVisibleChange .
[override virtual protected]
QSizeF
QGraphicsWidget::
sizeHint
(
Qt::SizeHint
which
, const
QSizeF
&
constraint
= QSizeF()) const
重实现: QGraphicsLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const .
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 QApplication::style ().
另请参阅 setStyle ().
返回
true
if
属性
is enabled for this widget; otherwise, returns
false
.
另请参阅 setAttribute ().
[override virtual]
int
QGraphicsWidget::
type
() const
重实现: QGraphicsItem::type() const .
[virtual protected]
void
QGraphicsWidget::
ungrabKeyboardEvent
(
QEvent
*
event
)
此事件处理程序用于 event ,可以在子类中重实现以接收通知为 QEvent::UngrabKeyboard 事件。
另请参阅 ungrabKeyboard () 和 ungrabMouse ().
[virtual protected]
void
QGraphicsWidget::
ungrabMouseEvent
(
QEvent
*
event
)
此事件处理程序用于 event ,可以在子类中重实现以接收通知为 QEvent::UngrabMouse 事件。
另请参阅 ungrabMouse () 和 ungrabKeyboard ().
Resets the window frame margins to the default value, provided by the style.
另请参阅 setWindowFrameMargins (), getWindowFrameMargins (),和 windowFrameRect ().
[override virtual protected]
void
QGraphicsWidget::
updateGeometry
()
重实现: QGraphicsLayoutItem::updateGeometry ().
If this widget is currently managed by a layout, this function notifies the layout that the widget's size hints have changed and the layout may need to resize and reposition the widget accordingly.
Call this function if the widget's sizeHint () has changed.
另请参阅 QGraphicsLayout::invalidate ().
[virtual protected]
bool
QGraphicsWidget::
windowFrameEvent
(
QEvent
*
event
)
此事件处理程序用于 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 ().
Returns the widget's geometry in parent coordinates including any window frame.
另请参阅 windowFrameRect (), getWindowFrameMargins (),和 setWindowFrameMargins ().
Returns the widget's local rect including any window frame.
另请参阅 windowFrameGeometry (), getWindowFrameMargins (),和 setWindowFrameMargins ().
[virtual protected]
Qt::WindowFrameSection
QGraphicsWidget::
windowFrameSectionAt
(const
QPointF
&
pos
) const
Returns the window frame section at position pos ,或 Qt::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 Qt::NoSection for all sections except Qt::BottomRightSection .
另请参阅 windowFrameEvent (), paintWindowFrame (),和 windowFrameGeometry ().
Returns the widgets window type.
另请参阅 windowFlags (), isWindow (),和 isPanel ().