2011-05-11 38 views
2

我最近開始使用Juce library。我通常會在論壇上發佈Juce相關問題,但是我很多時候都在爲一些問題煩惱,而我仍然沒有得到答案。所以即使看起來Juce的用戶並不多,stackoveflow也值得一試。在Juce庫中的FileBrowserComponent上攔截鼠標事件

這裏的問題:

我正在做一些實驗用JUCE的組件。 我有以下類:

class MyComponent : public Component{ 

public : 
MyComponent(Component * comp){ 
    this->child = comp; 
    comp->setInterceptsMouseClicks(false, false); 

} 
void mouseDown (const MouseEvent &e){ 

    //do stuff 
    Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class 
    if (target != NULL && doIWantToForwardEventToTarget()){ 
     MouseEvent newEvent = e.getEventRelativeTo(target); 
     target->mouseDown(newEvent); 
    } 
} 
void mouseMove (const MouseEvent &e); 
void mouseEnter (const MouseEvent &e); 
void mouseExit (const MouseEvent &e); 
void mouseDrag (const MouseEvent &e); 
void mouseUp (const MouseEvent &e); 
void mouseDoubleClick (const MouseEvent &e); 
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY); 

private: 

Component *child; 
} 

這個類的目的是:

  1. 商店 層次組件(子)

  2. 攔截所有的鼠標事件的單個組件相關 給孩子或其中的一個子孫

  3. 做點事
  4. 的MouseEvent最終轉發到 它是針對 到

我想這個類的滑塊組件的孩子,甚至是嵌套在其它組件內的組件..一切工作正常。 現在我正在用FileBrowserComponent做一些實驗,看起來不能正常工作。例如,當我點擊按鈕移動到上面的目錄時,它不會(按鈕接收鼠標事件並且它被點擊,但樹視圖中沒有任何事情發生)。 也從列表中選擇項目不起作用。

可能是什麼問題? (我做了一些實驗,似乎沒有調用FileBrowserComponent中的buttonClicked方法,但我不知道爲什麼) 任何建議?

我還試圖修改代碼是這樣的:

void mouseDown (const MouseEvent &e){ 

    //do stuff 
    Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class 
    if (target != NULL && doIWantToForwardEventToTarget()){ 
     target->setInterceptsMouseClicks(true, true); 
     MouseEvent newEvent = e.getEventRelativeTo(target); 
     target->mouseDown(newEvent); 
     target->setInterceptsMouseClicks(false, false); 
    } 
} 

它仍然無法正常工作。無論如何,我發現如果我評論第二次調用setInterceptMouseClicks(我禁用鼠標點擊後)使事情工作(即使這不是我想要獲得的結果,因爲我需要可重新分配的鼠標事件零件)。

這些事實可以使我2點的考慮:

  1. 的組件需要截取鼠標即使鼠標事件 手工傳遞給它的鼠標按下 方法(這是真的,我沒有那麼 點擊? 肯定沒有)
  2. 鼠標事件 FileBrowserComponent處理後還有其他 類使用的 信息的攔截鼠標點擊狀態, 否則如果在 target-> mouseDown(newEvent)後,我會 再次禁用鼠標點擊。任何 的想法?

在此先感謝

+0

我正面臨同樣的問題。你設法解決它嗎? – Dinaiz 2011-06-27 03:00:16

+0

不..我仍然沒有答案,也沒有解決方案.. – Heisenbug 2011-06-27 10:33:42

回答

0

你最好創建一個從組件派生類,在這些派生類實現自己所需的鼠標事件,當這些事件被觸發,你可以發佈一條消息,父類,所以它可以做其他的事情。

0

看起來您正在點擊FileBrowserComponent的子組件。並且該子項的鼠標事件不會轉發回父項(FileBrowserComponent)。這就是爲什麼當你點擊裏面時,父母不會收到事件。

所以不要去頂部 - >底部,你應該去底部 - >頂部。要做到這一點:

addMouseListener (MouseListener *newListener, 
        bool wantsEventsForAllNestedChildComponents) 

所以對於例如,你正在展示:你應該用這種方法鼠標監聽器添加到孩子

child->addMouseListener(this, true); 

而且方法鼠標按下()應該是這樣的:

void mouseDown (const MouseEvent &e){ 

    if(this->child == e.eventComponent){ 
     // this mouse event is coming from 
     // the child component or child's children 
     // as wantsEventsForAllNestedChildComponents = true 

     // do something 
    } 
} 

希望得到這個幫助。