PDA

View Full Version : QWebView linkclicked and form submit



maddog_fr
2nd August 2009, 20:10
Hello everybody. :D
I am creating a new class to subclass QWebView

To manage link navigation i used :
view->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks ); and connected
and then
connect( view->page(), SIGNAL(linkClicked(const QUrl&)),this, SLOT(OnLinkClicked(const QUrl&)));

it work for all kind of links but when a form is submitted I dont receive the signal and the submitted page is loaded in the QWebPage

I read that form submit call the function acceptNavigationRequest
and when I check in the sourcecode of Qwebpage and i get

#if QT_VERSION >= 0x040400
bool QWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
#else
bool QWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
#endif
{
if (type == NavigationTypeLinkClicked) {
switch (d->linkPolicy) {
case DontDelegateLinks:
return true;
case DelegateExternalLinks:
if (WebCore::SecurityOrigin::shouldTreatURLSchemeAsLo cal(request.url().scheme()))
return true;
emit linkClicked(request.url());
return false;
case DelegateAllLinks:
emit linkClicked(request.url());
return false;
}
}
return true;
}

any navigation request should emit linkclicked when DelegateAllLinks is set


Where is the problem :confused: ? I am using Qt 4.5

thx for your help.

[EDIT:] I am beginning at Qt

numbat
3rd August 2009, 10:08
The problem is here:


if (type == NavigationTypeLinkClicked)

According to the docs type will be QWebPage::NavigationTypeFormSubmitted for a form, and as such the code you pasted will just return true. You need to subclass QWebPage and override acceptNavigationRequest.

P.S Please use
tags.

maddog_fr
3rd August 2009, 12:09
geez i am stupid i didnt see the return true !!

another problem come then. I am not using directly QWebPage but a subclassed Qwebview
is it possible to use a subclassed QWebPage in my subclassed QWebview ?

yogeshgokul
3rd August 2009, 12:16
is it possible to use a subclassed QWebPage in my subclassed QWebview ?
Yes you can use,

QWebView::setPage ( YOURPAGE * page )

maddog_fr
3rd August 2009, 12:34
I just tried quickly but seems it's not so easy to subclass QWebPage

maddog_fr
3rd August 2009, 19:24
I just tried quickly but seems it's not so easy to subclass QWebPage

Sublclassing QWebPage is not the problem in fact but it seems that the function acceptNavigationRequest is protected

numbat
3rd August 2009, 19:57
I'm not a C++ guru, but I think protected member functions can be overridden in a subclass. Have you tried?

maddog_fr
4th August 2009, 11:59
Ok I managed to make it work (I guess my brain was tired yesterday).


//myqwebpage.h
#ifndef MYQWEBPAGE_H
#define MYQWEBPAGE_H

#include <QObject>
#include <QWebPage>

class MyQWebPage : public QWebPage
{
Q_OBJECT

public:
MyQWebPage(QObject *parent=0);

protected:
#if QT_VERSION >= 0x040400
bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
#else
bool acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, NavigationType type);
#endif

signals:
#if QT_VERSION >= 0x040400
void formSubmitted(const QNetworkRequest &request);
#else
void formSubmitted(const QWebNetworkRequest &request);
#endif


};

#endif // MYQWEBPAGE_H



// myqwebpage.cpp
#include <QtGui>
#include "myqwebpage.h"

MyQWebPage::MyQWebPage(QObject *parent): QWebPage(parent){
}


#if QT_VERSION >= 0x040400
bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
#else
bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
#endif
{
if (type == NavigationTypeFormSubmitted) {
emit formSubmitted(request);
return false;
}
return QWebPage::acceptNavigationRequest(frame,request,ty pe);
}


then finaly I connect the signal to a slot


#if QT_VERSION >= 0x040400
connect(webpage,SIGNAL(formSubmitted(const QNetworkRequest&)),this,SLOT(OnFormSubmitted(const QNetworkRequest&)));
#else
connect(webpage,SIGNAL(formSubmitted(const QWebNetworkRequest&)),this,SLOT(OnFormSubmitted(const QWebNetworkRequest&)));
#endif


thx for your help.

maddog_fr
4th August 2009, 17:38
Seems I did it for nothing.


I thought that the QNetworkRequest was holding the data to be posted but i dont find it.
any idea how to find the data ?

My goal is to open the result of the pos in a new window (window = a widget with a qwebview)

maddog_fr
8th August 2009, 20:03
Ok I managed to do what I want :D



#if QT_VERSION >= 0x040400
bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
#else
bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
#endif
{
if (type == NavigationTypeFormSubmitted) {
HTMLWindow *newWindow = WindowManager::instance()->addHTMLWindow();
MyQWebView *webview;
webview = newWindow->view;
webview->load(request);
return false;
}
return QWebPage::acceptNavigationRequest(frame,request,ty pe);
}