PDA

View Full Version : QWebView linkClicked



migel
26th September 2011, 11:50
Hi

When set to QWebPage::DelegateAllLinks a clickLink signal is taking over the click. But how to actually continue loading the link. So let say when I click on link it opens the url normally but in the background do some job through linkClicked ignal/slot yet ???

Thanks


connect(this, SIGNAL(linkClicked(const QUrl &)), SLOT(linkIsClicked(const QUrl&)),Qt::DirectConnection);


this->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks );

Added after 1 23 minutes:

answer


void CLASS:linkClicked(const QUrl&) {
load(url);
}

buvintech
13th January 2014, 21:12
NOTE: The prior posted answer works great in simple circumstances. But not in all...

webview->load( url ); will change the top level web view contents. This is no good if you use pages with iframes.

If you have nested Iframes, and you click inside such a nesting, that simple load invocation will effectively move your nested frame to the top level and drop the parent frame out of the web view altogether.

You need to load the url into the frame where the click occurred instead. In theory, you could do this here, as there a means to iterate through the frames in the webview and set their urls. The problem is knowing which frame contained the link. Unfortunately, the linkClicked signal only carries the destination url, but not the source of the click!

There may in fact be a cleaner way, but I solved this by integrating javascript inside the web view with this linkClicked function. Hint: JS event listeners can capture link clicks, js postMessage() allows iframes to communicate with each other (even works CROSS DOMAIN), and js post message event objects have a source member which is a reference to the window/frame where the message originated.

BWT, my solution works on ALL web pages (not just mine own) - so you can use the webview just like a normal browser with iframes displaying any site (or the way the webview works naturally - when you don't intercept these clicks).