Windows 上的 Qt::AA_SynthesizeMouseForUnhandledTouchEvents

Joe*_*Joe 5 c++ windows qt windows-7 qt5

我创建了一个简单的小部件,它将它接收到的鼠标和触摸事件输出到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();
}
Run Code Online (Sandbox Code Playgroud)

但是,无论我如何设置acceptEventssynthesizeMouse,当我在系统(Windows 7 系统)上使用多点触控显示器时,我总是会同时收到鼠标和触摸事件。有没有办法当触摸事件被接受时,使用 Qt 在 Windows 上获取触摸事件?

更新:

有效未记录的nomousefromtouch参数也没有影响,但是对于 Qt 5.3,QMouseEvent::source()报告Qt::MouseEventSynthesizedBySystem大多数(是的,大多数......在极少数情况下Qt::MouseEventNotSynthesized报告鼠标移动)合成鼠标事件。这意味着我可能能够自己(大部分时间)消除事件的歧义,但是更好/更容易/更清洁的解决方案将不胜感激。

Age*_*ade 0

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

你应该尝试一下。