环境qt5.8,vs2015(ui.webview必须要先show出来,不然加载不成功)

1.项目属性,c/c++,常规,附加包含目录,新增:

$(QTDIR)\include\QtWebChannel

$(QTDIR)\include\QtWebEngineWidgets

2.项目属性,链接器,输入,附加依赖项,新增:

Qt5WebEngine.lib
Qt5WebEngineWidgets.lib
Qt5WebChannel.lib

3.头文件增加:

#include <QWebEngineView>
#include <QtWebChannel>

 

与js交互:

头文件:

QWebChannel *m_webChannel;

cpp:

m_webChannel = new QWebChannel(this);
m_webChannel->registerObject("MainWindow", this);
ui.webview->page()->setWebChannel(m_webChannel);

 

QString reportpath= "file:///" + QApplication::applicationDirPath() + "/report.html";
ui.webview->load(QUrl(reportpath));
connect(ui.webview, &QWebEngineView::loadFinished, this, &MainWindow::slot_webLoadFinished);

如何调用:

ui.webview->page()->runJavaScript(QString("showWarningChart('%1')").arg(json_str));

 

js文件:

 new QWebChannel(qt.webChannelTransport, function(channel) {
        window.MainWindow = channel.objects.MainWindow;
    })

 

function showWarningChart(warningChart){  

}

posted on 2018-03-12 14:55  大陈1108  阅读(545)  评论(0编辑  收藏  举报