Qt 4.7:通过手势触发上下文菜单

发布于 2024-10-09 05:19:38 字数 1438 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-16 05:19:38

Instead of a QMouseEvent, try sending a QContextMenuEvent directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文