Qt中的焦點事件

pamxy發表於2013-05-28

轉自:http://blog.csdn.net/kangroger/article/details/7744077

 

在應用程式中,都會有一個當前視窗,即當前獲得焦點事件的視窗,這個視窗可以接受鍵盤的輸入。當應用有多個視窗時就要使用焦點事件了!

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”,自己動手吧!!

 

 

相關文章