使用Qt进行纯键盘操作界面开发时,经常需要获取当前焦点所在的部件,Qt提供了一个快速获取该部件的方法;
QWidget *QWidget::focusWidget() const
官方文档:
Returns the last child of this widget that setFocus had been called on. For top level widgets this is the widget that will get focus in case this window gets activated
This is not the same as QApplication::focusWidget(), which returns the focus widget in the currently active window.
有道翻译:
返回setFocus被调用的这个小部件的最后一个子部件。对于顶级小部件,这是在窗口被激活时获得焦点的小部件
这与QApplication::focusWidget()不同,后者在当前活动窗口中返回焦点小部件。
示例:
QLineEdit* lineEditFocus =
qobject_cast<QLineEdit*>(ui->widget->focusWidget());