⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventsandfilters.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit -  Events and Event Filters</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Events and Event Filters</h1><br clear="all">An event, in Qt, is an object that inherits <a href="qevent.html">QEvent</a>.  Events aredelivered to objects that inherit <a href="qobject.html">QObject</a> through calling <a href="qobject.html#c67adb">QObject::event()</a>.  Event delivery means that an event has occurred, theQEvent indicates precisely what, and the QObject needs to react.  Mostevents are specific to <a href="qwidget.html">QWidget</a> and its subclasses, but there areimportant events that aren't related to graphics, such as socketactivation, which is the event used by <a href="qsocketnotifier.html">QSocketNotifier</a> for itswork.<p>Some events come from the window system, e.g. <a href="qmouseevent.html">QMouseEvent</a>, somefrom other sources, e.g. <a href="qtimerevent.html">QTimerEvent</a>, and some come from theapplication program.  Qt is symmetric, as usual, so you can sendevents in exactly the same ways as Qt's own event loop does.<p>Most events types have special classes, most commonly <a href="qresizeevent.html">QResizeEvent</a>,<a href="qpaintevent.html">QPaintEvent</a>, <a href="qmouseevent.html">QMouseEvent</a>, <a href="qkeyevent.html">QKeyEvent</a> and <a href="qcloseevent.html">QCloseEvent</a>.There are many others, perhaps forty or so, but most are rather odd.<p>Each class, <a href="qresizeevent.html">QResizeEvent</a> is an excellent example, subclasses QEventand adds event-specific functions.  In the case of QResizeEvent, <a href="qresizeevent.html#a1e820">QResizeEvent::size()</a> and <a href="qresizeevent.html#9d3d24">QResizeEvent::oldSize()</a> are added.<p>Some classes support more than one event type.  <a href="qmouseevent.html">QMouseEvent</a>supports mouse moves, presses, shift-presses, drags, clicks,right-presses, and so on ad infinitum.<p>Since programs need to react in varied and complicated ways, Qt'sevent delivery mechanisms are rather flexible.  The documentation for<a href="qapplication.html#80dc5b">QApplication::notify()</a> concisely tells the whole story, here wewill explain enough for 99% of applications.<p>The normal way for an event to be delivered is by calling a virtualfunction.  For example, <a href="qpaintevent.html">QPaintEvent</a> is delivered by calling <a href="qwidget.html#e3d821">QWidget::paintEvent()</a>.  This virtual function is responsible forreacting appropriately, normally by repainting the widget.<p>Occasionally there isn't a such an event-specific function, or theevent-specific function isn't sufficient.  The most common example istab key presses.  Normally, those are interpreted by QWidget to movethe keyboard focus, but a few widgets need the tab key for themselves.<p>These objects can reimplement <a href="qobject.html#c67adb">QObject::event()</a>, the general eventhandler, and either do their event handling before or after the usualhandling, or replace it completely.  A very odd widget that bothinterprets tab and has an application-specific custom event mightcontain:<p><pre>  bool MyClass:event( <a href="qevent.html">QEvent</a> * e ) {      if ( e-&gt;<a href="qevent.html#4d3e5b">type</a>() == QEvent::KeyPress ) {          <a href="qkeyevent.html">QKeyEvent</a> * ke = (<a href="qkeyevent.html">QKeyEvent</a>*) e;          if ( ke-&gt;<a href="qkeyevent.html#b1e63d">key</a>() == Key_Tab ) {              // special tab handling here              k-&gt;accept();              return TRUE;          }      } else if ( e-&gt;<a href="qevent.html#4d3e5b">type</a>() &gt;= QEvent::User ) {          <a href="qcustomevent.html">QCustomEvent</a> * c = (<a href="qcustomevent.html">QCustomEvent</a>*) e;          // custom event handling here          return TRUE;      }      return QWidget::event( e );  }</pre><p>More commonly, an object needs to look at another's events.  Qtsupports this using <a href="qobject.html#185219">QObject::installEventFilter()</a> (and thecorresponding remove).  For example, dialogs commonly want to filterkey presses for some widgets, e.g. to modify Return-key handling.<p>An event filter gets to process events before the target object does.The filter's <a href="qobject.html#bd20fe">QObject::eventFilter()</a> implementation is called, andcan accept or reject the filter, and allow or deny further processingof the event.  If all the event filters allow further processing of anevent, the event is sent to the target object itself.  If one of themstops processing, the target and any later event filters don't get tosee the event at all.<p>It's also possible to filter <em>all</em> events for the entire application,by installing an event filter on <a href="qapplication.html">QApplication</a>.  This is what <a href="qtooltip.html">QToolTip</a> does in order to see <em>all</em> the mouse and keyboard activity.Of course, this is very powerful, but it also slows down eventdelivery of every single event in the entire application, so it's bestavoided.<p>The global event filters are called before the object-specificfilters.<p>Finally, many applications want to create and send their own events.<p>Creating an event of a built-in type is very simple: Create an objectof the relevant type, and then call <a href="qapplication.html#333290">QApplication::sendEvent()</a> or <a href="qapplication.html#96d77e">QApplication::postEvent()</a>.<p>sendEvent() processes the event immediately - when sendEvent()returns, (the event filters and) the object have already processed theevent.  For many event classes there is a function called isAccepted()tells you whether the event was accepted or rejected by the lasthandler that was called.<p>postEvent() posts the event on a queue for later dispatch.  The nexttime Qt's main event loop runs, it dispatches all posted events, withsome optimization.  For example, if there are several resize events,they are are compacted into one.  The same applies to paint events: <a href="qwidget.html#a66a88">QWidget::update()</a> calls postEvent(), which minimizes flickering andincreases speed by avoiding multiple repaints.<p>postEvent() is also often used during object initialization, since theposted event will typically be dispatched very soon after theinitialization of the object is complete.<p>To create events of a custom type, you need to define an event number,which must be greater than <code>QEvent::User,</code> and probably you also needto subclass <a href="qcustomevent.html">QCustomEvent</a> in order to pass characteristics aboutyour custom event.  See the documentation to <a href="qcustomevent.html">QCustomEvent</a> fordetails.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -