使用googlemap和QWebEngineView,我想在html文件中添加myscript.js。它在调试中运行得很好,但在发布中则不然。在 Release模式下,找不到 myscript.js 并出现错误 js: Uncaught (in Promise) ReferenceError: myscriptNameSpace is not Define
我正在使用 Qt 5.12.2、MSVC 2017
我的 qrc 文件
<RCC>
<qresource prefix="/">
<file>html/googlemap.html</file>
<file>js/myscript.js</file>
</qresource>
</RCC>
我的.h文件
class GoogleMapWebEngine : public QWidget
{
Q_OBJECT
public:
explicit GoogleMapWebEngine(QWidget *parent = nullptr);
virtual ~GoogleMapWebEngine() override;
protected:
virtual void wheelEvent(QWheelEvent * event) override;
private:
Ui::GoogleMapWebEngine *ui;
QWebEngineView* m_webview;
QWebChannel* m_webChannel;
};
我的.cpp 文件
GoogleMapWebEngine::GoogleMapWebEngine(QWidget *parent) :
QWidget(parent),
ui(new Ui::GoogleMapWebEngine)
{
qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "1234");
ui->setupUi(this);
m_webview = new QWebEngineView();
m_webChannel = new QWebChannel();
m_webChannel->registerObject("goolemainWindow", this);
m_webview->page()->setWebChannel(m_webChannel);
QUrl url = QUrl("qrc:/html/googlemap.html");
m_webview->setUrl(url);
ui->verticalLayout->addWidget(m_webview);
}
我的 googlemap.html 文件
<html>
<head>
...
<script src="qrc:/js/myscript.js"></script>
<script type="text/javascript">
function initMap()
{
var
mapCenter = [32.794488, -96.780372],
mapOptions = {
zoom: 18,
center: new google.maps.LatLng(mapCenter[0], mapCenter[1]),
mapTypeId: google.maps.MapTypeId.SATELLITE
},
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
myscriptNameSpace.test();
}
</script>
</head>
<body ondragstart="return false">
<div id="map-canvas" />
<script async defer src="https://maps.googleapis.com/maps/api/js?key=myKey&callback=initMap"></script>
</body>
</html>
myscript.js
var myscriptNameSpace = {}
myscriptNameSpace.test = function(){
alert('test');
}
预期:必须显示警报消息
当前错误:js:未捕获(在 promise 中)ReferenceError:myscriptNameSpace 未定义
我的完整源代码可以在这里找到:github.com/hunglxtp/googlemapTest
最佳答案
终于从qt documentation找到了问题所在。
我的解决方案是在 .pro 文件中添加:QTQUICK_COMPILER_SKIPPED_RESOURCES += testgooglemapwebengine.qrc
。
关于javascript - Qt QWebEngineView 在发布中但在调试中找不到 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900135/