Dear All,
I have re-implemented mouseMoveEvent(QGraphicsSceneMouseEvent *event) in my class which is derived from QGraphicsItem. When i draw an item on scene and move the mouse cursor inside the item, mouseMoveEvent() will not trigger. It will only trigger when i click on the item and move the mouse inside the item (i.e leftclick + move). I have set the setAcceptHoverEvents(true). But still mouseMoveEvent() event doesn't trigger. Can anyone tell how can i resolve this problem such that, mouse move on item trigger mouseMoveEvent(). Following is my code
{
m_Rectangle.setSize(size);
m_Rectangle.moveTopLeft(center);
setZValue(((int)m_Rectangle.top() + (int)m_Rectangle.left())% 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
CMyItem::~CMyItem()
{
}
QRectF CMyItem
::boundingRect() const {
return QRectF(0,
0,
200,
100);
}
{
path.addRect(0, 0, 200, 100);
return path;
}
{
Q_UNUSED(widget);
//QBrush brush(
pen.setColor(Qt::black);
painter->setPen(pen);
painter->drawRect(m_Rectangle);
}
{
update();
event->accept();
}
{
}
{
update();
}
CMyItem::CMyItem(QPointF center, QSizeF size)
{
m_Rectangle.setSize(size);
m_Rectangle.moveTopLeft(center);
setZValue(((int)m_Rectangle.top() + (int)m_Rectangle.left())% 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
CMyItem::~CMyItem()
{
}
QRectF CMyItem::boundingRect() const
{
return QRectF(0, 0, 200, 100);
}
QPainterPath CMyItem::shape() const
{
QPainterPath path;
path.addRect(0, 0, 200, 100);
return path;
}
void CMyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
//QBrush brush(
QPen pen;
pen.setColor(Qt::black);
painter->setPen(pen);
painter->drawRect(m_Rectangle);
}
void CMyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
update();
event->accept();
}
void CMyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseMoveEvent(event);
}
void CMyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseReleaseEvent(event);
update();
}
To copy to clipboard, switch view to plain text mode
Bookmarks