当前位置:网站首页>QT knowledge: detailed explanation of view frame qgraphicswidget

QT knowledge: detailed explanation of view frame qgraphicswidget

2022-06-23 12:00:00 Mr anhydrous

One 、 Abstract

        QGraphicsWidget Is an extended base item . It is in many ways related to QWidget be similar , At the same time, it also adds many graphic functions ; Provide a palette 、 typeface 、 style . Also have geometry(),setLayout() etc. .

Two 、 describe

1、QGraphicsWidget Is an extended base item , It's in QGraphicsItem Provides additional functionality over .

Provide a palette 、 typeface 、 style .
Yes geometry().
You can use setLayout() and layout() Support layout .
Support use grabShortcut() and insertAction() And QAction.
And QGraphicsItem Different ,QGraphicsWidget Not an abstract class .


2、 because QGraphicsWidget And QWidget Similar and having similar API, So take the widget from QWidget Migration to QGraphicsWidget Than QGraphicsItem It's easier .

3、QGraphicsWidget Default supported Qt Widget properties 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 ( from setStyle() Set up ), The widget has been explicitly assigned a style . If not set , The widget will use the style of the scene or application .
Qt::WA_Resized: Indicates that the widget has a definite 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: Make the top-level window inherit fonts from its parent 、 Palette and locale .
4、 Even though QGraphicsWidget Inherited from QObject and QGraphicsItem( Both have their own systems for managing sub items ), But you should use QGraphicsItem Features provided ( instead of QObject) To manage the relationship between parent and child items . from QGraphicsItem Control the stacking order and ownership of drawing items .

3、 ... and 、 Attribute members


1、autoFillBackground : bool

Whether to automatically fill the widget background . If enabled , This attribute will result in Qt Calling paint() Method to fill in the background of the widget . The colors used are determined by the... In the widget palette QPalette::Window Color role definitions .QGraphicsWidget Always use QPalette::Window fill , Unless set WA_OpaquePaintEvent attribute ( attribute ). By default, this attribute is false.

2、focusPolicy : Qt::FocusPolicy

The strategy of accepting keyboard focus ( Focus strategy Qt::FocusPolicy).

If the widget handles keyboard events , You must enable keyboard focus for the widget, for example ,QLineEdit Constructor call setFocusPolicy(Qt::StrongFocus).

If you enable the focus policy ( namely , No Qt::NoFocus),QGraphicsWidget Will automatically enable ItemIsFocusable sign ( Graphic item flags ). Set... On the widget Qt::NoFocus Will clear away ItemIsFocusable sign .

 3、font : QFont

The font of the widget . This attribute consists of an explicitly defined font attribute and an attribute implicitly inherited from the widget's parent . therefore , And use setFont() Compared with the set font ,font() You can return different Fonts . This scheme allows you to define a single entry in a font , It does not affect the inherited entries of the font .

When the font of the widget changes , It parses its entries according to its parent widget . If the widget does not have a parent widget , It parses its entries according to the scenario . The widget then sends itself a FontChange() Event and notify all its descendants , So that they can also parse their Fonts . By default , This property contains the default font for the application .

4、geometry : QRectF

The geometry of the widget . Set the geometry of the drawing item to rectangle . Calling this function modifies the position and size of the drawing item . The graphic item first moves , Then adjust the size. . A side effect of calling this function is that the widget will receive a move event and a resize event . Besides , If the widget is assigned a layout , The layout will activate .

5、layout : QGraphicsLayout*

Layout of widgets .QGraphicsWidget Ownership of the layout .

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

The maximum size of the widget / Minimum / Preferred size .

7、palette : QPalette

Palette of widgets . The palette provides colors and brushes for color groups , Define the general appearance of widgets and their children .QPalette Consists of explicitly defined color groups and groups implicitly inherited from the widget's parent . therefore ,palette() Can return with setPalette() Set up a different palette . When the widget's palette changes , It parses its entries according to its parent widget , Or if it doesn't have a parent widget , Then the scene will be parsed . Then it sends itself a PaletteChange() event , And inform all its descendants , So that they can also parse their palette . By default , This property contains the application's default palette .

8、size : QSizeF

The size of the widget . This attribute only affects the width and height of the widget . The position and upper left corner of the widget are not affected . Resizing the widget triggers GraphicsSceneResize() event .

9、sizePolicy : QSizePolicy

Size strategy .

10、windowFlags : Qt::WindowFlags

Window sign . A window flag is a window type ( Such as Qt::Dialog) And several flags that provide prompt for window behavior . By default , This property does not contain a window flag . Windows are panels , If set Qt::Window sign , be ItemIsPanel( Graphic item flags ) The flag will be set automatically . If you remove Qt::Window sign , be ItemIsPanel The flag will also be cleared . Be careful ,ItemIsPanel Signs can be independent of Qt::Window Set up .

11、windowTitle : QString

Window title . This property is only used for windows .

Four 、 Member functions


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

Add actions to QAction list .QGraphicsWidgets There is one QActions list .

2、void adjustSize()

Resize the widget to its valid preferred size (effectiveSizeHint(Qt::PreferredSize)(QGraphicsLayoutItem)). When drawing items are first displayed , This function is implicitly called .

3、bool close()

Close the widget . If the widget closes successfully , Then return to true, Otherwise return to false. First send a... To the widget QCloseEvent, The widget may or may not accept the event . If the event is ignored , Then do nothing . If the event is accepted , It will close the widget . If the widget is set to Qt::WA_DeleteOnClose attribute , It will be deleted .

4、[ Virtual functions ] bool focusNextPrevChild(bool next)

Press down Tab and Shift+Tab, Find a new widget to focus the keyboard on , If you can find new widgets , Then return to true Otherwise return to false. If next by true, Search forward or search backward .

5、QGraphicsWidget *focusWidget()

If this widget 、 Children or descendants of this widget currently have input focus , This function will return a pointer to the widget .

6、[ The signal ] void geometryChanged()

As long as setGeometry() Change geometry in , It will send out this signal .

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)

towards Qt Add a shortcut key to the shortcut system of (QKeySequence Is a class that encapsulates shortcut key information ), The system operates in a given environment ( Parameters 2) Monitor a given key sequence in .

Qt::ShortcutContext: The context in which the shortcut key is located .

Qt::WidgetShortcut: When the parent widget has focus , Shortcut keys are active .
Qt::WidgetWithChildrenShortcut: When the parent widget or any of its children has focus , Shortcut keys are active .
Qt::WindowShortcut: For widgets that do not belong to Windows ( That is, the top-level widget and its subcomponents ), Shortcut keys apply to the scene .
Qt::ApplicationShortcut: Shortcut keys apply to the entire application .
9、void initStyleOption(QStyleOption *option)

Set the object according to the style (QStyleOption) Set up QGraphicsWidget Widget styles for objects .QStyleOption Object has the following members :

These members are used by default QGraphicsWidget The following properties of :

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)

The parameter 2 The action of is inserted into the parameter 1 Before . If before == nullptr or before Actions that are not the current widget are appended to the action list .

11、bool isActiveWindow()

The widget's window is in the active window , Or the widget has no windows but is in the active scene ( That is, the current scene with focus ) in , Then return to true.( An active window is a window that contains child widgets that currently have input focus or that itself has input focus )

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

Called by the scenario , Draw the window border for the window in local coordinates . The basic implementation uses the current style to render borders and title blocks .

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 by true, This function enables the attribute , Otherwise, the attribute is disabled . Useful properties

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

Set left 、 On 、 Right 、 Content margin under . Layouts use content margins to define the location of child widgets and layouts .

17、void setLayout(QGraphicsLayout *layout)

Setting up layout . Before assigning a new layout , Any existing layout managers... Will be deleted .QGraphicsWidget Ownership of the layout .

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

Set when pressed tab Key focus from A Move to B.

set focus a->b->c->d 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 . The default margins are provided by the style , Depends on the current window flag . If you want to draw your own window style , You can set your own frame margins to override the default margins .

20、bool testAttribute(Qt::WidgetAttribute attribute)

Returns whether the widget has properties enabled .

21、void unsetWindowFrameMargins()

Reset border margins to the default values provided by the style .

5、 ... and 、 Event member functions


1、void changeEvent(QEvent *event)

Status 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)

Handling events . The following events can be handled :

QEvent::Polish
QEvent::GraphicsSceneMove: The position in the scene changes .
QEvent::GraphicsSceneResize: The size changes in the scene .
QEvent::Show: The widget is displayed on the screen .
QEvent::Hide: Widgets hide .
QEvent::PaletteChange: Palette changes .
QEvent::FontChange: Font change .
QEvent::EnabledChange: Enable state change .
QEvent::StyleChange: Style change .
QEvent::LayoutDirectionChange: The layout direction changes .
QEvent::ContentsRectChange: Content margins / Scope changes .
4、void grabKeyboardEvent(QEvent *event)

Handle QEvent::GrabKeyboard ( Drawing item get keyboard grab ) event .

5、void grabMouseEvent(QEvent *event)

Handle QEvent::GrabMouse( Drawing item get mouse crawl ) event .

6、void hideEvent(QHideEvent *event)

Hidden Events . Called on an event QEvent::accept() or QEvent::ignore() Invalid .

7、void moveEvent(QGraphicsSceneMoveEvent *event)

Mobile event . This event is only passed when the drawing item itself moves . call setTransform() Or moving the ancestor of any drawing item does not affect the local location of the drawing item . Called on an event QEvent::accept() or QEvent::ignore() Invalid .

8、void polishEvent()

This event is passed from the scene to the drawing item at a certain time point after the drawing item is built , The function of this event is to perform the final initialization operation before the graphic item is displayed .

9、void resizeEvent(QGraphicsSceneResizeEvent *event)

Resizing Events . This event is only passed when the widget is resized locally . Call on the widget or any of its ancestors or views setTransform() Does not affect the local size of the widget . Called on an event QEvent::accept() or QEvent::ignore() Invalid .

10、bool sceneEvent(QEvent *event)

from QGraphicsItem::sceneEvent() Re actualize .QGraphicsWidget Of sceneEvent() The implementation simply passes events to QGraphicsWidget::event(). Should not be in QGraphicsWidget Re implement this function in a subclass of .

11、void showEvent(QShowEvent *event)

Show events . Pass this event before the widget is displayed . Called on an event QEvent::accept() or QEvent::ignore() Invalid .

12、void ungrabKeyboardEvent(QEvent *event)

Handle lost keyboard grab Events QEvent::UngrabKeyboard.

13、void ungrabMouseEvent(QEvent *event)

Handle lost mouse grab Events QEvent::UngrabMouse.

14、bool windowFrameEvent(QEvent *event)

Handle window frame Events . Its basic implementation provides support for default window frame interaction , Such as mobile 、 Resize, etc .
 

原网站

版权声明
本文为[Mr anhydrous]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231152017016.html