0

如果我这样做this->setFocusPolicy(Qt::WheelFocus);了,如果我将鼠标滚轮放在小部件上方,它将获得焦点。如果我要这样做,this->setFocusPolicy(Qt::ClickFocus);但鼠标滚轮会导致它失去焦点,即使指针在同一个小部件中也是如此。那么,我如何专注于点击,但将其保持在鼠标滚轮上?

经过进一步调查:mouseWheelEvent 总是在焦点事件之后处理。所以试图设置一个变量来记住这是否是由于鼠标滚轮不起作用。此外,FocusReason 中给出的原因是鼠标在滚轮或点击的情况下,所以这也无济于事。

4

2 回答 2

0

枚举 Qt::FocusPolicy

This enum type defines the various policies a widget can have with respect to acquiring keyboard focus.
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::WheelFocus由 组成StrongFocus由 组成ClickFocus,因此您只需设置 WheelFocus 即可获取所有先前的。

于 2012-12-28T06:58:02.810 回答
0

The following does the trick:

Foo::Foo(){
    this->setFocusPolicy(Qt::WheelFocus);
}

void Foo::focusInEvent( QFocusEvent *event ){
    if (!(QApplication::mouseButtons() & Qt::LeftButton)){
        this->clearFocus();
    }
}
于 2012-12-28T16:51:29.573 回答