Qt 4.7:通过手势触发上下文菜单
我正在使用 FlickCharm 示例和 QtWebKit 进行手指友好的浏览器练习,这里是 到目前为止我拥有的所有源代码。
我想通过长按(点击并按住)来触发上下文,目前我可以通过 eventFilter()
获取 QGestureEvent 和位置
QGestureEvent *gestureEvent = dynamic_cast<QGestureEvent*>(event);
if (gestureEvent) {
if (const QGesture *g = gestureEvent->gesture(Qt::TapAndHoldGesture)) {
if (g->state() == Qt::GestureStarted) {
qDebug() << "tap-n-hold event started";
QWebView *webView = dynamic_cast<QWebView*>(object);
FlickData *w = d->flickData.value(webView);
qDebug() << w->pressPos; // get position
}
}
}
我的第一次尝试是使用 模拟右键单击事件sendEvent()
:
QMouseEvent pressEvent(QEvent::MouseButtonPress, w->pressPos,
Qt::RightButton, Qt::RightButton, Qt::NoModifier);
QApplication::sendEvent(webView->page(), &pressEvent);
看起来事件确实发送了,但没有触发上下文菜单,所以我尝试实现 customContextMenu()
并发出 customContextMenuRequested() 直接发出信号:
emit QWidget::customContextMenuRequested(w->pressPos);
但失败并出现错误:
error: cannot call member function ‘void QWidget::customContextMenuRequested(const QPoint&)’ without object
如何通过 QGestureEvent 触发上下文菜单?
I'm working on a finger friendly browser exercise with FlickCharm example and QtWebKit, here's all the source code I have so far.
I want to trigger context by long press (tap-and-hold), currently I can get the QGestureEvent and position in eventFilter()
by
QGestureEvent *gestureEvent = dynamic_cast<QGestureEvent*>(event);
if (gestureEvent) {
if (const QGesture *g = gestureEvent->gesture(Qt::TapAndHoldGesture)) {
if (g->state() == Qt::GestureStarted) {
qDebug() << "tap-n-hold event started";
QWebView *webView = dynamic_cast<QWebView*>(object);
FlickData *w = d->flickData.value(webView);
qDebug() << w->pressPos; // get position
}
}
}
My first attempt is simulating a right click event by using sendEvent()
:
QMouseEvent pressEvent(QEvent::MouseButtonPress, w->pressPos,
Qt::RightButton, Qt::RightButton, Qt::NoModifier);
QApplication::sendEvent(webView->page(), &pressEvent);
It seems like the event did send, but doesn't trigger the context menu, so I tried to implement customContextMenu()
and emit customContextMenuRequested()
signal directly:
emit QWidget::customContextMenuRequested(w->pressPos);
but failed with error:
error: cannot call member function ‘void QWidget::customContextMenuRequested(const QPoint&)’ without object
How can I trigger the context menu by QGestureEvent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试发送
QContextMenuEvent
,而不是QMouseEvent
直接代码>。Instead of a
QMouseEvent
, try sending aQContextMenuEvent
directly.