PDA

View Full Version : How to get event of clicking out of a QGraphicsItem



tuxit
29th July 2011, 14:08
hello,
i have a class implemented from QGraphicsItem, called Node.

i have a rectangle shaped "Node" and i can do something when user clicked on it

i use mousePress and mouseRelease events.

but i want to be notified when user clicked "out of" the rectangle shape.

i tried to implement these functions:


void Node::focusInEvent ( QFocusEvent * event){
cout<<"in"<<endl;
update();

QGraphicsItem::focusInEvent(event);
}
void Node::focusOutEvent ( QFocusEvent * event ){
cout<<"out"<<endl;
update();
QGraphicsItem::focusOutEvent(event);

}
void Node::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
cout<<"out"<<endl;
}



these do not reacts if i click in or out of rectangle.
should i set a logic on my own for example getting the mouse position and control if it is out of rectangle?
or is there a built in method?
thanks for any idea.

Added after 39 minutes:

or how can a "Node" object know if other Node object is clicked?

also i wonder, googled but could not found that when does focusinevent and focusoutevent triggered?

stampede
29th July 2011, 15:21
If you want to click out of object shape and be notified, you need to capture the event in GraphicsScene mousePressEvent.

or how can a "Node" object know if other Node object is clicked?
If Node inherits QGraphicsObject, it can emit a signal connected to other Node objects slots. If not, then you can capture the click in Scene's mousePressEvent, check if node will be under cursor (so it will get clicked) and notify other Nodes on scene manually.

tuxit
31st July 2011, 16:25
thanks for the reply
i achieved to run focusInEvent and focusOutEvent by seting flags by

setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocu sable);

i have asked about to be notified on click on other object because i could not get worked focusOut. now it s ok.
thanks