Window system events tell the widget when it needs to repaint, reposition, or resize itself, when mouse clicks or keystrokes have been directed toward that widget, when the widget receives or loses the focus, and so on. QWidget handles the events by calling a virtual method for each event. Each method gets passed, as an argument, a class containing information about the event. To handle the event, the corresponding method must be reimplemented in the subclass of QWidget. A very important system event is the paint event. In response to this event, a widget draws (or "paints") itself. It is sent to the widget every time the widget needs to be displayed or redisplayed. For example, the event is sent when the widget is first created, when it is made visible after being hidden, or when it is being uncovered after having been fully or partially obscured. The paint event is discussed in detail in the next section, and techniques for repainting efficiently are discussed in Chapter 9, "Constructing a Responsive User Interface." The following is a list of the (protected, virtual) QWidget event handlers and corresponding events. This is the main event handler. This method dispatches the events to their specialized event handlers. Normally, you do not need to reimplement this method. The argument tells the type of event. Gets called when one of the mouse buttons is pressed down with the mouse cursor inside this widget. The argument tells which button was pressed, whether a modifier key (Ctrl, Alt, or Shift) was pressed in combination with it, and where the cursor was when the button was pressed. Gets called when one of the mouse buttons is released with the mouse cursor inside this widget. (See mousePressEvent() for a description of the argument.) Gets called when the user double-clicks on the widget. (See mousePressEvent() for a description of the argument.) Gets called when the user moves the mouse with the cursor over the widget. This event is generated only when a button is held down, unless you turn on mouse tracking with QWidget::setMouseTracking (true). (See mousePressEvent() for a description of the argument.) Gets called when the user moves the mouse wheel (if there is one) and this widget has the focus. The argument tells how far and in which direction the wheel has been rotated. This event can be ignored by calling QWheelEvent::ignore() if it is not processed. In this case, the event gets passed to the parent widget for processing. Gets called when a key is pressed and this widget has the focus. The argument contains a code telling which key was pressed and whether a modifier key (Ctrl, Alt, or Shift) was being held down. If you have turned on key compression with QWidget::setKeyCompression (true), the argument may contain a text string representing all the keys that were pressed since the last time you received this event. Gets called when a key has been released and this widget has the focus. Gets called when this widget receives the focus. If this widget is willing to accept the focus, the default implementation calls QWidget::repaint() to redraw the widget with a focused look. Gets called when this widget loses the focus. If this widget is willing to accept the focus, the default implementation calls QWidget::repaint() to redraw the widget with an unfocused look. Gets called when the mouse cursor enters the widget. Gets called when the mouse cursor leaves the widget. Gets called when the widget needs to be repainted, such as when it is first created or uncovered after being totally or partially obscured by another window. The argument tells which part of the widget needs to be repainted. Gets called when the widget has been moved relative to its parent. The argument tells the new and old positions. Gets called when the widget has been resized. The argument tells the new and old sizes. For a top-level widget, this gets called when the user tries to close the window (using the close button on the window frame, for example). For other widgets, this gets called when the application calls the QWidget::close() method. In a Qt (non-KDE) application, this would be a good place to ask, "Are you sure?" KDE applications should use KTMainWindow::queryClose() for this purpose. You can accept or ignore the close event by setting a flag in the QCloseEvent argument. Gets called when a user is dragging data and the mouse cursor first enters this widget. The argument tells where the mouse cursor is, what kind of data is being dragged, and what the data is. Gets called when a user drags data over this widget. (See dragEnterEvent() for a description of the argument.) Gets called when a user is dragging data and the mouse cursor leaves this widget. (See dragEnterEvent() for a description of the argument.) Gets called when a user drops (in a drag-and-drop operation) data onto your widget. (See dragEnterEvent() for a description of the argument.) Gets called when the widget is first created or when the window in which it lies is deiconified. The argument tells whether the show event originated inside the application or outside the application (for example, the user clicked the deiconify button on the taskbar). Gets called right after the widget has been hidden. The argument tells whether the show event originated inside the application or outside the application (for example, the user clicked the iconify button on the window). |