如何获取点击 QGraphicsItem 的事件,并收到 focusOut 通知
我有一个从 QGraphicsItem 实现的类,称为 Node。
我有一个矩形“节点”,当用户单击它时我可以执行某些操作,
我使用 mousePress 和 mouseRelease 事件。
但我希望当用户单击“超出”矩形形状时收到通知。
我尝试实现这些功能:
Qt 代码:
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;
}
如果我单击矩形内或外,这些功能不会做出反应。 我应该自己设置一个逻辑,例如获取鼠标位置并控制它是否超出矩形? 或者有内置的方法吗?
或者“Node”对象如何知道是否单击了其他 Node 对象?
我也想知道,谷歌搜索但找不到 focusinevent 和 focusoutevent 何时触发?我想当我单击该项目然后单击该项目时, focusOutEvent 必须起作用,我错了吗?
谢谢你的主意。
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 ( 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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在构建节点时,您需要执行以下操作:
第一行使您的项目实际上能够接收焦点,而后者使您的项目能够收到鼠标事件的通知。
You need to do the following when you construct your nodes:
The first line makes your item actually capable of receiving focus, and the latter makes it so your item is notified of mouse events.
您是否使用 QGraphicsItem 调用了图形项目的 setFlags 方法: :ItemIsSelectable 或 QGraphicsItem::ItemIsMovable ?
根据 QT 文档。
Have you called setFlags method of your graphics item with QGraphicsItem::ItemIsSelectable or QGraphicsItem::ItemIsMovable ?
According to QT doc.