PDA

View Full Version : Highlight QGraphicsItem during connection.



salcin
9th September 2013, 16:18
Hi,

I'm playing around with the Diagramscene example provided with QT. What I would like to accomplish is to highlight the DiagramItem when connecting two boxes (e.g. process box).

I have tried to implement the hoverEnter/hoverLeave event and it seems to work for the box I start the connect. When I enter the other box while dragging a line no events are passed to the other DiagramItem. When I release the mousebutton I get hoverEnter events.

How can I make this work?

wysota
9th September 2013, 16:53
It depends how you implemented what you have so far.

salcin
9th September 2013, 17:10
I started with Diagramscene example.
In DiagramItem.cpp/.h I override:


void DiagramItem::hoverEnterEvent(QGraphicsSceneHoverEv ent * event)
{
qDebug() << (int)this << ": Hover enter event";
m_color = Qt::cyan;
QGraphicsPolygonItem::hoverEnterEvent(event);
}

void DiagramItem::hoverLeaveEvent(QGraphicsSceneHoverEv ent * event)
{
qDebug() << (int)this << ": Hover leave event";
m_color = Qt::black;
QGraphicsPolygonItem::hoverLeaveEvent(event);
}

void DiagramItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option,
QWidget * widget)
{
if (option->state & QStyle::State_MouseOver )
{
qDebug() << (int)this << "Mouse over!";
}

QPen p = pen();
p.setColor(m_color);
setPen(p);
qDebug() << m_color;
QGraphicsPolygonItem::paint(painter, option, widget);
}


in void DiagramScene::mousePressEvent(QGraphicsSceneMouseE vent *mouseEvent) I added item->setAcceptHoverEvents(true);

salcin
10th September 2013, 08:43
Ok, I solved it.
Instead of putting code in hoverenter/hoverleave of the DiagramItem, I put the code in scene's mouseMoveEvent. To know wich item to highlight I reused some of the code in mouseReleseEvent.