Qt5.5 如何在应用程序级别覆盖 MousePress 事件坐标 [英] Qt5.5 How to override MousePress event coordinates at Application level

查看:42
本文介绍了Qt5.5 如何在应用程序级别覆盖 MousePress 事件坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt5.5 的小型嵌入式 Linux 设备开发应用程序.我需要能够在我的应用程序中修改从 Linux ( tslib ) 接收到的鼠标按下坐标.我尝试在我的主视图中实现一个事件过滤器,它修改接收到的鼠标坐标,创建一个新的鼠标事件并将新事件提交给小部件.但是,在小部件的mousePressEvent 函数中,我只看到了一次调试消息,而且是针对接收到的原始坐标,而不是我截取和修改的坐标.

I'm developing an application on a small Embedding Linux device with Qt5.5. I need to be able to modify the mouse press coordinates received from Linux ( tslib ) in my application. I've tried implementing an event filter in my main view that modifies the mouse coordinate received, creates a new mouse event and submits the new event to the widget. However, in the widget's mousePressEvent function, I see the debug message only once, and it is for the original coordinates received, not my intercepted and modified coordinate.

目前,当我触摸屏幕时,我会收到调试消息,它们看起来像这样:

Currently, when I touch the screen, I get my debug messages and they look like this:

Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Original:  QPoint(192,148)
Mouse New:  QPoint(128,148)
Mouse Press 192 148

如何在顶层拦截鼠标事件,修改它们的坐标,并将它们发布到我的小部件,同时还消耗原始鼠标事件?谢谢!

How can I intercept mouse events at the top level, modify their coordinates, and post them to my widget, while also consuming the original mouse events? Thanks!

main.cpp:

MyWidget w;
app.installEventFilter(&w);
w.show();

我的小部件:

bool MyWidget::eventFilter(QObject *object, QEvent *event)
{

    if ( event->type() == QEvent::MouseButtonPress ) {
        QMouseEvent *orig = static_cast<QMouseEvent*>( event );
        QPoint origLocation = orig->pos();
        qDebug() << "Mouse Original: "<< origLocation;
        int newx = abs(origLocation.x()-320);
        QPoint newPoint(newx,origLocation.y());
        QMouseEvent *newPosEvent = new QMouseEvent(QEvent::MouseButtonPress, newPoint, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
        qDebug() << "Mouse New: " << newPosEvent->pos();
        //qApp->postEvent(this, newPos);
        return QObject::eventFilter(object, newPosEvent);
    }

    return QObject::eventFilter(object, event);
}

void MyWidget::mousePressEvent ( QMouseEvent * event )
{
    qDebug() << "Mouse Press" << event->x() << event->y();
}

推荐答案

您可以通过从 eventFilter()true 来消耗"事件并停止其进一步传播> 方法.如果您不使用事件过滤器,则可以使用其 accept() 方法停止该事件.

You can "consume" the event and stop its further propagation by returning true from the eventFilter() method. If you don't use an event filter, you can stop the event with its accept() method.

您可以使用静态方法重新发布您创建的事件 QCoreApplication::postEvent(obj, evnt);

You can re-post the event you create using the static method QCoreApplication::postEvent(obj, evnt);

此外,您可能希望拦截更深的内容,例如在 QWindow 级别.我的意思是,如果您在小部件级别拦截它,然后再次将其发布到您的小部件,它只会再次被事件过滤器拦截.您没有发布新活动,而是收到了原始活动,因为您没有阻止它的传播.

Also, you might want to intercept the even deeper, for example at QWindow level. I mean, if you intercept it at your widget level, and post it to your widget again, it will just get intercepted by the event filter again. You don't post your new event, and you receive the original, because you didn't stop it from propagating.

此外,如果您想产生点击,您必须连续发布两个事件,即按下和释放事件:

Also, if you want to generate a click, you will have to post two events in a row, a press and and release event:

    QMouseEvent * e1 = new QMouseEvent(QEvent::MouseButtonPress, QPointF(x, y), Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier);
    QCoreApplication::postEvent(obj, e1);
    QMouseEvent * e2 = new QMouseEvent(QEvent::MouseButtonRelease, QPointF(x, y), Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier);
    QCoreApplication::postEvent(obj, e2);

这篇关于Qt5.5 如何在应用程序级别覆盖 MousePress 事件坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆