开发者

How to get event of clicking out of a QGraphicsItem, being notified of focusOut

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:

Qt Code:

void Node::focusInEvent ( QFocusE开发者_如何转开发vent * 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?

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? I guess focusOutEvent must work when i had clicked in the item, then out of the item, am i wrong?

thanks for idea.


You need to do the following when you construct your nodes:

setFlag( QGraphicsItem::ItemIsFocusable );
setAcceptHoverEvents( true );

The first line makes your item actually capable of receiving focus, and the latter makes it so your item is notified of mouse events.


Have you called setFlags method of your graphics item with QGraphicsItem::ItemIsSelectable or QGraphicsItem::ItemIsMovable ?

According to QT doc.

By default, no flags are enabled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜