Juce c++ - 鼠标事件不起作用
Juce c++ - mouse events don't work
我刚开始使用 C++ 的 Juce GUI 库。我正在尝试创建自定义列表框,稍后我将在其中显示文件名。现在我需要在鼠标为 in/out/clicked 时更改此自定义列表框的行的背景颜色。问题是 MouseEnter()、MouseExit() 和 MouseUp() 不起作用。这是代码:
class LeftExplorerItem : public Component, public MouseListener {
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
addMouseListener(this, true);
}
void paint(Graphics& g) override {
if (!isActive) g.setColour(Colour(40, 40, 40));
else g.setColour(Colour(150, 190, 255));
g.fillRoundedRectangle(2, 2, getWidth() - 4, getHeight() - 4, 4);
g.setColour(Colours::white);
g.drawText("Frame #", 40, 0, 100, 25, Justification::centredLeft);
}
void mouseEnter(const MouseEvent& event) override {
AlertWindow("", "", AlertWindow::AlertIconType::InfoIcon);
isActive = true;
}
void mouseExit(const MouseEvent& event) override {
isActive = false;
}
void mouseUp(const MouseEvent& event) override {
AlertWindow("", "click", AlertWindow::AlertIconType::InfoIcon);
}
void resized() override {
}
private:
bool isActive;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LeftExplorerItem)
};
这只是我的自定义列表框中每个项目的 class。请记住,一切看起来都很好(列表框、所有列表框项目等),唯一的问题是鼠标事件永远不会被触发。这里缺少什么?
你不需要在这里从MouseListener
派生——Component
class有它自己的所有鼠标更新方法的内置版本,所有这些都有与 MouseListener
class 中的签名相同。
a) 从 MouseListener
中删除派生并且不向组件添加鼠标侦听器。一切正常。
class LeftExplorerItem : public Component /*, public MouseListener*/ {
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
// addMouseListener(this, true);
}
b) 创建一个从 MouseListener
派生的单独的 class 以添加您想要的逻辑,并将指向该类型对象的指针传递给 addMouseListener
方法(但是这可能不是你想要的)。
文档说 MouseListener class 的目的是 "If you need to get informed about mouse events in a component but can't or don't want to override its methods, you can attach any number of listeners to the component, and these will get told about the events in addition to the component's own callbacks being called." 在我看来,多重继承在这里不必要地妨碍了您。
编辑:您的这个版本 class 在鼠标移动时改变颜色 enters/exits:
class LeftExplorerItem : public Component
{
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
//addMouseListener(this, true);
}
~LeftExplorerItem()
{
}
void paint (Graphics& g) override
{
if (!isActive) g.setColour(Colour(40, 40, 40));
else g.setColour(Colour(150, 190, 255));
g.fillRoundedRectangle(2, 2, getWidth() - 4, getHeight() - 4, 4);
g.setColour(Colours::white);
g.drawText("Frame #", 40, 0, 100, 25, Justification::centredLeft);
}
void resized() override
{
// This method is where you should set the bounds of any child
// components that your component contains..
}
void mouseEnter(const MouseEvent& event) override {
//AlertWindow("", "", AlertWindow::AlertIconType::InfoIcon);
isActive = true;
repaint();
}
void mouseExit(const MouseEvent& event) override {
isActive = false;
repaint();
}
void mouseUp(const MouseEvent& event) override {
//AlertWindow("", "click", AlertWindow::AlertIconType::InfoIcon);
repaint();
}
private:
bool isActive;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LeftExplorerItem)
};
我刚开始使用 C++ 的 Juce GUI 库。我正在尝试创建自定义列表框,稍后我将在其中显示文件名。现在我需要在鼠标为 in/out/clicked 时更改此自定义列表框的行的背景颜色。问题是 MouseEnter()、MouseExit() 和 MouseUp() 不起作用。这是代码:
class LeftExplorerItem : public Component, public MouseListener {
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
addMouseListener(this, true);
}
void paint(Graphics& g) override {
if (!isActive) g.setColour(Colour(40, 40, 40));
else g.setColour(Colour(150, 190, 255));
g.fillRoundedRectangle(2, 2, getWidth() - 4, getHeight() - 4, 4);
g.setColour(Colours::white);
g.drawText("Frame #", 40, 0, 100, 25, Justification::centredLeft);
}
void mouseEnter(const MouseEvent& event) override {
AlertWindow("", "", AlertWindow::AlertIconType::InfoIcon);
isActive = true;
}
void mouseExit(const MouseEvent& event) override {
isActive = false;
}
void mouseUp(const MouseEvent& event) override {
AlertWindow("", "click", AlertWindow::AlertIconType::InfoIcon);
}
void resized() override {
}
private:
bool isActive;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LeftExplorerItem)
};
这只是我的自定义列表框中每个项目的 class。请记住,一切看起来都很好(列表框、所有列表框项目等),唯一的问题是鼠标事件永远不会被触发。这里缺少什么?
你不需要在这里从MouseListener
派生——Component
class有它自己的所有鼠标更新方法的内置版本,所有这些都有与 MouseListener
class 中的签名相同。
a) 从 MouseListener
中删除派生并且不向组件添加鼠标侦听器。一切正常。
class LeftExplorerItem : public Component /*, public MouseListener*/ {
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
// addMouseListener(this, true);
}
b) 创建一个从 MouseListener
派生的单独的 class 以添加您想要的逻辑,并将指向该类型对象的指针传递给 addMouseListener
方法(但是这可能不是你想要的)。
文档说 MouseListener class 的目的是 "If you need to get informed about mouse events in a component but can't or don't want to override its methods, you can attach any number of listeners to the component, and these will get told about the events in addition to the component's own callbacks being called." 在我看来,多重继承在这里不必要地妨碍了您。
编辑:您的这个版本 class 在鼠标移动时改变颜色 enters/exits:
class LeftExplorerItem : public Component
{
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
//addMouseListener(this, true);
}
~LeftExplorerItem()
{
}
void paint (Graphics& g) override
{
if (!isActive) g.setColour(Colour(40, 40, 40));
else g.setColour(Colour(150, 190, 255));
g.fillRoundedRectangle(2, 2, getWidth() - 4, getHeight() - 4, 4);
g.setColour(Colours::white);
g.drawText("Frame #", 40, 0, 100, 25, Justification::centredLeft);
}
void resized() override
{
// This method is where you should set the bounds of any child
// components that your component contains..
}
void mouseEnter(const MouseEvent& event) override {
//AlertWindow("", "", AlertWindow::AlertIconType::InfoIcon);
isActive = true;
repaint();
}
void mouseExit(const MouseEvent& event) override {
isActive = false;
repaint();
}
void mouseUp(const MouseEvent& event) override {
//AlertWindow("", "click", AlertWindow::AlertIconType::InfoIcon);
repaint();
}
private:
bool isActive;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LeftExplorerItem)
};