最近在上位机软件加入一个qtwebkit模块,用于加载google maps(V3)。写好google maps的html文件之后,在chrome和IE中显示均正常,但是一旦用qtwebview加载后只能显示基本的地图,鼠标拖动和滚轮放大等交互操作均无法响应,由于原来的google maps html文件调试正常,所以考虑是qtwebview中默认的浏览器引擎造成的原因。
查阅了qtwebview类的用法,发现其中有一个虚函数QString QWebPage::userAgentForUrl ( const QUrl &url ) const,这个一个设置用户代理的函数,重载该函数可以实现更改qtwebview的默认浏览器代理,查阅相关资料后,做如下改动(代码来源:http://stackoverflow.com/questions/6184240/map-doesnt-respond-on-mouse-clicks-with-google-maps-api-v3-and-qwebview):
Add this class in the source file where you are "loading" the HTML file (containing Javascript API) in the Qt widget.
class myWebPage : public QWebPage { virtual QString userAgentForUrl(const QUrl& url) const { return "Chrome/1.0"; } };
Then in your own class in the same source file, add the setPage function call as shown below and see the magic happen!
MainScreen::MainScreen(QWidget *parent):QWidget(parent) { ... ***map->setPage (new myWebPage());*** map->load (QUrl("./index.html") ) ; };
即将浏览器代理设置为Chrome版本,继续调试google maps在qtwebview中一切正常。
文章转自:http://blog.youkuaiyun.com/jakiechen68/article/details/7710961