gpt4 book ai didi

c++ - Juce c++ - 鼠标事件不起作用

转载 作者:行者123 更新时间:2023-11-28 05:14:11 25 4
gpt4 key购买 nike

我刚开始使用 C++ 的 Juce GUI 库。我正在尝试创建自定义列表框,稍后我将在其中显示文件名。现在我需要在鼠标移入/移出/单击时更改此自定义列表框行的背景颜色。问题是 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)
};

这只是我的自定义列表框的每个项目的类。请记住,一切看起来都很好(列表框、所有列表框项目等),唯一的问题是鼠标事件永远不会被触发。这里缺少什么?

最佳答案

你不需要在这里从 MouseListener 派生 -- Component 类有它自己的所有鼠标更新方法的内置版本,所有这些都有与 MouseListener 类中的签名相同。要么

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 的单独类以添加您想要的逻辑,并将指向该类型对象的指针传递给 addMouseListener 方法(但是这可能不是你想要的)。

文档说 MouseListener 类的目的是“如果您需要了解码件中的鼠标事件但不能或不想覆盖其方法,您可以将任意数量的监听器附加到组件,除了被调用的组件自己的回调之外,这些将被告知有关事件的信息。”在我看来多重继承在这里不必要地妨碍了你。

编辑:这个版本的类在鼠标进入/退出时改变颜色:

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 c++ - 鼠标事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42985614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com