问题描述
我覆盖了 QGraphicsScene
并重载了 2 个方法:mouseDoubleClickEvent
和 mouseReleaseEvent
.我想在每个事件上执行不同的逻辑,但我不知道如何区分?在 mouseDoubleClickEvent
之前至少发生了 1 个 mouseReleaseEvent
.
I am overriden QGraphicsScene
and overload 2 methods: mouseDoubleClickEvent
and mouseReleaseEvent
. I want different logic executing on each of this event, but I do not know how to distinguish it? At least 1 mouseReleaseEvent
occured before mouseDoubleClickEvent
.
推荐答案
对于想要在双击时发生的逻辑,将代码放在 mouseDoubleClickEvent()
中想要在鼠标释放时发生,将代码放在 mouseReleaseEvent()
中.
For the logic that you want to occur on a double click, put the code inside mouseDoubleClickEvent()
and for the logic that you want to occur on a mouse release, put the code inside mouseReleaseEvent()
.
如果你想在用户点击但没有双击时做某事,你必须等待看他们是否点击了两次.在第一次释放鼠标时启动一个 200 毫秒的计时器.
If you want to do something when the user clicks but doesn't double click, you have to wait to see if they click twice or not. On the first mouse release start a 200ms timer.
如果您在计时器到期之前收到 mouseDoubleClickEvent()
则是双击,您可以执行双击逻辑.如果计时器在您获得另一个 mouseDoubleClick()
之前到期,那么您就知道这是一次单击.
If you get a mouseDoubleClickEvent()
before the timer expires then it was a double click and you can do the double click logic. If the timer expires before you get another mouseDoubleClick()
then you know it was a single click.
伪代码
main()
{
connect(timer, SIGNAL(timeout()), this, SLOT(singleClick()));
}
mouseReleaseEvent()
{
timer->start();
}
mouseDoubleClickEvent()
{
timer->stop();
}
singleClick()
{
// Do single click behavior
}
这个答案给出了一个相当相似的解决方案.
This answer gives a rather similar solution.
这篇关于QGraphicsScene上如何区分mouseReleaseEvent和mousedoubleClickEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!