Qt::AA_SynthesizeMouseForUnhandledTouchEvents on Windows

Qt::AA_SynthesizeMouseForUnhandledTouchEvents on Windows

本文关键字:on Windows SynthesizeMouseForUnhandledTouchEvents AA Qt      更新时间:2023-10-16

我创建了一个简单的小部件,它将收到的鼠标和触摸事件输出到qDebug()(摘录):

// ...
MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent), acceptEvents(false)
{
    this->setAttribute(Qt::WA_AcceptTouchEvents);
}
static QEvent::Type const mouseEventTypes[] = {
    QEvent::MouseButtonDblClick,
    QEvent::MouseButtonRelease,
    QEvent::MouseButtonPress,
    QEvent::MouseMove
};
static QEvent::Type const touchEventTypes[] = {
    QEvent::TouchBegin,
    QEvent::TouchUpdate,
    QEvent::TouchEnd
};
template<typename Container, typename Value>
bool contains(Container const & c, Value const & v)
{
    return std::find(std::begin(c), std::end(c), v) != std::end(c);
}
bool MyWidget::event(QEvent * e)
{
    auto type = e->type();
    if(contains(mouseEventTypes, type))
        qDebug() << "MouseEvent";
    else if(contains(touchEventTypes, type))
        qDebug() << "TouchEvent";
    else
        return QWidget::event(e);
    e->setAccepted(this->acceptEvents);
    return true;
}
// ...
const bool acceptEvents = true;
const bool synthesizeMouse = false;
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    app.setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents, synthesizeMouse);
    MainWindow gui; // contains a MyWidget as centralWidget
    gui.setAcceptEvents(acceptEvents);
    gui.show();
    return app.exec();
}

然而,无论我如何设置acceptEventssynthesizeMouse,当我在我的系统(这是一个Windows 7系统)上使用多点触控监视器时,我总是收到鼠标和触摸事件。是否有任何方法只有获得触摸事件在Windows上使用Qt触摸事件被接受?

更新:

有效未记录的nomousefromtouch参数也没有影响,但是在Qt 5.3中,QMouseEvent::source()报告Qt::MouseEventSynthesizedBySystem大多数(是的,大多数……)在非常罕见的情况下,报告了一个Qt::MouseEventNotSynthesized用于鼠标移动)合成鼠标事件。这意味着我可能能够自己消除事件的歧义(大多数时候),但是更好/更容易/更干净的解决方案将是值得赞赏的。

我没有运气设置属性,正如你所说的。然而,当我在应用程序开始时将设置属性的方法从app.setAttribute()更改为QCoreApplication::setAttribute()时,事情开始为我解决。

你应该试试