QT 事件处理

  • Post author:
  • Post category:其他



从C++.GUI.Programming.with.Qt.4中摘抄而来:

Qt offers five levels at which events can be processed and filtered:


  1. We can reimplement a specific event handler.

    Reimplementing event handlers such as

    mousePressEvent()

    ,

    keyPress-Event()

    , and

    paintEvent()

    is by far the most common way to process events. We have already seen many examples of this.


  2. We can reimplement QObject::event().

    By reimplementing the

    event()

    function, we can process events before they reach the specific event handlers. This approach is mostly needed to override the default meaning of the Tab key, as shown earlier (p. 164). This is also used to handle rare types of event for which no specific event handler exists (for example,

    QEvent::HoverEnter

    ). When we reimplement

    event()

    , we must call the base class’s

    event()

    function for handling the cases we don’t explicitly handle.


  3. We can install an event filter on a single QObject.

    Once an object has been registered using

    installEventFilter()

    , all the events for the target object are first sent to the monitoring object’s

    event-Filter()

    function. If multiple event filters are installed on the same object, the filters are activated in turn, from the most recently installed back to the first installed.


  4. We can install an event filter on the QApplication object.

    Once an event filter has been registered for

    qApp

    (the unique

    QApplication

    object), every event for every object in the application is sent to the

    event-Filter()

    function before it is sent to any other event filter. This approach is mostly useful for debugging. It can also be used to handle mouse events sent to disabled widgets, which

    QApplication

    normally discards.


  5. We can subclass QApplication and reimplement notify().

    Qt calls

    QApplication::notify()

    to send out an event. Reimplementing this function is the only way to get all the events, before any event filters get the opportunity to look at them. Event filters are generally more useful,




















    because there can be any number of concurrent event filters, but only one

    notify()

    function.


Qt


的事件处理有


5


中级别:



1.






重写控件的事件处理函数:如重写


keyPressEvent()





mousePressEvent()





paintEvent()


,这是最常用的事件处理方法,我们已经看到过很多这样的例子了。



2.






重写


QObject::event()


,在事件到达事件处理函数时处理它。在需要改变


Tab


键的惯用法时这样做。也可以处理那些没有特定事件处理函数的比较少见的事件类型(例如,


QEvent::HoverEnter


)。我们重写


event()


时,必须要调用基类的


event()


,由基类处理我们不需要处理的那些情况。



3.









QObject


对象安装事件过滤器:对象用


installEventFilter()


后,所有达到目标控件的事件都首先到达监视对象的


eventFilter()


函数。如果一个对象有多个事件过滤器,过滤器按顺序激活,先到达最近安装的监视对象,最后到达最先安装的监视对象。



4.









QApplication


安装事件过滤器,如果


qApp


(唯一的


QApplication


对象)安装了事件过滤器,程序中所有对象的事件都要送到


eventFilter()


函数中。这个方法在调试的时候非常有用,在处理非活动状态控件的鼠标事件时这个方法也很常用。



5.






继承


QApplication


,重写


notify()





Qt


调用


QApplication::nofity()


来发送事件。重写这个函数是在其他事件过滤器处理事件前得到所有事件的唯一方法。通常事件过滤器是最有用的,因为在同一时间,可以有任意数量的事件过滤器,但是


notify()


函数只有一个。