QWebChannel 因条件而失败(JS 到 C++)
QWebChannel fails with condition (JS to C++)
与此答案相关:
我试过这个例子,一切正常。但我发现了一些有趣的事情。如果我有条件,QWebChannel 似乎会失败(JS 到 C++)。
这是一个例子(基于之前的post来源):
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);
}
这个有效。我有接口 LOG from JS: Hello from JavaScript
.
的输出
但是如果我添加测试条件,它会失败:
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);
}
}
JS 端什么都没有.. 没有调用 JSinterface.
知道它为什么出现吗?
好的,我回答我自己的问题,这是一个范围问题。这是正确的代码:
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);
}
}
与此答案相关:
我试过这个例子,一切正常。但我发现了一些有趣的事情。如果我有条件,QWebChannel 似乎会失败(JS 到 C++)。
这是一个例子(基于之前的post来源):
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);
}
这个有效。我有接口 LOG from JS: Hello from JavaScript
.
但是如果我添加测试条件,它会失败:
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);
}
}
JS 端什么都没有.. 没有调用 JSinterface.
知道它为什么出现吗?
好的,我回答我自己的问题,这是一个范围问题。这是正确的代码:
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);
}
}