2012-12-28 20 views
0

如果我做this->setFocusPolicy(Qt::WheelFocus);那麼如果我將鼠標滾輪放在小工具上方,它會變得焦點。如果我要做this->setFocusPolicy(Qt::ClickFocus);,但是然後鼠標滾輪會導致它失去焦點,即使指針位於同一個小部件中。那麼,我該如何關注點擊,但將它保留在鼠標滾輪上?如何專注點擊,但將它保持在Qt中的鼠標滾輪?

經過進一步調查:mouseWheelEvent總是在焦點事件之後進行處理。所以試圖設置一個變量來記住這是否是由於鼠標滾輪造成的。此外,FocusReason中給出的原因是在鼠標輪或點擊的情況下,所以也沒有幫助。

回答

0

下面的伎倆:

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

void Foo::focusInEvent(QFocusEvent *event){ 
    if (!(QApplication::mouseButtons() & Qt::LeftButton)){ 
     this->clearFocus(); 
    } 
} 
0

enum 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讓所有的以前的。

+0

與WheelFocus的問題是,它抓住,當我鼠標滾輪在窗口小部件,這是我不想關注。 – chacham15

相關問題