Reputation: 377
OK bending my brain trying to make sense of QWebEngine.
I understand the concept of implementing virtual functions but I'm unsure how to get the url that the user has clicked being a newTab/newWindow link that a page or view has requested.
QWebEngineView * WebEngineTabView::createWindow(QWebEnginePage::WebWindowType type)
{
// signal Main window for a new view( @URL )
emit requestNewTab(page()->requestedUrl());
}
This is for an educational GPL browser app Any help greatly appreciated
Upvotes: 3
Views: 4049
Reputation: 71
If you check the Qt source code, you will see that after the function QWebEnginePage::createWindow is called to create the QWebEnginePage pointer, this pointer will have some data written into it. See https://code.qt.io/cgit/qt/qtwebengine.git/tree/src/webenginewidgets/api/qwebenginepage.cpp?h=5.9#n404
For me the following example works:
class MyWebEnginePage : public QWebEnginePage
{
...
QWebEnginePage *createWindow(WebWindowType type) Q_DECL_OVERRIDE
{
QWebEnginePage *page = new QWebEnginePage();
connect(page, &QWebEnginePage::urlChanged, this, [this] (const QUrl &url) {
emit newPageUrlChanged(url);
}
return page;
}
signals:
void newPageUrlChanged(const QUrl &url);
};
class MyClass : public QObject
{
...
MyClass()
{
connect(m_pPage, &MyWebEnginePage::newPageUrlChanged, this, &MyClass::onNewPageUrlChanged);
}
private slots:
void onNewPageUrlChanged(const QUrl &url)
{
qDebug() << url; // new url will be printed here
}
private:
MyWebEnginePage *m_pPage;
}
Hope this helps
Upvotes: 2
Reputation: 222
Did you saw how this done in the demobrowser example?
QWebEnginePage *WebPage::createWindow(QWebEnginePage::WebWindowType type)
{
if (type == QWebEnginePage::WebBrowserTab) {
return mainWindow()->tabWidget()->newTab()->page();
} else if (type == QWebEnginePage::WebBrowserWindow) {
BrowserApplication::instance()->newMainWindow();
BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
return mainWindow->currentTab()->page();
} else {
PopupWindow *popup = new PopupWindow(profile());
popup->setAttribute(Qt::WA_DeleteOnClose);
popup->show();
return popup->page();
}
}
If you still want to delegate this work, notifying the mainwindow/app/whatever, you probably can intercept clicks and store links, but I'm not sure about calls order, plus you have to pay attention for cases when requested window is just a "new tab" (an empty tab without url):
bool WebPage::acceptNavigationRequest(const QUrl & url, NavigationType type, bool isMainFrame)
{
switch( type )
{
case QWebEnginePage::NavigationTypeLinkClicked:
{
mLastClickedLink = url; //-- clear it in WebPage::createWindow
return true;
}
default:
return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame );
}
}
Upvotes: 2