PDA

View Full Version : Qwebview read data from gogle maps



Tadas
3rd June 2011, 00:40
Hi,

I want to load google maps to qwebview from Html file and read longitude and latitude values. There is some problems with QWebView and google maps API v3. You can't use mouse to navigate in map by default. Here I have found solution http://developer.qt.nokia.com/forums/viewthread/1643/P15/#36423 but if I use this solution, then I can't read information from HTML file. Here is code:
map.h

#ifndef MAP_H
#define MAP_H
#include <QWebView>
#include <QNetworkAccessManager>
#include <QWebFrame>
#include <qobject.h>
class Map : public QWebView
{
Q_OBJECT
public:

explicit Map(QWidget *parent = 0);
public slots:
void loadMap();

void doSomething( const QString &param );
int doSums( int a, int b );

private:
QNetworkAccessManager *manager;
int pendingRequests;
private slots:
void attachObject();

};

#endif // MAP_H


map.cpp

#include "map.h"
#include "ui_mapwindow.h"
#include <QDebug>


class myWebPage : public QWebPage
{
virtual QString userAgentForUrl(const QUrl& url) const {
return "Chrome/2.0";
}
};

Map::Map(QWidget *parent) :
QWebView(parent) , pendingRequests(0)
{
manager = new QNetworkAccessManager(this);
attachObject();
connect( this->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachObject()) );
//this->setPage( new myWebPage() );

}
void Map::loadMap()
{
this->setUrl(QUrl("map.html"));
this->show();
}
void Map::attachObject()
{

this->page()->mainFrame()->addToJavaScriptWindowObject( QString("MyApi"), this );
}

void Map::doSomething( const QString &param )
{
qDebug() << "doSomething called with parameter " << param;
}

int Map::doSums( int a, int b )
{
return a + b;
}


The problem is. If I will uncomment //this->setPage( new myWebPage() ); line, then google maps will function well. But i will not get data from HTML file. If I will comment this line //this->setPage( new myWebPage() ); Then I can't navigate in maps using mouse. but I will get something like this
doSomething called with parameter "Hello from JS!!!!" (because I have added this line MyApi.doSomething( 'Hello from JS!!!!' ); in HTML file)

I believe I have done something wrong with this part
class myWebPage : public QWebPage
{
virtual QString userAgentForUrl(const QUrl& url) const {
return "Chrome/2.0";
}

But don't know what. I can post all code with HTML code if that would help more. Thank you.

yuvalal
3rd June 2011, 05:27
Hi,
Just a quick guess:
It seems as if you connect the signal from the original created page, and after that replace it with the new page (myWebPage) and lose the connection.

Try replacing lines 18&19 to get:


...
this->setPage( new myWebPage() );
connect( this->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachObject()) );
...

Tadas
3rd June 2011, 08:09
Hi,

Yes you are right. Thank you for the answer.