我创建了一个简单的小部件,它将接收到的鼠标和触摸事件输出到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
)。这意味着我可能能够自己消除事件的歧义(大多数情况下),但更好/更容易/更清洁的解决方案将不胜感激。