View Full Version : Subclassing a QWebView
UKCitizen1423
25th May 2014, 19:16
Hi All,
I need to solve the issue of navigating links within an iframe with a QWebView. After setting setLinkDelegationPolicy(QWebPage::DelegateAllLinks ), I check the link and then with the pages I'm not interested in then I open the link but if this link is in an iframe then the QWebView will open the link in the main frame and not the iframe. I know I need to subclass the QWebView and the closest example I get is here:
http://www.qtcentre.org/threads/22997-QWebView-linkclicked-and-form-submit
but I can't find a full code example ( .h and .cpp ) and my attempts produce linker errors. I guess a guru could set this up in less than 5 minutes but I've spent hours and hours on this :(
As mentioned, I've seen a few solutions and at least one where somebody solves with JavaScript. I would appreciate help and I'm sure others would appreciate a small example. I don't know if this should have been posted in the advanced section?
Thanks!!
UKCitizen1423
28th May 2014, 22:41
Am I posting to the wrong section? No answers or views :(
wysota
29th May 2014, 10:48
What exactly is the problem? What errors are you getting?
UKCitizen1423
29th May 2014, 16:38
Hi Wysota,
Thanks for the reply. I have solved the linking errors but I need to know how to navigate pages within a iframe or frameset rather than the top main frame. As far as I understand then I am supposed to over ride navigation request in the subclassed QWebView something like:
bool MyWebView::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
{
//Q_UNUSED(frame)
if (type == NavigationTypeLinkClicked) {
switch (d->linkPolicy) {
case DontDelegateLinks:
return true;
case DelegateExternalLinks:
if (WebCore::SchemeRegistry::shouldTreatURLSchemeAsLo cal(request.url().scheme()))
return true;
emit linkClicked(request.url());
return false;
case DelegateAllLinks:
emit linkClicked(request.url());
frame->load(QUrl("http://www.google.com/")); // Need to analyse first
qDebug() << "LINK HAS BEEN CLICKED";
return true;
}
}
return true;
}
It is obviously wrong as I will get all sorts of errors such as:
"error: use of undeclared identifier 'NavigationTypeLinkClicked'
if (type == NavigationTypeLinkClicked) {
^"
and
"error: 'd' is a private member of 'QWebView'
switch (d->linkPolicy) {"
and
"error: member access into incomplete type 'QWebViewPrivate'
switch (d->linkPolicy) {
^
^"
So I need an overview or quick example of how to cater for iframe navigation in a subclassed QWebView.
FYI
I have subclassed a QWebPag to specify a specific useragent and that works OK.
I'm sure newbies would appreciate a full example of navigating within iframes as other solutions just state override the acceptNavigationRequest method to access the specific frame.
Regards & Thanks
wysota
29th May 2014, 21:09
I'm not sure what the problem is... The web frame is the method's parameter, you can access it there...
UKCitizen1423
29th May 2014, 23:11
Wysota,
First of all - thanks for your time, appreciated! I've made some progress.
I have a subclassed QWebPage
I have this:
*****************
bool MyCustomWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
{
if (frame->parentFrame() != 0){
if (type == NavigationTypeLinkClicked) {
//frame->setUrl(request.url()); // THIS FAILS
frame->setUrl(QUrl("http://www.google.co.uk")); // this works now and changes the URL in the frame
// I Need to analyse the querystring parameters first
}
}
qDebug() << "LINK HAS BEEN CLICKED"; // Just Checking where I am
return true;
}
******************
Note this works with no setLinkDelegationPolicy(QWebPage::DelegateAllLinks ); NOT being set
Notice that:
frame->setUrl(request.url()); // THIS FAILS
With
error: member access into incomplete type 'const QNetworkRequest'
frame->setUrl(request.url()); // Need to analyse first
^
on the setUrl() method
So
a) I need to analyse the url
b) I need to shut of that error
Thanks in advance.
Regards
wysota
29th May 2014, 23:56
#include <QNetworkRequest>
BTW. Please use the [code] tags.
UKCitizen1423
30th May 2014, 10:32
Wysota,
Bingo! That does the job nicely. Thank you.
Point taken on the code tags.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.