Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 103

qtbase5-doc-5.12.6-2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- eventsandfilters.qdoc -->
  <title>The Event System | Qt Core 5.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtcore-index.html">Qt Core</a></td><td >The Event System</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtcore-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#how-events-are-delivered">How Events are Delivered</a></li>
<li class="level1"><a href="#event-types">Event Types</a></li>
<li class="level1"><a href="#event-handlers">Event Handlers</a></li>
<li class="level1"><a href="#event-filters">Event Filters</a></li>
<li class="level1"><a href="#sending-events">Sending Events</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">The Event System</h1>
<span class="subtitle"></span>
<!-- $$$eventsandfilters.html-description -->
<div class="descr"> <a name="details"></a>
<p>In Qt, events are objects, derived from the abstract <a href="qevent.html">QEvent</a> class, that represent things that have happened either within an application or as a result of outside activity that the application needs to know about. Events can be received and handled by any instance of a <a href="qobject.html">QObject</a> subclass, but they are especially relevant to widgets. This document describes how events are delivered and handled in a typical application.</p>
<a name="how-events-are-delivered"></a>
<h2 id="how-events-are-delivered">How Events are Delivered</h2>
<p>When an event occurs, Qt creates an event object to represent it by constructing an instance of the appropriate <a href="qevent.html">QEvent</a> subclass, and delivers it to a particular instance of <a href="qobject.html">QObject</a> (or one of its subclasses) by calling its <a href="qobject.html#event">event()</a> function.</p>
<p>This function does not handle the event itself; based on the type of event delivered, it calls an event handler for that specific type of event, and sends a response based on whether the event was accepted or ignored.</p>
<p>Some events, such as <a href="../qtgui/qmouseevent.html">QMouseEvent</a> and <a href="../qtgui/qkeyevent.html">QKeyEvent</a>, come from the window system; some, such as <a href="qtimerevent.html">QTimerEvent</a>, come from other sources; some come from the application itself.</p>
<a name="event-types"></a>
<h2 id="event-types">Event Types</h2>
<p>Most event types have special classes, notably <a href="../qtgui/qresizeevent.html">QResizeEvent</a>, <a href="../qtgui/qpaintevent.html">QPaintEvent</a>, <a href="../qtgui/qmouseevent.html">QMouseEvent</a>, <a href="../qtgui/qkeyevent.html">QKeyEvent</a>, and <a href="../qtgui/qcloseevent.html">QCloseEvent</a>. Each class subclasses <a href="qevent.html">QEvent</a> and adds event-specific functions. For example, <a href="../qtgui/qresizeevent.html">QResizeEvent</a> adds <a href="../qtgui/qresizeevent.html#size">size()</a> and <a href="../qtgui/qresizeevent.html#oldSize">oldSize()</a> to enable widgets to discover how their dimensions have been changed.</p>
<p>Some classes support more than one actual event type. <a href="../qtgui/qmouseevent.html">QMouseEvent</a> supports mouse button presses, double-clicks, moves, and other related operations.</p>
<p>Each event has an associated type, defined in <a href="qevent.html#Type-enum">QEvent::Type</a>, and this can be used as a convenient source of run-time type information to quickly determine which subclass a given event object was constructed from.</p>
<p>Since programs need to react in varied and complex ways, Qt's event delivery mechanisms are flexible. The documentation for <a href="qcoreapplication.html#notify">QCoreApplication::notify</a>() concisely tells the whole story; the <i>Qt Quarterly</i> article <a href="http://doc.qt.io/archives/qq/qq11-events.html">Another Look at Events</a> rehashes it less concisely. Here we will explain enough for 95% of applications.</p>
<a name="event-handlers"></a>
<h2 id="event-handlers">Event Handlers</h2>
<p>The normal way for an event to be delivered is by calling a virtual function. For example, <a href="../qtgui/qpaintevent.html">QPaintEvent</a> is delivered by calling <a href="../qtwidgets/qwidget.html#paintEvent">QWidget::paintEvent</a>(). This virtual function is responsible for reacting appropriately, normally by repainting the widget. If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation.</p>
<p>For example, the following code handles left mouse button clicks on a custom checkbox widget while passing all other button clicks to the base <a href="../qtwidgets/qcheckbox.html">QCheckBox</a> class:</p>
<pre class="cpp">

  <span class="type">void</span> MyCheckBox<span class="operator">::</span>mousePressEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
          <span class="comment">// handle left mouse button here</span>
      } <span class="keyword">else</span> {
          <span class="comment">// pass on other buttons to base class</span>
          <span class="type"><a href="../qtwidgets/qcheckbox.html">QCheckBox</a></span><span class="operator">::</span>mousePressEvent(event);
      }
  }

</pre>
<p>If you want to replace the base class's function, you must implement everything yourself. However, if you only want to extend the base class's functionality, then you implement what you want and call the base class to obtain the default behavior for any cases you do not want to handle.</p>
<p>Occasionally, there isn't such an event-specific function, or the event-specific function isn't sufficient. The most common example involves Tab key presses. Normally, <a href="../qtwidgets/qwidget.html">QWidget</a> intercepts these to move the keyboard focus, but a few widgets need the Tab key for themselves.</p>
<p>These objects can reimplement <a href="qobject.html#event">QObject::event</a>(), the general event handler, and either do their event handling before or after the usual handling, or they can replace the function completely. A very unusual widget that both interprets Tab and has an application-specific custom event might contain the following <a href="qobject.html#event">event()</a> function:</p>
<pre class="cpp">

  bool MyWidget<span class="operator">::</span>event(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress) {
          <span class="type"><a href="../qtgui/qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span>ke <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="../qtgui/qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(event);
          <span class="keyword">if</span> (ke<span class="operator">-</span><span class="operator">&gt;</span>key() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Tab) {
              <span class="comment">// special tab handling here</span>
              <span class="keyword">return</span> <span class="keyword">true</span>;
          }
      } <span class="keyword">else</span> <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> MyCustomEventType) {
          MyCustomEvent <span class="operator">*</span>myEvent <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span>MyCustomEvent <span class="operator">*</span><span class="operator">&gt;</span>(event);
          <span class="comment">// custom event handling here</span>
          <span class="keyword">return</span> <span class="keyword">true</span>;
      }

      <span class="keyword">return</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span><span class="operator">::</span>event(event);
  }

</pre>
<p>Note that <a href="../qtwidgets/qwidget.html#event">QWidget::event</a>() is still called for all of the cases not handled, and that the return value indicates whether an event was dealt with; a <code>true</code> value prevents the event from being sent on to other objects.</p>
<a name="event-filters"></a>
<h2 id="event-filters">Event Filters</h2>
<p>Sometimes an object needs to look at, and possibly intercept, the events that are delivered to another object. For example, dialogs commonly want to filter key presses for some widgets; for example, to modify Return-key handling.</p>
<p>The <a href="qobject.html#installEventFilter">QObject::installEventFilter</a>() function enables this by setting up an <i>event filter</i>, causing a nominated filter object to receive the events for a target object in its <a href="qobject.html#eventFilter">QObject::eventFilter</a>() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the <a href="qobject.html#removeEventFilter">QObject::removeEventFilter</a>() function.</p>
<p>When the filter object's <a href="qobject.html#eventFilter">eventFilter()</a> implementation is called, it can accept or reject the event, and allow or deny further processing of the event. If all the event filters allow further processing of an event (by each returning <code>false</code>), the event is sent to the target object itself. If one of them stops processing (by returning <code>true</code>), the target and any later event filters do not get to see the event at all.</p>
<pre class="cpp">

  bool FilterObject<span class="operator">::</span>eventFilter(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>object<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (object <span class="operator">=</span><span class="operator">=</span> target <span class="operator">&amp;</span><span class="operator">&amp;</span> event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress) {
          <span class="type"><a href="../qtgui/qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span>keyEvent <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="../qtgui/qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(event);
          <span class="keyword">if</span> (keyEvent<span class="operator">-</span><span class="operator">&gt;</span>key() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Key_Tab) {
              <span class="comment">// Special tab handling</span>
              <span class="keyword">return</span> <span class="keyword">true</span>;
          } <span class="keyword">else</span>
              <span class="keyword">return</span> <span class="keyword">false</span>;
      }
      <span class="keyword">return</span> <span class="keyword">false</span>;
  }

</pre>
<p>The above code shows another way to intercept Tab key press events sent to a particular target widget. In this case, the filter handles the relevant events and returns <code>true</code> to stop them from being processed any further. All other events are ignored, and the filter returns <code>false</code> to allow them to be sent on to the target widget, via any other event filters that are installed on it.</p>
<p>It is also possible to filter <i>all</i> events for the entire application, by installing an event filter on the <a href="../qtwidgets/qapplication.html">QApplication</a> or <a href="qcoreapplication.html">QCoreApplication</a> object. Such global event filters are called before the object-specific filters. This is very powerful, but it also slows down event delivery of every single event in the entire application; the other techniques discussed should generally be used instead.</p>
<a name="sending-events"></a>
<h2 id="sending-events">Sending Events</h2>
<p>Many applications want to create and send their own events. You can send events in exactly the same ways as Qt's own event loop by constructing suitable event objects and sending them with <a href="qcoreapplication.html#sendEvent">QCoreApplication::sendEvent</a>() and <a href="qcoreapplication.html#postEvent">QCoreApplication::postEvent</a>().</p>
<p><a href="qcoreapplication.html#sendEvent">sendEvent()</a> processes the event immediately. When it returns, the event filters and/or the object itself have already processed the event. For many event classes there is a function called <a href="qevent.html#accepted-prop">isAccepted()</a> that tells you whether the event was accepted or rejected by the last handler that was called.</p>
<p><a href="qcoreapplication.html#postEvent">postEvent()</a> posts the event on a queue for later dispatch. The next time Qt's main event loop runs, it dispatches all posted events, with some optimization. For example, if there are several resize events, they are compressed into one. The same applies to paint events: <a href="../qtwidgets/qwidget.html#update">QWidget::update</a>() calls <a href="qcoreapplication.html#postEvent">postEvent()</a>, which eliminates flickering and increases speed by avoiding multiple repaints.</p>
<p><a href="qcoreapplication.html#postEvent">postEvent()</a> is also used during object initialization, since the posted event will typically be dispatched very soon after the initialization of the object is complete. When implementing a widget, it is important to realize that events can be delivered very early in its lifetime so, in its constructor, be sure to initialize member variables early on, before there's any chance that it might receive an event.</p>
<p>To create events of a custom type, you need to define an event number, which must be greater than <a href="qevent.html#Type-enum">QEvent::User</a>, and you may need to subclass <a href="qevent.html">QEvent</a> in order to pass specific information about your custom event. See the <a href="qevent.html">QEvent</a> documentation for further details.</p>
</div>
<!-- @@@eventsandfilters.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>