如何通过 C++ 触发 JavaScript 事件在QWebView中

发布于 2024-09-18 23:53:51 字数 358 浏览 3 评论 0原文

请注意,我是 Qt 开发的完全初学者。

我有一个 QWebView,其中添加了一个 QObject 到 JavaScript 窗口对象。 如何在该对象上触发 JS 事件?

view->page()->mainFrame()->addToJavaScriptWindowObject(objName,obj);

我希望能够使用 addEventListener 监听事件。

window.objName.addEventListener('customEventName',function(e){ ... });

感谢您的任何帮助。

please note that I'm a complete beginner to Qt development.

I have a QWebView with a QObject added to the JavaScript window object.
How do I fire a JS event on that object?

view->page()->mainFrame()->addToJavaScriptWindowObject(objName,obj);

I want to be able to listen to events using addEventListener.

window.objName.addEventListener('customEventName',function(e){ ... });

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅黛梨妆こ 2024-09-25 23:53:51

你不能那样做。只有 DOM 树中的节点才能触发事件,并且您注入的对象不是节点。我建议你考虑使用信号槽机制。您可以将 JS 中的槽连接到将在 C++ 代码中发出的信号:

window.objectName.signalName.connect(slot);

槽只是一个 JS 函数,您将在代码的 JS 部分中声明它:

function slot(arg1, arg2, ...)
{
}

它需要与中声明的参数一样多的参数信号的签名。

You cannot do that. Only nodes in a DOM tree can fire events and the object you're injecting is not a node. I suggest you consider using the signal-slot mechanism instead. You can connect a slot in JS to a signal that you'll be emitting in your C++ code:

window.objectName.signalName.connect(slot);

slot is just an JS function, that you'll declare in the JS part of your code:

function slot(arg1, arg2, ...)
{
}

It takes as many arguments as are declared in the signal's signature.

如此安好 2024-09-25 23:53:51

在最近的一个项目中,我使用了以下技术:

void VideoWebPage::urlLoaded(bool ok)
{
    static const QString javascript =
        "function installCallbacks()                                    " \
        "{                                                                  " \
        "   var videoTags = document.getElementsByTagName('object');        " \
        "   for (var i = 0; i < videoTags.length; ++i)                      " \
        "   {                                                               " \
        "        if (videoTags[i].type == 'application/x-qt-plugin')        " \
        "        {                                                          " \
        "            if (videoTags[i].playing)                              " \
        "            {                                                      " \
        "                videoTags[i].playing.connect(playingSlot); " \
        "            }                                                      " \
        "        }                                                          " \
        "    }                                                              " \
        "}                                                                  " \
        \
        "function playingSlot(videoId)                              " \
        "{                                                                  " \
        "    var playEvent=document.createEvent('Events');                  " \
        "    playEvent.initEvent('play', true, false);                      " \
        "    document.getElementById(videoId).dispatchEvent(playEvent); " \
        "}                                                                  " \
        "installCallbacks();                                            ";
    mainFrame()->evaluateJavaScript(javascript);
}

此方法查找所有 标签,并将 playing 信号连接到 Javascript 函数 playingSlot()< /代码>。
playingSlot() 函数又创建一个名为 playEvent 对象,并将其作为普通 DOM 事件进行调度。
HTML 文件如下所示:

<html>
<head>
<script language="text/javascript">
    void init()
    {
        document.getElementById('id1').addEventListener('play', onPlay);
    }
    void onPlay(event)
    {
        alert('playing');
    }
</script>
</head>
<body onload='init()'>
    <object id='id1' type='application/x-qt-plugin'>
    </object>
</body>
</html>

这当然是与 Qt 插件 Widgets 一起使用的。我没有使用纯 HTML 对其进行测试(即不使用 Qt 插件的情况)。

In a recent project I used the following technique:

void VideoWebPage::urlLoaded(bool ok)
{
    static const QString javascript =
        "function installCallbacks()                                    " \
        "{                                                                  " \
        "   var videoTags = document.getElementsByTagName('object');        " \
        "   for (var i = 0; i < videoTags.length; ++i)                      " \
        "   {                                                               " \
        "        if (videoTags[i].type == 'application/x-qt-plugin')        " \
        "        {                                                          " \
        "            if (videoTags[i].playing)                              " \
        "            {                                                      " \
        "                videoTags[i].playing.connect(playingSlot); " \
        "            }                                                      " \
        "        }                                                          " \
        "    }                                                              " \
        "}                                                                  " \
        \
        "function playingSlot(videoId)                              " \
        "{                                                                  " \
        "    var playEvent=document.createEvent('Events');                  " \
        "    playEvent.initEvent('play', true, false);                      " \
        "    document.getElementById(videoId).dispatchEvent(playEvent); " \
        "}                                                                  " \
        "installCallbacks();                                            ";
    mainFrame()->evaluateJavaScript(javascript);
}

This method looks up all <object> tags and connects the playing signal to the Javascript function playingSlot().
The playingSlot() function in its turn creates an Event object with the name play and dispatches it as an ordinary DOM event.
The HTML file then looks like:

<html>
<head>
<script language="text/javascript">
    void init()
    {
        document.getElementById('id1').addEventListener('play', onPlay);
    }
    void onPlay(event)
    {
        alert('playing');
    }
</script>
</head>
<body onload='init()'>
    <object id='id1' type='application/x-qt-plugin'>
    </object>
</body>
</html>

This is of course working with Qt plugin Widgets. I did not test it with plain HTML (i.e. where no Qt plugins are used).

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