[C++ GUI Programming with Qt4] chap7
Click here to load reader
-
Upload
browny-lin -
Category
Technology
-
view
1.140 -
download
7
Transcript of [C++ GUI Programming with Qt4] chap7
![Page 1: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/1.jpg)
C++ GUI Programming with Qt 4
Chap7. Event Processing
![Page 2: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/2.jpg)
Outline
● 重新實現事件處理器
● 安裝事件過濾器
● 處理密集時的響應保持
![Page 3: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/3.jpg)
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.
![Page 4: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/4.jpg)
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... ).
![Page 5: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/5.jpg)
Common Event Types (1)
● 鍵盤事件 : keyPressEvent(), keyReleaseEvent()
![Page 6: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/6.jpg)
Tab & BackTab
● 當安排的事件非預期行為時,利用重新實現QObject::event() → QWidget::event() 在窗口widget 調用 keyPressEvent() 之前,先攔截處理
![Page 7: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/7.jpg)
QAction, QShortcut
● 利用 Action當作 signal發出者,綁定一個鍵盤事件,然後 trigger一個 slot反應函數
![Page 8: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/8.jpg)
Common Event Types (2)
● 定時器事件
● 大多數事件類型發生是因為用戶的動作,但定時器事件允許應用程序在一定的時間間隔後執行事件處理 (游標閃爍、動畫播放、簡單顯示的刷新,等等 ...)
![Page 9: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/9.jpg)
跑馬燈範例
● 利用 showEvent() 和 hideEvent() 啟動和關閉定時器
● timerEvent() 每隔若干時間會被呼叫
● 當需要多個定時器時,保持對所有定時器 ID的追蹤會變得很麻煩。此時,更簡單的方式是為每一個定時器分別建立一個 QTimer object。
![Page 10: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/10.jpg)
7.2 Event Filter
● QObject instance在看到他自己的事件之前,可以通過設置另外一個 QObject instance監視 /攔截 這些事件
● 當許多 Qt物件需要針對某一致事件產生反應,利用 Event Filter機制,由另外一個 QObject將處理集中於一處。
● InstallEventFilter(), eventFilter()
![Page 11: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/11.jpg)
5 levels event processing
● 重新實現特殊的事件處理器
● MousePressEvent(), keyPressEvent(), etc...
● 重新實現QObject::event()● 覆蓋 Tab鍵的預設語意
● 在QObject中安裝事件過濾器
● 在QApplication object中安裝事件過濾器● 應用程序中每個 object的每個事件都會在發送到其他事件過濾器之前,先發送給這個 eventFilter()函數,適用於 debug的時候
● Subclassing Qapplication 並且重新實現 notify()
![Page 12: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/12.jpg)
Event Propagation
![Page 13: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/13.jpg)
7.3 Staying Responsive
● 處理一個事件時,也可能同時產生其他的事件,並且將其追加到Qt的事件陣列中
● 當處理一個特定事件上耗費的時間過多 (I/O存取 ),用戶介面就會變得無法響應
● 解法
● 1. 多線程● 2. 在 I/O存取的代碼中頻繁調用
QApplication::processEvents() - 這個函數告訴Qt處理所有那些還沒有被處理的各類事件,然後再將控制權返回給調用者。實際上,QApplication::exec()就是一個不停調用processEvents()的while循環
![Page 14: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/14.jpg)
qApp->processEvents(QEventLoop::ExcludeUserInputEvents) 告訴 Qt忽略滑鼠和鍵盤事件
![Page 15: [C++ GUI Programming with Qt4] chap7](https://reader038.fdocuments.net/reader038/viewer/2022100506/555ad867d8b42a024a8b4da4/html5/thumbnails/15.jpg)
Defer the processing until the application is idle
● 另一種處理耗時事件的方式:推遲至應用程式空閒時再處理
● 使用一個 0毫秒定時器。只要沒有其他尚待處理的事件,就觸發該定時器。