Qt:如何过滤意外(连续)MouseButtonRelease 和 MouseButtonPress 事件?
我的问题是电阻式触摸屏效果不佳。当您在屏幕上拖动手指时。由于 MouseButtonRelease 事件,拖动会随机中断。
我想通过过滤 mouseEvents 并删除所有连续的 MouseButtonRelease 和 MouseButtonPress 事件(如果它们发生的时间少于 100 毫秒)来解决此问题。我想在整个应用程序范围内进行此操作。
我已经尝试过 eventFilter 但它无法正常工作。存储的 MouseButtonRelease 事件不会发送到正确的对象。我通过 installEventFilter(new MouseFilter(this)); 安装了它在一个小部件上,但所有鼠标释放都会丢失。
您建议采用不同的方法还是我的代码有问题?
#include "mousefilter.h"
#include <QApplication>
#include <QEvent>
#include <QMouseEvent>
#include <QTimer>
MouseFilter::MouseFilter(QObject *parent) :
QObject(parent),
storedEvent_(0)
{
}
void MouseFilter::send() {
if ( storedEvent_ == 0 ) {
return;
}
QApplication::sendEvent(parent(), storedEvent_);
delete storedEvent_;
storedEvent_ = 0;
}
bool MouseFilter::eventFilter(QObject *object, QEvent *event) {
if ( event->type() == QEvent::MouseButtonRelease ) {
QMouseEvent* release = static_cast<QMouseEvent*>(event);
// Dalay the event
storedEvent_ = new QMouseEvent(QEvent::MouseButtonRelease, release->pos(), release->globalPos(),
release->button(), release->buttons(), release->modifiers());
QTimer::singleShot(100, this, SLOT(send()));
return true;
}
else if ( storedEvent_ != 0 && event->type() == QEvent::MouseButtonPress ) {
// Clear stored release and ignore new press.
delete storedEvent_;
storedEvent_ = 0;
return true;
}
return QObject::eventFilter(object, event);
}
My problem is a poor resistive touch screen. When you drag your finger across the screen. The drag randomly cuts off because of the MouseButtonRelease event.
I would like to fix this by filtering the mouseEvents and removing all consecutive MouseButtonRelease and MouseButtonPress events if they occured e.g. less than 100ms appart. I would like to do this application-wide.
I already tried an eventFilter but it doesn't work properly. The stored MouseButtonRelease-events don't get sent to right objects. I installed this by installEventFilter(new MouseFilter(this)); at a widget, but all the mouse releases get lost.
Do you suggest a different approach or is there something wrong with my code?
#include "mousefilter.h"
#include <QApplication>
#include <QEvent>
#include <QMouseEvent>
#include <QTimer>
MouseFilter::MouseFilter(QObject *parent) :
QObject(parent),
storedEvent_(0)
{
}
void MouseFilter::send() {
if ( storedEvent_ == 0 ) {
return;
}
QApplication::sendEvent(parent(), storedEvent_);
delete storedEvent_;
storedEvent_ = 0;
}
bool MouseFilter::eventFilter(QObject *object, QEvent *event) {
if ( event->type() == QEvent::MouseButtonRelease ) {
QMouseEvent* release = static_cast<QMouseEvent*>(event);
// Dalay the event
storedEvent_ = new QMouseEvent(QEvent::MouseButtonRelease, release->pos(), release->globalPos(),
release->button(), release->buttons(), release->modifiers());
QTimer::singleShot(100, this, SLOT(send()));
return true;
}
else if ( storedEvent_ != 0 && event->type() == QEvent::MouseButtonPress ) {
// Clear stored release and ignore new press.
delete storedEvent_;
storedEvent_ = 0;
return true;
}
return QObject::eventFilter(object, event);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正处于一个痛苦的世界,去过那里。我向硬件人员投诉,但毫无结果。
一种方法可能是自己完成所有发布活动。基本上,当手指接触时,您会收到一个事件(即使它是静止的)。因此,根据该逻辑,您可以启动计时器,然后在 100 毫秒无触摸事件后触发鼠标释放。
QTimer 的工作方式是,您可以重复调用 .start(),它将重新开始倒计时。
当然,这充满了问题,因为它依赖于 100 毫秒的神奇数字,它降低了应用程序的响应能力,并且只能修复应用程序中的错误,而不是应有的系统范围内的错误。
You are in a world of pain, been there. I complained to the hardware people and got nowhere.
An approach could be to do all the release events yourself. Basically whilst the finger is in contact you get an event (even if its stationary). Therefore based on that logic you can start a timer, then after 100ms of no touch events you fire off the mouse release.
The way that the QTimer works is that you can repeatedly call .start() and it will restart the count down.
Of course this is filled with problems in that its dependant on a magic number of 100ms, it reduces the responsiveness of your application and only fixes the bug in your application not system wide as it should be.