问题描述
请考虑以下代码:
#include <QWidget>
#include <iostream>
#include <QApplication>
class Widget : public QWidget
{
public:
void mousePressEvent(QMouseEvent* event)
{
std::cout << "mousePressEvent" < std::endl;
}
void mouseDoubleClickEvent(QMouseEvent* event)
{
std::cout << "mouseDoubleClickEvent" << std::endl;
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
每次我处理双击时,输出为:
Every time I process double click, the output is:
mousePressEvent
mouseDoubleClickEvent
这意味着Qt总是在按下一次按键后立即调用mousePressEvent,而无需等待第二次按下按键.有没有办法关闭此选项,以便在双击的情况下不执行mousePressEvent调用.
This means Qt always call mousePressEvent as soon as one press proceed without waiting the second press. Is there a way to turn off this option, so that no mousePressEvent call will perform in case of double-click.
推荐答案
我将在等于QApplication::doubleClickInterval()
值的时间段内绕过单击事件的处理(使用QTimer
).如果在此期间未发生双击,则应处理单击",否则应处理双击.
I would bypass the handling of single click event (using a QTimer
) by the period of time equal to the QApplication::doubleClickInterval()
value. If double click is not happened during that time, I should handle "single click", otherwise the double click should be processed.
这篇关于为什么总是在mouseDoubleClickEvent之前调用mousePressEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!