Details
-
Bug
-
Resolution: Invalid
-
Not Evaluated
-
None
-
5.15.2
-
None
-
Windows / VS2019
Description
Hey,
I'm doing some tests with QWebChannel interaction to include it in my application.
I've try the following code and it works well :
#include <QApplication> #include <QWebEngineView> #include <QWebChannel> #include <QFile> class JsInterface: public QObject { Q_OBJECT public: /// Log, for debugging Q_INVOKABLE void log(const QString& str) const { qDebug() << "LOG from JS: " << str; } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); 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); 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); QFile apiFile(":/qtwebchannel/qwebchannel.js"); if(!apiFile.open(QIODevice::ReadOnly)) qDebug()<< "Couldn't load Qt's QWebChannel API!"; QString apiScript = QString::fromLatin1(apiFile.readAll()); apiFile.close(); browser->page()->runJavaScript(apiScript); 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); }); return a.exec(); }
I've the output from the interface LOG from JS: Hello from JavaScript.
But I've found something interesting. If I insert a condition, the QWebChannel seems to fail (JS to C++).
This second example failed :
#include <QApplication> #include <QWebEngineView> #include <QWebChannel> #include <QFile> class JsInterface: public QObject { Q_OBJECT public: /// Log, for debugging Q_INVOKABLE void log(const QString& str) const { qDebug() << "LOG from JS: " << str; } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); 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); 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); QFile apiFile(":/qtwebchannel/qwebchannel.js"); if(!apiFile.open(QIODevice::ReadOnly)) qDebug()<< "Couldn't load Qt's QWebChannel API!"; QString apiScript = QString::fromLatin1(apiFile.readAll()); apiFile.close(); browser->page()->runJavaScript(apiScript); 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); }); } return a.exec(); }
Any idea why it's appening ?
Thanks in advance