JavaScript에서 웹 측에 QObject
이 노출되었지만 채널을 다시로드하거나 페이지 재로드 후에 또는 해당 채널을 잃어버린 경우 응용 프로그램과 WebEngine
사이에 웹 채널을 만들었습니다. 다른 페이지로 연결되는 링크를 클릭하면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