【问题标题】:Qt::AA_SynthesizeMouseForUnhandledTouchEvents on WindowsWindows 上的 Qt::AA_SynthesizeMouseForUnhandledTouchEvents
【发布时间】:2014-07-22 12:04:57
【问题描述】:

我创建了一个简单的小部件,它将接收到的鼠标和触摸事件输出到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 parameter 也没有任何效果,但是在 Qt 5.3 中,QMouseEvent::source() 大多数报告Qt::MouseEventSynthesizedBySystem(是的,大多数...在极少数情况下报告Qt::MouseEventNotSynthesized 用于鼠标移动)合成鼠标事件。这意味着我可能能够自己消除事件的歧义(大部分时间),但是更好/更容易/更清洁的解决方案将不胜感激。

【问题讨论】:

    标签: c++ windows qt windows-7 qt5


    【解决方案1】:

    我没有像你提到的那样设置属性。 但是,当我在应用程序开始时将设置属性的方法从app.setAttribute() 更改为QCoreApplication::setAttribute() 时,事情开始对我有用。

    你应该试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多