antoyo
antoyo

Reputation: 11933

QWebView Open in new Window

I want to open links from a QWebView with target="_blank" in a new window of a new process.

I tried using QWebView::createWindow(QWebPage::WebWindowType), but I didn't find a way to get the url to send to the new process. Actually, I tried a hack to get the url using a hidden QWebView (returned by the createWindow method) with the urlChanged event, but sometimes this event is triggered twice, so it is not reliable.

Is there any way to get the URL in the createWindow method?

If not, what can I do to open this kind of links in a new window of a new process?

Upvotes: 2

Views: 1762

Answers (2)

antoyo
antoyo

Reputation: 11933

I found a way to do what I want.

I had to overload the mousePressEvent to get the mouse position so that I can get the url from it.

Here is the code:

QWebView* WebView::createWindow(QWebPage::WebWindowType) {
    QWebHitTestResult result{page()->mainFrame()->hitTestContent(lastClickPosition)};
    openNewWindow(result.linkUrl());
    return nullptr;
}

void WebView::mousePressEvent(QMouseEvent* mouseEvent) {
    lastClickPosition = mouseEvent->pos();
    QWebView::mousePressEvent(mouseEvent);
}

I let this question opened a few days to see if anyone can find a better solution.

Upvotes: 1

K. Barresi
K. Barresi

Reputation: 1315

If you're trying for a multi-process webview architecture, I would suggest looking at QtWebEngine. QtWebEngine will be replacing QtWebKit and is based on Chromium. Because of this, it has a multi-process architecture by default. QtWebEngine will no longer be updated, so I would suggest migrating to QtWebEngine anyway.

QtWebEngine overview

Upvotes: 0

Related Questions