Qt中的焦点事件
Qt中的焦點事件
分類: Qt 2012-07-13 14:59 8555人閱讀 評論(2) 收藏 舉報
qtos
目錄(?)[+]
在應用程序中,都會有一個當前窗口,即當前獲得焦點事件的窗口,這個窗口可以接受鍵盤的輸入。當應用有多個窗口時就要使用焦點事件了!
Qt中有很好的焦點事件管理,我在這里拋磚引玉了。一個空間要先設置它焦點事件的模式,即窗口如何接受焦點事件(通過鼠標單擊、Tab鍵、不接受焦點事件等)
void setFocusPolicy ( Qt::FocusPolicy policy )
就是設置焦點事件模式的函數,其中函數的參數為
Constant Value Description
Qt::TabFocus 0x1 the widget accepts focus by tabbing.
Qt::ClickFocus 0x2 the widget accepts focus by clicking.
Qt::StrongFocus TabFocus | ClickFocus | 0x8 the widget accepts focus by both tabbing and clicking. On Mac OS X this will also be indicate that the widget accepts tab focus when in ‘Text/List focus mode’.
Qt::WheelFocus StrongFocus | 0x4 like Qt::StrongFocus plus the widget accepts focus by using the mouse wheel.
Qt::NoFocus 0 the widget does not accept focus.
如果想通過單擊鼠標獲取焦點事件,那么就可以選擇參數Qt::ClickFocus,其他不贅述。
當前有焦點事件的窗口只能有一個,當一個窗口獲取焦點事件或失去焦點事件時,可能需要相應的操作,或者如何判斷一個才窗口有沒有焦點事件。Qt中亦有相應的函數。
void QWidget::focusInEvent ( QFocusEvent * event ) [virtual protected]
void QWidget::focusOutEvent ( QFocusEvent * event ) [virtual protected]
這兩個就是窗口獲取或失去焦點事件的函數,需要我們重寫(好多窗口都是從QWidget繼承這兩個函數的)
bool hasFocus () const
這個函數就是判斷當前窗口有沒有焦點事件的,返回布爾值。
void QWidget::setFocus ( Qt::FocusReason reason )
void QWidget::clearFocus ()
這兩個函數就是設置或清除焦點事件的。
要想知道更多,只需要在“Qt助手”的“索引”中輸入“Focus”,自己動手吧!!
總結
- 上一篇: 推荐我们在B站免费的生信入门基础课程|测
- 下一篇: 生信宝典联合科学出版社在双 11推出生物