2

我最近开始使用Juce 库。我通常在其论坛上发布与 Juce 相关的问题,但我一直在为一个问题苦苦挣扎,但我仍然没有收到任何答案。所以 stackoveflow 确实值得一试,即使这里似乎没有很多 Juce 的用户。

这是问题:

我正在用 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 的第二次调用(我在之后禁用鼠标单击)会使事情正常工作(即使这不是我想要获得的结果,因为我需要重新禁用鼠标事件零件)。

这些事实可以使我想到两个考虑:

  1. 即使手动将鼠标事件传递给它的 mouseDown 方法,组件也需要拦截鼠标点击(这是真的吗?我不太确定)
  2. 在 FileBrowserComponent 中处理鼠标事件之后,还有其他类使用其拦截鼠标单击状态的信息,否则如果在 target->mouseDown(newEvent) 之后,它会起作用,我将再次禁用鼠标单击。任何想法?

提前致谢

4

2 回答 2

0

您最好从 Component 创建派生类,并在这些派生类中实现它们所需的鼠标事件,当这些事件被触发时,您可以向父类发布消息,以便它可以做其他事情。

于 2013-10-23T18:16:07.360 回答
0

您似乎正在单击 FileBrowserComponent 的子组件。并且子级的鼠标事件不会转发回父级(FileBrowserComponent)。这就是为什么当您单击内部时,父级将不会收到该事件。

所以不要去顶部->底部,你应该去底部->顶部。为此:您应该通过以下方法向子级添加鼠标侦听器:

addMouseListener (MouseListener *newListener, 
                  bool wantsEventsForAllNestedChildComponents)

因此,对于您展示的示例:

child->addMouseListener(this, true);

而且 mouseDown() 方法应该是这样的:

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
    }
}

希望这有帮助。

于 2015-03-10T19:27:43.760 回答