Search code examples
c++qt5qwebengineviewqwebchannel

QWebChannel fails with condition (JS to C++)


Related to this answer : https://stackoverflow.com/a/62210448/16304747

I have tried the example and everything works fine. But I've found something interesting. If I had a condition, the QWebChannel seems to fail (JS to C++).

Here is an example (based on the previous post source) :

QWebEngineView * browser = new QWebEngineView;
browser->resize(QSize(800,600));
browser->show();
browser->load(QUrl("http://www.wikipedia.org"));

QWebChannel channel;
JsInterface jsInterface;
browser->page()->setWebChannel(&channel, 42);
channel.registerObject(QString("JsInterface"), &jsInterface);

QObject::connect(browser, &QWebEngineView::loadFinished, [&browser](bool ok)
{
    qDebug()<<"Load Finished " << ok;

    // TEST CODE HERE
    QString code = QStringLiteral(
    R"DELIM(

    var links = document.getElementsByTagName('a');
    for ( var i=0; i<links.length; ++i)
    {
        links[i].style.backgroundColor = 'yellow';
    };

    )DELIM");
    browser->page()->runJavaScript(code, 42);

    browser->page()->runJavaScript(qWebChannelJs(), 42);

    QString code2 = QStringLiteral(
    R"DELIM(
    window.webChannel = new QWebChannel(qt.webChannelTransport, function( channel)
    {
        var cpp = channel.objects.JsInterface;
        cpp.log("Hello from JavaScript");
    });

    )DELIM");
    browser->page()->runJavaScript(code2, 42);
}

This one works. I've the output from the interface LOG from JS: Hello from JavaScript.

But if I add a testing condition, it fails :

bool testwebchannel_main = true;

if ( testwebchannel_main )
{
    QWebEngineView * browser = new QWebEngineView;
    browser->resize(QSize(800,600));
    browser->show();
    browser->load(QUrl("http://www.wikipedia.org"));

    QWebChannel channel;
    JsInterface jsInterface;
    browser->page()->setWebChannel(&channel, 42);
    channel.registerObject(QString("JsInterface"), &jsInterface);

    QObject::connect(browser, &QWebEngineView::loadFinished, [&browser](bool ok)
    {
        qDebug()<<"Load Finished " << ok;

        // TEST CODE HERE
        QString code = QStringLiteral(
        R"DELIM(

        var links = document.getElementsByTagName('a');
        for ( var i=0; i<links.length; ++i)
        {
            links[i].style.backgroundColor = 'yellow';
        };

        )DELIM");
        browser->page()->runJavaScript(code, 42);

        browser->page()->runJavaScript(qWebChannelJs(), 42);

        QString code2 = QStringLiteral(
        R"DELIM(
        window.webChannel = new QWebChannel(qt.webChannelTransport, function( channel)
        {
            var cpp = channel.objects.JsInterface;
            cpp.log("Hello from JavaScript");
        });

        )DELIM");
        browser->page()->runJavaScript(code2, 42);
    }
}

Nothing from the JS side.. The JSinterface is not called.

Any idea why it's appening ?


Solution

  • Ok I answer my own question, it was a scope problem. Here is the correct code :

    bool testwebchannel_main = true;
    
    if ( testwebchannel_main )
    {
        QWebEngineView * browser = new QWebEngineView;
        browser->resize(QSize(800,600));
        browser->show();
        browser->load(QUrl("http://www.wikipedia.org"));
    
        QWebChannel * channel = new QWebChannel;
        JsInterface * jsInterface = new JsInterface;
        browser->page()->setWebChannel(channel, 42);
        channel.registerObject(QString("JsInterface"), jsInterface);
    
        QObject::connect(browser, &QWebEngineView::loadFinished, [&browser](bool ok)
        {
            qDebug()<<"Load Finished " << ok;
    
            // TEST CODE HERE
            QString code = QStringLiteral(
            R"DELIM(
    
            var links = document.getElementsByTagName('a');
            for ( var i=0; i<links.length; ++i)
            {
                links[i].style.backgroundColor = 'yellow';
            };
    
            )DELIM");
            browser->page()->runJavaScript(code, 42);
    
            browser->page()->runJavaScript(qWebChannelJs(), 42);
    
            QString code2 = QStringLiteral(
            R"DELIM(
            window.webChannel = new QWebChannel(qt.webChannelTransport, function( channel)
            {
                var cpp = channel.objects.JsInterface;
                cpp.log("Hello from JavaScript");
            });
    
            )DELIM");
            browser->page()->runJavaScript(code2, 42);
        }
    }