How to pass value from QML to JavaScript in QWebEngineView

后端 未结 1 986
名媛妹妹
名媛妹妹 2020-12-16 07:12

DataManager is a class and I can access this in QML by below code (Qt version 5.8.0).

DataManager *d = new DataManager;
QQuickView *viewver = ne         


        
1条回答
  •  青春惊慌失措
    2020-12-16 07:54

    QML Code

    import QtQuick 2.0
    import QtWebEngine 1.4
    import QtWebChannel  1.0
    
    Item{
        id:root
        height: 500
        width:  500
    
    // Create WebChannel
    WebChannel{
        id:webChannel
    }
    
    //Now, let’s create an object that we want to publish to the HTML/JavaScript clients:
    QtObject {
        id: myObject
        objectName: "myObject"
    
        // the identifier under which this object
        // will be known on the JavaScript side
        //WebChannel.id: "webChannel"
    
        property var send: function (arg) {
                    sendTextMessage(arg);
                }
    
        // signals, methods and properties are
        // accessible to JavaScript code
        signal someSignal(string message);
    
    
        function someMethod(message) {
            console.log(message);
            someSignal(message);
            return dataManager.getGeoLat();
        }
    
        property string hello: "world";
    }
    
    Rectangle{
        anchors.fill: parent
        color: "black"
    
    WebEngineView{
        id : webEnginView
        anchors.fill: parent
        url : dataManager.htmlURL();
        webChannel: webChannel
    }
    }
    
    Component.onCompleted: {
        webChannel.registerObject("foo", myObject);
        //Expose C++ object 
        webChannel.registerObject("bar", dataManager);
    }
    }
    

    HTML code

    
    
    

    0 讨论(0)
提交回复
热议问题