焦点事件是否适用于 QDockWidget objects?

Do focus events work for QDockWidget objects?

我无法让 "focusInEvent" 在 PyQt4 中为 QDockWidget-derived class 工作。我四处寻找,似乎我的事件重新实现语法是正确的,但我在事件处理程序中的代码从未被执行(我在事件代码块中设置了一个断点,但它从未被触发)。这是我所做的:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ToolWindow(QDockWidget):
    def __init__(self, title, parent = None):
        super(ToolWindow, self).__init__(title, parent)

         ...initialization code here...

    def focusInEvent(self, event):
        ..do on-focus work here...

在运行时,我通过 mouse-clicking 将焦点设置到停靠栏上。我同时单击了停靠栏 window 标题栏和停靠栏客户端 space,但没有任何事件触发。我在我的应用程序中单击其他 "ToolWindow" objects,其中 none 在聚焦时触发事件。这不是用于该目的的正确事件吗?另外,我尝试四处切换,但也没有用。非常感谢任何建议。

问题是很多 QWidgets 都有 focusPolicy attribute in Qt::NoFocus mode, then according to doc.

Qt::TabFocus: the widget accepts focus by tabbing.

Qt::ClickFocus: the widget accepts focus by clicking.

Qt::StrongFocus: the widget accepts focus by both tabbing and clicking. On macOS this will also be indicate that the widget accepts tab focus when in 'Text/List focus mode'.

Qt::WheelFocus: like Qt::StrongFocus plus the widget accepts focus by using the mouse wheel.

Qt::NoFocus : the widget does not accept focus.

因此,为了激活焦点,我建议将 属性 更改为 Qt::StrongFocus.

class ToolWindow(QtGui.QDockWidget):
    def __init__(self, title, parent = None):
        super(ToolWindow, self).__init__(title, parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)