我有两个 QGraphicsRectItem 类型的项目。一个比一个。
第一个是名为 Wall 的自定义类。墙内有门窗。事实上,我在这个自定义 Wall 类中有一个门窗列表。
门也是物品,并且被绘制在墙内。
当我将鼠标移到门上时,会发出墙壁的悬停功能,但不会发出门的悬停功能。他们两个都正确地相互复制了一个,作为虚拟虚空保护。
为什么会这样?我怎样才能让门窗项目意识到悬停?
我有两个 QGraphicsRectItem 类型的项目。一个比一个。
第一个是名为 Wall 的自定义类。墙内有门窗。事实上,我在这个自定义 Wall 类中有一个门窗列表。
门也是物品,并且被绘制在墙内。
当我将鼠标移到门上时,会发出墙壁的悬停功能,但不会发出门的悬停功能。他们两个都正确地相互复制了一个,作为虚拟虚空保护。
为什么会这样?我怎样才能让门窗项目意识到悬停?
我尝试使用 customQGraphicsItem
而不是 custom QGraphicsRectItem
。似乎为Wall
和成功调用了悬停事件处理程序Door
。使用 显式设置时会发生这种QGraphicsItem
情况setAcceptHoverEvents(true)
。这未使用 custom 进行测试QGraphicsRectItem
。
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QPainter>
class Item : public QGraphicsItem
{
QBrush m_brush;
public:
explicit Item(bool nested = true, QGraphicsItem* parent = 0) : QGraphicsItem(parent), m_brush(Qt::white)
{
if (nested) {
Item* item = new Item(false, this);
item->setPos(10,10);
m_brush = Qt::red;
}
setAcceptHoverEvents(true);
}
QRectF boundingRect() const
{
return QRectF(0,0,100,100);
}
void hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
m_brush = Qt::red;
update();
}
void hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
m_brush = Qt::white;
update();
}
void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
p->setBrush(m_brush);
p->drawRoundRect(boundingRect());
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene;
scene.addItem(new Item);
QGraphicsView view;
view.setScene(&scene);
view.setMouseTracking(true);
view.show();
return app.exec();
}