私のアプリケーションとWebEngine
の間にウェブチャネルを作成しましたが、JavaScriptでウェブ側にQObject
が公開されましたが、別のページへのリンクをクリックするとQWebEngineとQWebChannel:ページリロード後に転送オブジェクト `qt.webChannelTransport`が消えました
私はページリロード時にチャンネルを作り直す必要があると思っていますが、それはできませんでした。私はページの読み込み、進捗状況と終了スロットでそれをやろうとしましたが、たったのはjs: Uncaught ReferenceError: qt is not defined
です。
<!-- language: lang-cpp -->
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webEngineWidget->load(QUrl("qrc:/index.html"));
page = ui->webEngineWidget->page();
channel = new QWebChannel;
channel->registerObject("external", &exposedObject);
page->setWebChannel(channel);
connect(page, &QWebEnginePage::loadStarted, this, &MainWindow::onPageLoadStarted);
connect(page, &QWebEnginePage::loadProgress, this, &MainWindow::onPageLoadProgress);
connect(page, &QWebEnginePage::loadFinished, this, &MainWindow::onPageLoadFinished);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onPageLoadStarted()
{
qDebug() << "Loading started";
}
void MainWindow::onPageLoadProgress(int progress)
{
qDebug() << "Loading in progress: " << progress;
}
void MainWindow::onPageLoadFinished()
{
qDebug() << "Loading finished";
}
チャンネルがqwebchannel.js
を使用して、ページ側で作成されます。
<h1>Page</h1>
<a href="other.html">Other Page</a>
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script>
var webChannel = new QWebChannel(qt.webChannelTransport, function(channel){
window.external = channel.objects.external;
});
</script>
例の完全なコードはここにある:https://github.com/DanmerZ/QWebChannels-example
ビデオ:https://monosnap.com/file/ZTOgj1QH06VRVF3ogmXln07eOVXXCW
P.S.このエラーはQt5.7でのみ発生し、Qt5.6.1を確認しましたが、チャンネルはうまく動作します。 https://bugreports.qt.io/browse/QTBUG-52209?jql=text%20~%20%22QWebChannel%20reload%22