SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
C++ GUI Programming with Qt 4

Chap7. Event Processing
Outline
●   重新實現事件處理器
●   安裝事件過濾器
●   處理密集時的響應保持
7.1 QEvent
●   The QEvent class is the base class of all event
    classes
●   Qt's main event loop

        (QCoreApplication::exec())

    fetches native window system events from the
    event queue, translates them into QEvents, and
    sends the translated events to QObjects.
QWidget::event()
●   This is the main event handler; it handles event
    event
●   You can re-implement this function in a
    subclass, but we recommend using one of
    the specialized event handlers instead
    ( keyPressEvent(), mouseMoveEvent(),
    paintEvent(), etc... ).
Common Event Types (1)
●   鍵盤事件 : keyPressEvent(), keyReleaseEvent()
Tab & BackTab
●   當安排的事件非預期行為時,利用重新實現
    QObject::event() → QWidget::event() 在窗口
    widget 調用 keyPressEvent() 之前,先攔截處
    理
QAction, QShortcut
●   利用 Action 當作 signal 發出者,綁定一個鍵盤
    事件,然後 trigger 一個 slot 反應函數
Common Event Types (2)
●   定時器事件
●   大多數事件類型發生是因為用戶的動作,但定時
    器事件允許應用程序在一定的時間間隔後執
    行事件處理 ( 游標閃爍、動畫播放、簡單顯示
    的刷新,等等 ...)
跑馬燈範例
●   利用 showEvent() 和 hideEvent() 啟動和關閉
    定時器
●   timerEvent() 每隔若干時間會被呼叫

●   當需要多個定時器時,保持對所有定時器 ID 的
    追蹤會變得很麻煩。此時,更簡單的方式是為每
    一個定時器分別建立一個 QTimer object 。
7.2 Event Filter
●   QObject instance 在看到他自己的事件之前,可
    以通過設置另外一個 QObject instance 監視 / 攔
    截 這些事件

●   當許多 Qt 物件需要針對某一致事件產生反應,
    利用 Event Filter 機制,由另外一個 QObject 將
    處理集中於一處。

●   InstallEventFilter(), eventFilter()
5 levels event processing
●   重新實現特殊的事件處理器
    ●   MousePressEvent(), keyPressEvent(), etc...
●   重新實現 QObject::event()
    ●   覆蓋 Tab 鍵的預設語意
●   在 QObject 中安裝事件過濾器
●   在 QApplication object 中安裝事件過濾器
    ●   應用程序中每個 object 的每個事件都會在發送到其他事件過
        濾器之前,先發送給這個 eventFilter() 函數,適用於 debug
        的時候
●   Subclassing Qapplication 並且重新實現 notify()
Event Propagation
7.3 Staying Responsive
●   處理一個事件時,也可能同時產生其他的事件,並且將
    其追加到 Qt 的事件陣列中
●   當處理一個特定事件上耗費的時間過多 (I/O 存取 ) ,用
    戶介面就會變得無法響應
●   解法
    ●   1. 多線程
    ●   2. 在 I/O 存取的代碼中頻繁調用
        QApplication::processEvents() - 這個函數告訴 Qt 處理所有
        那些還沒有被處理的各類事件,然後再將控制權返回給調用
        者。實際上, QApplication::exec() 就是一個不停調用
        processEvents() 的 while 循環
qApp->processEvents(QEventLoop::ExcludeUserInputEvents)
告訴 Qt 忽略滑鼠和鍵盤事件
Defer the processing until the
          application is idle
●   另一種處理耗時事件的方式:推遲至應用程式空
    閒時再處理
●   使用一個 0 毫秒定時器。只要沒有其他尚待處理
    的事件,就觸發該定時器。

Contenu connexe

Plus de Shih-Hsiang Lin

Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache AntShih-Hsiang Lin
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageShih-Hsiang Lin
 
Ch6 file, saving states, and preferences
Ch6 file, saving states, and preferencesCh6 file, saving states, and preferences
Ch6 file, saving states, and preferencesShih-Hsiang Lin
 
[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9Shih-Hsiang Lin
 
Ch5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetCh5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetShih-Hsiang Lin
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfacesShih-Hsiang Lin
 
Ch3 creating application and activities
Ch3 creating application and activitiesCh3 creating application and activities
Ch3 creating application and activitiesShih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4Shih-Hsiang Lin
 
Introduction to homography
Introduction to homographyIntroduction to homography
Introduction to homographyShih-Hsiang Lin
 
Project Hosting by Google
Project Hosting by GoogleProject Hosting by Google
Project Hosting by GoogleShih-Hsiang Lin
 
An Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelAn Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelShih-Hsiang Lin
 

Plus de Shih-Hsiang Lin (13)

Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache Ant
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
Ch6 file, saving states, and preferences
Ch6 file, saving states, and preferencesCh6 file, saving states, and preferences
Ch6 file, saving states, and preferences
 
[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9[C++ gui programming with qt4] chap9
[C++ gui programming with qt4] chap9
 
Ch5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internetCh5 intent broadcast receivers adapters and internet
Ch5 intent broadcast receivers adapters and internet
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfaces
 
Ch3 creating application and activities
Ch3 creating application and activitiesCh3 creating application and activities
Ch3 creating application and activities
 
[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4[C++ GUI Programming with Qt4] chap4
[C++ GUI Programming with Qt4] chap4
 
Function pointer
Function pointerFunction pointer
Function pointer
 
Introduction to homography
Introduction to homographyIntroduction to homography
Introduction to homography
 
Git basic
Git basicGit basic
Git basic
 
Project Hosting by Google
Project Hosting by GoogleProject Hosting by Google
Project Hosting by Google
 
An Introduction to Hidden Markov Model
An Introduction to Hidden Markov ModelAn Introduction to Hidden Markov Model
An Introduction to Hidden Markov Model
 

[C++ GUI Programming with Qt4] chap7

  • 1. C++ GUI Programming with Qt 4 Chap7. Event Processing
  • 2. Outline ● 重新實現事件處理器 ● 安裝事件過濾器 ● 處理密集時的響應保持
  • 3. 7.1 QEvent ● The QEvent class is the base class of all event classes ● Qt's main event loop (QCoreApplication::exec()) fetches native window system events from the event queue, translates them into QEvents, and sends the translated events to QObjects.
  • 4. QWidget::event() ● This is the main event handler; it handles event event ● You can re-implement this function in a subclass, but we recommend using one of the specialized event handlers instead ( keyPressEvent(), mouseMoveEvent(), paintEvent(), etc... ).
  • 5. Common Event Types (1) ● 鍵盤事件 : keyPressEvent(), keyReleaseEvent()
  • 6. Tab & BackTab ● 當安排的事件非預期行為時,利用重新實現 QObject::event() → QWidget::event() 在窗口 widget 調用 keyPressEvent() 之前,先攔截處 理
  • 7. QAction, QShortcut ● 利用 Action 當作 signal 發出者,綁定一個鍵盤 事件,然後 trigger 一個 slot 反應函數
  • 8. Common Event Types (2) ● 定時器事件 ● 大多數事件類型發生是因為用戶的動作,但定時 器事件允許應用程序在一定的時間間隔後執 行事件處理 ( 游標閃爍、動畫播放、簡單顯示 的刷新,等等 ...)
  • 9. 跑馬燈範例 ● 利用 showEvent() 和 hideEvent() 啟動和關閉 定時器 ● timerEvent() 每隔若干時間會被呼叫 ● 當需要多個定時器時,保持對所有定時器 ID 的 追蹤會變得很麻煩。此時,更簡單的方式是為每 一個定時器分別建立一個 QTimer object 。
  • 10. 7.2 Event Filter ● QObject instance 在看到他自己的事件之前,可 以通過設置另外一個 QObject instance 監視 / 攔 截 這些事件 ● 當許多 Qt 物件需要針對某一致事件產生反應, 利用 Event Filter 機制,由另外一個 QObject 將 處理集中於一處。 ● InstallEventFilter(), eventFilter()
  • 11. 5 levels event processing ● 重新實現特殊的事件處理器 ● MousePressEvent(), keyPressEvent(), etc... ● 重新實現 QObject::event() ● 覆蓋 Tab 鍵的預設語意 ● 在 QObject 中安裝事件過濾器 ● 在 QApplication object 中安裝事件過濾器 ● 應用程序中每個 object 的每個事件都會在發送到其他事件過 濾器之前,先發送給這個 eventFilter() 函數,適用於 debug 的時候 ● Subclassing Qapplication 並且重新實現 notify()
  • 13. 7.3 Staying Responsive ● 處理一個事件時,也可能同時產生其他的事件,並且將 其追加到 Qt 的事件陣列中 ● 當處理一個特定事件上耗費的時間過多 (I/O 存取 ) ,用 戶介面就會變得無法響應 ● 解法 ● 1. 多線程 ● 2. 在 I/O 存取的代碼中頻繁調用 QApplication::processEvents() - 這個函數告訴 Qt 處理所有 那些還沒有被處理的各類事件,然後再將控制權返回給調用 者。實際上, QApplication::exec() 就是一個不停調用 processEvents() 的 while 循環
  • 15. Defer the processing until the application is idle ● 另一種處理耗時事件的方式:推遲至應用程式空 閒時再處理 ● 使用一個 0 毫秒定時器。只要沒有其他尚待處理 的事件,就觸發該定時器。