【发布时间】: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();
}
但是,无论我如何设置acceptEvents 和synthesizeMouse,当我在我的系统(Windows 7 系统)上使用多点触控显示器时,我总是会同时收到鼠标和触摸事件。当触摸事件被接受时,有没有办法仅在 Windows 上使用 Qt 获取触摸事件?
更新:
实际上未记录的nomousefromtouch parameter 也没有任何效果,但是在 Qt 5.3 中,QMouseEvent::source() 大多数报告Qt::MouseEventSynthesizedBySystem(是的,大多数...在极少数情况下报告Qt::MouseEventNotSynthesized 用于鼠标移动)合成鼠标事件。这意味着我可能能够自己消除事件的歧义(大部分时间),但是更好/更容易/更清洁的解决方案将不胜感激。
【问题讨论】:
标签: c++ windows qt windows-7 qt5