Opening an URL in external browser when newViewRequested signal emited - qt

I Use WebEngineView QML Type to show a web page that have some link that need to open in a new tab. Links are somethings like
Go to google in new tab
I want to open the URL of newViewRequested signal in an external browser but the WebEngineNewViewRequest has no 'url' property that I can use with Qt.openUrlExternally(request.url).
WebEngineNewViewRequest has a private member QUrl m_requestedUrl that not accesible as property in qml.
How can I handle the issue,get the URL and open it in an external browser.
Thanks.

In Qt5, you can use navigationRequested signal to achieve this:
onNavigationRequested: function(request) {
if (request.navigationType === WebEngineNavigationRequest.LinkClickedNavigation) {
Qt.openUrlExternally(request.url)
}
request.action = WebEngineNavigationRequest.IgnoreRequest
}
The line of assigning IgnoreRequest to the action property is to make sure the URL is not opened in the WebEngineView.

Related

JXBrowser control over dialog website not responding

Hey im having issues with a website in the jxbrowser. it seems like it is running into a timeout or whatever and then in the jxbrowser there is a dialog showing up "website not responding" and i can click on "reload" or "leave".
Can I in any way access this dialog and overwrite it? For instance everytime i would get this dont ask but go to the homepage instead?
I'm having trouble finding this if it is even possible.
I found a solution. JXBrowser has a RenderAdapter where a function exists onRenderUnresponsive wich can be overridden. Look at this: https://jxbrowser.support.teamdev.com/support/solutions/articles/9000091687-detecting-unresponsive-web-page
In my case I simply want to reload the website:
Browser browser = new Browser();
browser.addRenderListener(new RenderAdapter() {
#Override
public void onRenderUnresponsive(RenderEvent event) {
browser.reloadIgnoringCache(false);
}
});

QtWebKit: How can I detect a URL changed via the HTML5 history API

We're currently using QtWebKit to build an application, part of which displays the product catalogue page of our website. On that webpage we're using the jQuery.history plugin to keep track of sorting and filtering options without users needing to reloading the page.
When jQuery.history's pushState() function is called the URL provided is pushed to the history properly, but QWebView's urlChanged() signal (which we're listening for to update back/forward buttons and the address bar) isn't fired even though the current URL has changed. I can only assume this is because no link was clicked, nor was there a page reload so Qt doesn't think it needs to do anything URL-related.
Is there any way to detect a URL change made via the HTML5 history API in QtWebKit, or am I missing something obvious?
There is QWebPage::saveFrameStateRequested signal:
This signal is emitted shortly before the history of navigated pages in frame is changed, for example when navigating back in the history.
You can use it to track history changes:
void MainWindow::saveFrameStateRequested(QWebFrame *frame, QWebHistoryItem *item) {
// this slot is executed before the history is changed,
// so we need to wait a bit:
QTimer::singleShot(100, this, SLOT(listHistoryItems()));
}
void MainWindow::listHistoryItems() {
for (QWebHistoryItem historyItem: view->page()->history()->items()) {
qDebug() << "item" << historyItem.url() << historyItem.title();
}
}
void MainWindow::finishLoading(bool) {
// (re)connect the signal to track history change,
// maybe there is a better place to connect this signal
// where disconnect won't be needed
disconnect(view->page(), SIGNAL(saveFrameStateRequested(QWebFrame*,QWebHistoryItem*)),
this, SLOT(saveFrameStateRequested(QWebFrame*,QWebHistoryItem*)));
connect(view->page(), SIGNAL(saveFrameStateRequested(QWebFrame*,QWebHistoryItem*)),
this, SLOT(saveFrameStateRequested(QWebFrame*,QWebHistoryItem*)));
}
The modified Fancy Browser example screenshot.

I want to handle the link clicked in a QWebView

so this is my problem : I would like to handle a clicked link in a QWebView. There is the signal
void QWebView::linkClicked ( const QUrl & url )
that is emitted when the user clicks on a link, so I could use that for my purposes, but it depends of the value of linkDelegationPolicy property that is set by default to not delegate links
but I can't change this because it's a function of QWebPage and I have a QWebView and QWebView doesn't inherit from QWebPage...so I'm really confused
Any help please! Thanks!
The QWebView has an underlying QWepPage. You can get a pointer to it using the method QWebView::page().

QWebView doesn't open links in new window and not start external application for handling pdf

I am using a QWebView in this way:
QWebView *window = new QWebView();
window->setUrl(QString("my url"));
window->show();
And it works. I can see the html page I want.
The problem is this. By default if I "right click" on a link the action "Open in new window" is shown but if I click on it, nothing happens. If I "left click" on the same link it works.
So the problem is that no new windows are open by QWebView. Does anyone know why?
I have another problem. Some links are pdf file so I expect that QWebView ask me to download it or to run an application to open it. But nothing happens instead. I think the problem is related to the fact that no new windows are allowed to be opened by QWebView and not on the pdf.
Obviously I tested the page with a web browser and everything work well, so the problem is in some settings of QWebView.
Does anyone know how to make QWebView open new windows when required?
Notes:
all links are local resources.
The html links use this syntax (and they works):
Some link
The link to pdfs use this syntax (nothing happens when I click):
Some pdf
Try to handle cicks by yourself. Here is an example that can guide you. I have not compiled it though .
QWebView *window = new QWebView();
window->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);//Handle link clicks by yourself
window->page()->setContextMenuPolicy(Qt::NoContextMenu); //No context menu is allowed if you don't need it
connect( window, SIGNAL( linkClicked( QUrl ) ),
this, SLOT( linkClickedSlot( QUrl ) ) );
window->setUrl(QString("my url"));
window->show();
//This slot handles all clicks
void MyWindow::linkClickedSlot( QUrl url )
{
if (url.ishtml()//isHtml does not exist actually you need to write something like it by yourself
window->load (url);
else//non html (pdf) pages will be opened with default application
QDesktopServices::openUrl( url );
}
Note that if the HTML you are displaying may containing relative/internal links to other parts of itself, then you should use QWebPage::DelegateExternalLinks instead of QWebPage::DelegateAllLinks.
The above answer is informative but might be overwhelmed for this question.
Connecting signals to QWebPage::action(OpenLinkInNewWindow) or overriding QWebPage::triggerAction should solve this problem.

Is it possible to open a URL in an external browser from within HTML component?

I'm loading a webpage inside a HTML component in AIR. By default, when something is clicked the next page is loaded inside the HTML component itself. I want the links from that page to open in an external web browser.
Is this possible at all?
You have to set the property navigateInSystemBrowser. Check this code:
private changeHtmlLoaderBehaviour():void
{
html.htmlLoader.navigateInSystemBrowser = true;
}
<mx:HTML id="html"..location="http://www.amazon.com"../>

Resources