首先来说说为什么要设置焦点吧。
设置焦点可以让应用更便捷。比如当你打开百度主页或其他带有编辑框的页面时,不需要先用鼠标点击编辑框就可以直接输入文字等信息到其中。这就是由于搜索框设置了焦点。
一个空间要先设置它焦点事件的模式,即窗口如何接受焦点事件(通过鼠标单击、Tab键、不接受焦点事件等)
用
void
setFocusPolicy ( Qt::FocusPolicy policy )
设置获得焦点的方式
ConstantValueDescriptionQt::TabFocus0x1通过Tab键获得焦点Qt::ClickFocus0x2通过被单击获得焦点Qt::StrongFocusTabFocus | ClickFocus | 0x8可通过上面两种方式获得焦点Qt::WheelFocusStrongFocus | 0x4类似Qt::StrongFocus只不过再加上鼠标滑轮Qt::NoFocus0不能通过上两种方式获得焦点(默认值),setFocus仍可使其获得焦点.当前有焦点事件的窗口只能有一个,当一个窗口获取焦点事件或失去焦点事件时,可能需要相应的操作,或者如
何判断一个才窗口有没有焦点事件。Qt中亦有相应的函数。
这两个就是窗口获取或失去焦点事件的函数,需要我们重写(好多窗口都是从QWidget继承这两个函数的)
bool hasFocus () const
这个函数就是判断当前窗口有没有焦点事件的,返回布尔值。
这两个函数就是设置或清除焦点事件的。
ConstantValueDescriptionQt::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. 分享:转载于:https://www.cnblogs.com/Dennis-mi/articles/5231465.html