"qt is undefined " when I declare QWebChannel



  • Hello everyone.
    I tried to use QWebEngineView with Qt 5.7.0 on Windows, I found it dose not work to invoke c++ function from javascript

        QWebChannel *channel{new QWebChannel(this)};
        channel->registerObject(QStringLiteral("VisJsApi"), jsApi);
        channel->registerObject(QStringLiteral("VPntr"), pjso);
    
        webView->page()->setWebChannel(channel);
    
    document.addEventListener("DOMContentLoaded", function() {
        new QWebChannel(qt.webChannelTransport, function(channel) {
            window.VisJsApi = channel.objects.VisJsApi;
            window.VPntr = channel.objects.VPntr;
        });
    });
    

    when I use textQtWebchannel to test.

    function textQtWebchannel() {
    if (typeof QWebChannel != 'undefined') {
            alert("QWebChannel on");
        } else {
            alert("QWebChannel off");
        }
    
        if (typeof navigator != 'undefined') {
            alert("navigator on");
    
            if (typeof navigator.qtWebChannelTransport != 'undefined') {
                alert("navigator.qtWebChannelTransport on");
            } else {
                alert("navigator.qtWebChannelTransport off");
            }
        } else {
            alert("navigator off");
        }
    
        if (typeof qt != 'undefined') {
            alert("qt on");
    
            if (typeof qt.webChannelTransport != 'undefined') {
                alert("qt.webChannelTransport on");
    
                new QWebChannel(qt.webChannelTransport, function(channel) {
                    window.VisJsApi = channel.objects.VisJsApi;
                    window.VPntr = channel.objects.VPntr;
                });
    
            } else {
                alert("qt.webChannelTransport off");
            }
        } else {
            alert("qt off");
        }
    };
    

    what i finially find it print out:

    QWebChannel on
    navigator on
    navigator.qtWebChannelTransport off
    qt off
    


  • @Mason1024 I have a bit different problem in QML undef Linux. It works but too fragile.

    ApplicationWindow {
        visible: true
        title: "test: " + web.title
        menuBar: MenuBar {
            Menu {
                title: qsTr("&File")
                MenuItem {
                    text: qsTr("&Web")
                    onTriggered: {
                        web.url = "http://meta.ua"
                    }
                }
                MenuItem {
                    text: qsTr("&Load")
                    onTriggered: {
                        web.loadHtml('dummy')
                    }
                }
                MenuItem {
                    text: qsTr("E&xit")
                    onTriggered: Qt.quit();
                }
            }
        }
    
        WebEngineView {
            id: web
            webChannel.registeredObjects: []
            anchors.fill: parent
            userScripts: [testScript]
            WebEngineScript {
                id: testScript
                sourceCode: "document.title = 'qt = ' + typeof qt"
                injectionPoint: WebEngineScript.Deferred
                worldId:  WebEngineScript.MainWorld
            }
        }
    }
    

    More precisely. First time my page is loaded qt.webChannelTransport is visible. But when I switch url or do loadHtml I cannot access qt.webChannelTransport after page loads.
    Besides that when I try to connect with remote web debugging (using env QTWEBENGINE_REMOTE_DEBUGGING=23654) before page loaded I get the same problem on my first page.
    Also qt is inaccessible without setting webChannel.registeredObjects or webChannel itself during component load.
    Looks like it is simply unstable yet that results in strange internal state.

    I'd trye to set url for webView after setting webChannel.

    P.S. Mine qtwebengine is built without system ffmpeg.



  • @Mason1024 i want to know, too



  • Let me know if you figured a way to make it work, currently stuck on this too.

    I must say the documentation is lacking on QWebEngine.
    The example is too complex for nothing, we don't want a chat server, just a simple WebPage communicating to C++ in both way (printing text and alert message example would be sufficient to understand).
    I don't understand why they use a QWebSocket since it's not necessary?
    Any more example around that could help? I did everything the docs says and I can't get it to work js-->c++, the other way is easy with runJavascript


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.