您的位置:首页 > 编程语言 > Qt开发

Qt中的焦点事件

2016-10-31 10:28 1046 查看
在应用程序中,都会有一个当前窗口,即当前获得焦点事件的窗口,这个窗口可以接受键盘的输入。当应用有多个窗口时就要使用焦点事件了!

Qt中有很好的焦点事件管理,我在这里抛砖引玉了。一个空间要先设置它焦点事件的模式,即窗口如何接受焦点事件(通过鼠标单击、Tab键、不接受焦点事件等)

voidsetFocusPolicy ( Qt::FocusPolicy policy )
就是设置焦点事件模式的函数,其中函数的参数为

ConstantValueDescription
Qt::TabFocus0x1the widget accepts focus by tabbing.
Qt::ClickFocus0x2the widget accepts focus by clicking.
Qt::StrongFocusTabFocus | ClickFocus | 0x8the 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::WheelFocusStrongFocus | 0x4like Qt::StrongFocus plus the widget accepts focus by using the mouse wheel.
Qt::NoFocus0the 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”,自己动手吧!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: