Qml window resize event


  • Qt Documentation
  • Every widget is rectangular, and they are sorted in a Z-order. A widget is clipped by its parent and by the widgets in front of it. A widget that is not embedded in a parent widget is called a window.

    Usually, windows have a frame and a title bar, although it is also possible to create windows without such decoration using suitable window flags. If it is nullptr the default , the new widget will be a window. If not, it will be a child of parent, and be constrained by parent's geometry unless you specify Qt::Window as window flag.

    QWidget has many member functions, but some of them have little direct functionality; for example, QWidget has a font property, but never uses this itself. Top-Level and Child Widgets A widget without a parent widget is always an independent window top-level widget.

    For these widgets, setWindowTitle and setWindowIcon set the title bar and icon respectively. Non-window widgets are child widgets, displayed within their parent widgets.

    Most widgets in Qt are mainly useful as child widgets. For example, it is possible to display a button as a top-level window, but most people prefer to put their buttons inside other widgets, such as QDialog. The diagram above shows a QGroupBox widget being used to hold various child widgets in a layout provided by QGridLayout.

    The QLabel child widgets have been outlined to indicate their full sizes. If you want to use a QWidget to hold child widgets you will usually want to add a layout to the parent QWidget. See Layout Management for more information.

    Composite Widgets When a widget is used as a container to group a number of child widgets, it is known as a composite widget. These can be created by constructing a widget with the required visual properties - a QFrame , for example - and adding child widgets to it, usually managed by a layout. The above diagram shows such a composite widget that was created using Qt Designer. Composite widgets can also be created by subclassing a standard widget, such as QWidget or QFrame , and adding the necessary layout and child widgets in the constructor of the subclass.

    Many of the examples provided with Qt use this approach, and it is also covered in the Qt Tutorials. Custom Widgets and Painting Since QWidget is a subclass of QPaintDevice , subclasses can be used to display custom content that is composed using a series of painting operations with an instance of the QPainter class.

    This approach contrasts with the canvas-style approach used by the Graphics View Framework where items are added to a scene by the application and are rendered by the framework itself.

    Each widget performs all painting operations from within its paintEvent function. This is called whenever the widget needs to be redrawn, either as a result of some external change or when requested by the application. The Analog Clock example shows how a simple widget can handle paint events.

    Size Hints and Size Policies When implementing a new widget, it is almost always useful to reimplement sizeHint to provide a reasonable default size for the widget and to set the correct size policy with setSizePolicy.

    By default, composite widgets which do not provide a size hint will be sized according to the space requirements of their child widgets. The size policy lets you supply good default behavior for the layout management system, so that other widgets can contain and manage yours easily.

    The default size policy indicates that the size hint represents the preferred size of the widget, and this is often good enough for many widgets. You can resize the widget manually if these bounds are inadequate. Events Widgets respond to events that are typically caused by user actions. Qt delivers events to widgets by calling specific event handler functions with instances of QEvent subclasses containing information about each event.

    If your widget only contains child widgets, you probably do not need to implement any event handlers. If you want to detect a mouse click in a child widget call the child's underMouse function inside the widget's mousePressEvent. The Scribble example implements a wider set of events to handle mouse movement, button presses, and window resizing. You will need to supply the behavior and content for your own widgets, but here is a brief overview of the events that are relevant to QWidget, starting with the most common ones: paintEvent is called whenever the widget needs to be repainted.

    Every widget displaying custom content must implement it. Painting using a QPainter can only take place in a paintEvent or a function called by a paintEvent. Pressing the mouse without releasing it is effectively the same as calling grabMouse. A widget receives mouse release events when it has received the corresponding mouse press event. This means that if the user presses the mouse inside your widget, then drags the mouse somewhere else before releasing the mouse button, your widget receives the release event.

    There is one exception: if a popup menu appears while the mouse button is held down, this popup immediately steals the mouse events. If the user double-clicks, the widget receives a mouse press event, a mouse release event, a mouse click event, a second mouse press, this event and finally a second mouse release event. Some mouse move events may also be received if the mouse is not held steady during this operation.

    It is not possible to distinguish a click from a double-click until the second click arrives. This is one reason why most GUI books recommend that double-clicks be an extension of single-clicks, rather than trigger a different action. Widgets that accept keyboard input need to reimplement a few more event handlers: keyPressEvent is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat.

    To force those keys to be processed by your widget, you must reimplement QWidget::event. Well-behaved widgets indicate that they own the keyboard focus in a clear but discreet way. You may be required to also reimplement some of the less common event handlers: mouseMoveEvent is called whenever the mouse moves while a mouse button is held down. This can be useful during drag and drop operations. If you call setMouseTracking true , you get mouse move events even when no buttons are held down. See also the Drag and Drop guide.

    In that case, the widget will receive a pair of key release and key press event for every repeat. This excludes screen space owned by any of the widget's children. If the mouse enters a child widget it will not cause a leaveEvent. There are also some rather obscure events described in the documentation for QEvent::Type.

    To handle these events, you need to reimplement event directly. Events and the mechanism used to deliver them are covered in The Event System. Groups of Functions and Properties Context.

    The difference between Window::activeFocusItem and ApplicationWindow::activeFocusControl is that the former may point to a building block of a control, whereas the latter points to the enclosing control. The background item is stacked under the content item , but above the background color of the window. The background item is useful for images and gradients, for example, but the color property is preferable for solid colors, as it doesn't need to create an item.

    Note: If the background item has no explicit size specified, it automatically follows the control's size. In most cases, there is no need to specify width or height for a background item. The data property allows you to freely mix visual children, resources and other windows in an ApplicationWindow.

    If you assign an Item to the contentData list, it becomes a child of the window's contentItem , so that it appears inside the window.

    The item's parent will be the window's contentItem. It should not generally be necessary to refer to the contentData property, as it is the default property for ApplicationWindow and thus all child items are automatically assigned to this property.

    The content item is stacked above the background item, and under the menuBar , header , and footer items. The default font depends on the system environment. Finally, the font is matched against Qt's font database to find the best match. ApplicationWindow propagates explicit font properties to child controls.

    If you change a specific property on the window's font, that property propagates to all child controls in the window, overriding any system defaults for that property.

    The footer item is positioned to the bottom, and resized to the width of the window. The default value is null. The header item is positioned at the top of the window, below the menu bar, and resized to the width of the window.

    The default locale depends on the system environment. ApplicationWindow propagates the locale to child controls. If you change the window's locale, that locale propagates to all child controls in the window, overriding the system default locale. The menu bar is positioned at the top of the window, above the header, and resized to the width of the window.

    The above diagram shows such a composite widget that was created using Qt Designer. Composite widgets can also be created by subclassing a standard widget, such as QWidget or QFrameand adding the necessary layout and child widgets in the constructor of the subclass. Many of the examples provided with Qt use this approach, and it is also covered in the Qt Tutorials.

    Custom Widgets and Painting Since QWidget is a subclass of QPaintDevicesubclasses can be used to display custom content that is composed using a series of painting operations with an instance of the QPainter class.

    This approach contrasts with the canvas-style approach used by the Graphics View Framework where items are added to a scene by the application and are rendered by the framework itself.

    Each widget performs all painting operations from within its paintEvent function. This is called whenever the widget needs to be redrawn, either as a result of some external change or when requested by the application. The Analog Clock example shows how a simple widget can handle paint events. Size Hints and Size Policies When implementing a new widget, it is almost always useful to reimplement sizeHint to provide a reasonable default size for the widget and to set the correct size policy with setSizePolicy.

    By default, composite widgets which do not provide a size hint will be sized according to the space requirements of their child widgets. The size policy lets you supply good default behavior for the layout management system, so that other widgets can contain and manage yours easily. The default size policy indicates that the size hint represents the preferred size of the widget, and this is often good enough for many widgets.

    You can resize the widget manually if these bounds are inadequate. Events Widgets respond to events that are typically caused by user actions. Qt delivers events to widgets by calling specific event handler functions with instances of QEvent subclasses containing information about each event. If your widget only contains child widgets, you probably do not need to implement any event handlers.

    If you want to detect a mouse click in a child widget call the child's underMouse function inside the widget's mousePressEvent. The Scribble example implements a wider set of events to handle mouse movement, button presses, and window resizing.

    You will need to supply the behavior and content for your own widgets, but here is a brief overview of the events that are relevant to QWidget, starting with the most common ones: paintEvent is called whenever the widget needs to be repainted.

    Every widget displaying custom content must implement it. Painting using a QPainter can only take place in a paintEvent or a function called by a paintEvent. Pressing the mouse without releasing it is effectively the same as calling grabMouse. A widget receives mouse release events when it has received the corresponding mouse press event. This means that if the user presses the mouse inside your widget, then drags the mouse somewhere else before releasing the mouse button, your widget receives the release event.

    There is one exception: if a popup menu appears while the mouse button is held down, this popup immediately steals the mouse events. If the user double-clicks, the widget receives a mouse press event, a mouse release event, a mouse click event, a second mouse press, this event and finally a second mouse release event.

    Some mouse move events may also be received if the mouse is not held steady during this operation. It is not possible to distinguish a click from a double-click until the second click arrives. This is one reason why most GUI books recommend that double-clicks be an extension of single-clicks, rather than trigger a different action. Widgets that accept keyboard input need to reimplement a few more event handlers: keyPressEvent is called whenever a key is pressed, and again when a key has been held down long enough for it to auto-repeat.

    Qt Documentation

    The background item is stacked under the content itembut above the background color of the window. The background item is useful for images and gradients, for example, but the color property is preferable for solid colors, as it doesn't need to create an item. Note: If the background item has no explicit size specified, it automatically follows the control's size.

    In most cases, there is no need to specify width or height for a background item. The data property allows you to freely mix visual children, resources and other windows in an ApplicationWindow. If you assign an Item to the contentData list, it becomes a child of the window's contentItemso that it appears inside the window. The item's parent will be the window's contentItem.

    It should not generally be necessary to refer to the contentData property, as it is the default property for ApplicationWindow and thus all child items are automatically assigned to this property.

    The content item is stacked above the background item, and under the menuBarheaderand footer items. The default font depends on the system environment.


    thoughts on “Qml window resize event

    Leave a Reply

    Your email address will not be published. Required fields are marked *