在QObject中更改鼠标光标-QGraphicsItem继承的类 [英] Changing mouse cursor in a QObject - QGraphicsItem inherited class
问题描述
我想在图形项中更改鼠标光标(MyCircle继承自QObject
和QGraphicsItem
).
如果我的课程是从QWidget
继承的,我将重新实现enterEvent()
和leaveEvent()
并按如下方式使用它:
I would like to change my mouse cursor when it is in a graphics item (MyCircle inherits from QObject
and QGraphicsItem
).
Had my class inherited from QWidget
, I would have reimplemented enterEvent()
and leaveEvent()
and use it as follows :
MyCircle::MyCircle(QObject *parent)
: QWidget(parent), QGraphicsItem() // But I can't
{
rect = QRect(-100,-100,200,200);
connect(this,SIGNAL(mouseEntered()),this,SLOT(in()));
connect(this,SIGNAL(mouseLeft()),this,SLOT(out()));
}
void MyCircle::in()
{
QApplication::setOverrideCursor(Qt::PointingHandCursor);
}
void MyCircle::out()
{
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
void MyCircle::enterEvent(QEvent *)
{
emit mouseEntered();
}
void MyCircle::leaveEvent(QEvent *)
{
emit mouseLeft();
}
不幸的是,我需要为该圆圈设置动画(实际上是一个按钮),所以我需要QObject,有没有一种简单的方法来更改光标?
Unfortunately, I need to animate that circle (it's a button actually), so I need QObject, is there an easy way to change the cursor ?
推荐答案
您可能可以使用在您的类构造函数中,请确保您...
In your class constructor make sure you do...
setAcceptHoverEvents(true);
然后覆盖 hoverEnterEvent
和
Then override hoverEnterEvent
and hoverLeaveEvent
.
virtual void hoverEnterEvent (QGraphicsSceneHoverEvent *event) override
{
QGraphicsItem::hoverEnterEvent(event);
QApplication::setOverrideCursor(Qt::PointingHandCursor);
}
virtual void hoverLeaveEvent (QGraphicsSceneHoverEvent *event) override
{
QGraphicsItem::hoverLeaveEvent(event);
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
作为旁注:您实际上是否同时继承了QObject
和 QGraphicsItem
?如果是这样,您可能只需从 QGraphicsObject
.
As a side note: do you actually inherit from both QObject
and QGraphicsItem
? If so, you could probably achieve the same goal by simply inheriting from QGraphicsObject
.
在回答...
我在整个边界区域都有指点图标,我该如何 仅缩小我的绘图区域(在这种情况下为圆形)?
I have the pointing hand icone in my whole bounding rect, how can I reduce the area only to my drawing (in this case a circle) ?
覆盖 QGraphicsItem::shape
以返回表示的QPainterPath
实际形状...
virtual QPainterPath shape () const override
{
QPainterPath path;
/*
* Update path to represent the area in which you want
* the mouse pointer to change. This will probably be
* based on the code in your 'paint' method.
*/
return(path);
}
现在覆盖 QGraphicsItem::hoverMoveEvent
以使用shape
方法...
Now override QGraphicsItem::hoverMoveEvent
to make use of the shape
method...
virtual void hoverMoveEvent (QGraphicsSceneHoverEvent *event) override
{
QGraphicsItem::hoverMoveEvent(event);
if (shape().contains(event->pos())) {
QApplication::setOverrideCursor(Qt::PointingHandCursor);
} else {
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
}
显然,以上内容可能会对性能产生影响,具体取决于所绘制形状的复杂性,因此取决于QPainterPath
.
The above could, obviously, have an impact on performance depending on the complexity of the shape drawn and, hence, the QPainterPath
.
(注意:您可以使用QGraphicsItem::shape > QGraphicsItem::opaque
.)
(Note: rather than using QGraphicsItem::shape
you could do a similar thing with QGraphicsItem::opaque
instead.)
这篇关于在QObject中更改鼠标光标-QGraphicsItem继承的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!