QT5.5 webengine 打开browser 后调用 web 的 JavaScript

当你使用QT5.5 的WebEngine 打开一个http协议的网页,并想执行网页上的JS方法。

我用的方法是,

1、在xxx.qml里面 添加一个函数:

function animation(url)
{
wbview.runJavaScript("window.animation_run()", function(result) { return true;});
}

wbview,是WebEngineView的ID

window.animation_run() -- 就是网页上的JS方法。function(result) { return true;} 是我随意写的。

2、在QT应用程序里添加:

QMetaObject::invokeMethod(m_pRootObj, "animation", Q_RETURN_ARG(QVariant, retVal), Q_ARG(QVariant, 0));

这句话就是调用 animation 方法的。


如此就可以在QT里面去调用网页上的JavaScript的方法了



xxx.qml如下所示:

import QtQuick 2.3  
import QtQuick.Window 2.2  
import QtQuick.Controls 1.2
import QtWebEngine 1.1  
import "qrc:///qml/Resource/func.js" as Funcs


ApplicationWindow
{
signal messageEvent
 
    id: mainwin
    visible: true;  
    width: animated.width;  
    height: animated.height + 24;  
    flags: Qt.Window | Qt.FramelessWindowHint
    color: Qt.rgba(0.5, 0.5, 0.5, 0.5)
Keys.forwardTo: keyhandle


    AnimatedImage {  
        id: animated;  
        source: "qrc:/animator/Resource/loading.gif";  
     Keys.forwardTo: keyhandle


        onCurrentFrameChanged: {  
            //info.text = "%1/%2".arg(animated.currentFrame).arg(animated.frameCount);  
        }
    }
 
    Row{  
        spacing: 4;  
        anchors.horizontalCenter: parent.horizontalCenter;  
        anchors.bottom: parent.bottom;  
        anchors.bottomMargin: 4;  
        Text {  
            id: info;  
            width: 60;  
            height: 24;  
            color: "red";  
            verticalAlignment: Text.AlignVCenter;  
            horizontalAlignment: Text.AlignRight;
text: "Loading ..."  
        }  
    }

    WebEngineView
    {
   id: wbview
visible: false
        width: Screen.width
        height: Screen.height
        anchors.fill: parent
        opacity: 1.0
   Keys.forwardTo: keyhandle


        //url: initialUrl


onNewViewRequested: { //supported in v1.1
messageEvent();
   Funcs.LogOutput();
}
     }  


function loadUrl(url)
{
   //goto fullwin.qml
wbview.url = url;


   return url;
}


function animation(url)
{
wbview.runJavaScript("window.animation_run()", function(result) { return true;});
}


function showUrl(url)
{
   //goto fullwin.qml
animated.paused = true
animated.visible = false


//minimum window first
mainwin.x = 0
mainwin.y = 0


mainwin.width = 0
mainwin.height = 0


//maxmium window
mainwin.flags = Qt.Window | Qt.FramelessWindowHint | Qt.WindowFullScreen


mainwin.width = 2000
mainwin.height = 1280


wbview.visible = true


return url;
}


MouseArea {  
anchors.fill: parent;  
acceptedButtons: Qt.LeftButton | Qt.RightButton;  
onDoubleClicked: {  
if(mouse.button == Qt.RightButton){  
   messageEvent();
Funcs.LogOutput();
Qt.quit();  
}
}  
}


    Rectangle {
   id: keyhandle
   width: 1
height: 1
focus: true


Keys.onPressed: {
messageEvent();
Funcs.LogOutput();
Qt.quit();  
}


Keys.onEscapePressed: {  
messageEvent();
Funcs.LogOutput();
Qt.quit();  
}
}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值