Hello forum,


I am playing around with the elastic node example and have the some issue to discuss with:


When the scene loads the nodes are already connected with edges and i want to change this attribute. The edge will be connected with two adjacent nodes in the following manner:

1. When the user move the cursor over one of the adjacent nodes and press the Ctrl modifier and the nodes gets highlighted and remains highlighted as long as the Ctrl button is pressed while the cursor is over the node.

2. When the user move the mouse cursor while the Ctrl button is pressed down , the edge arrow will be drawn from the source node. And When the user cursor reach any of the destination node , the edge arrow will finish drawing at the destination node.

Initially , I did the following changes inside the node class :


1. Inside the constructor , i have set the

Qt Code:
  1. setAcceptHoverEvents(true);
To copy to clipboard, switch view to plain text mode 

I am having trouble with the highlighting effect. I have over-ridden the following functions:


Qt Code:
  1. //the following events are for the items visual appearance
  2. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  3. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  4. //addded by sajjad
  5. void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
  6. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
  7. void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
To copy to clipboard, switch view to plain text mode 

And the definition is as follows:

Qt Code:
  1. /////////////////////////////////////////
  2.  
  3. void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
  4. {
  5. update();
  6. QGraphicsItem::mousePressEvent(event);
  7. }
  8. void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  9. {
  10. update();
  11. QGraphicsItem::mouseReleaseEvent(event);
  12. }
  13. void Node::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
  14. {
  15. if(event->modifiers() == Qt::ControlModifier)
  16. {
  17. update();
  18. QGraphicsItem::hoverMoveEvent(event);
  19. }
  20. }
  21. void Node::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
  22. {
  23. if(event->modifiers() == Qt::ControlModifier)
  24. {
  25. update();
  26. QGraphicsItem::hoverEnterEvent(event);
  27. }
  28. }
  29. void Node::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
  30. {
  31.  
  32. update();
  33. QGraphicsItem::hoverMoveEvent(event);
  34. }
To copy to clipboard, switch view to plain text mode 
/////////////////////////////////////////////

And the nodes paint function is also updatd:


/////////////////////////////////////////////////////////////////

Qt Code:
  1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  2. {
  3. painter->setPen(Qt::NoPen);
  4. painter->setBrush(Qt::darkGray);
  5. painter->drawEllipse(-7, -7, 20, 20);
  6. QRadialGradient gradient(-3, -3, 10);
  7. if (option->state & QStyle::State_Sunken)
  8. {
  9. //button pressed mode
  10. gradient.setCenter(3, 3);
  11. gradient.setFocalPoint(3, 3);
  12. gradient.setColorAt(1, QColor(Qt::yellow).light(120));
  13. gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
  14. //gradient.setColorAt(0, QColor(Qt::red).light(120));
  15. //qDebug() << "Sunken state" << endl;
  16. std::cout << "It is in the sunken state" << std::endl;
  17. }
  18. else if(option->state & QStyle::State_MouseOver)
  19. {
  20. gradient.setColorAt(0, Qt::red);
  21. gradient.setColorAt(1, Qt::darkYellow);
  22. std::cout << "It is in the mosue over state" << std::endl;
  23. }
  24. else if(option->state & (!QStyle::State_Sunken & QStyle::State_MouseOver))
  25. {
  26. gradient.setColorAt(0, Qt::yellow);
  27. gradient.setColorAt(1, Qt::darkYellow);
  28. }
  29. else
  30. {
  31. gradient.setColorAt(0, Qt::yellow);
  32. gradient.setColorAt(1, Qt::darkYellow);
  33. std::cout << "It is in the initial state" << std::endl;
  34. }
  35. painter->setBrush(gradient);
  36. painter->setPen(QPen(Qt::black, 0));
  37. painter->drawEllipse(-10, -10, 20, 20);
  38. }
To copy to clipboard, switch view to plain text mode 

//////////////////////////////////////////////////////////////////


When i press the mouse button over a node , it sunken and the view is updated , but when i release the button , the node gets highlighted which should not happen. Node will only be highlighted only when the Ctrl modifier will be pressed.

If my explanation is not clear enough, please ask me for further elaboration.

I need some hint to resolve the issue.



Regards
Sajjad