好吧,弯曲我的大脑,试图理解QWebEngine。
我理解实现虚拟函数的概念,但我不知道如何获得用户单击的url作为页面或视图请求的newTab/newWindow链接。
QWebEngineView * WebEngineTabView::createWindow(QWebEnginePage::WebWindowType type)
{
// signal Main window for a new view( @URL )
emit requestNewTab(page()->requestedUrl());
}这是一个教育性的GPL浏览器应用程序,任何帮助都非常感谢。
发布于 2016-01-26 14:16:36
你看到用户示例是怎么做到的了吗?
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();
}
}如果您仍然希望委托这项工作,通知主窗口/应用程序/任何其他,您可能可以拦截点击并存储链接,但我不确定呼叫顺序,另外,当请求的窗口只是一个“新选项卡”(没有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 );
}
}发布于 2019-01-28 19:36:36
如果您检查Qt源代码,您将看到在调用QWebEnginePage::createWindow函数来创建QWebEnginePage指针之后,该指针将有一些数据写入其中。请参阅https://code.qt.io/cgit/qt/qtwebengine.git/tree/src/webenginewidgets/api/qwebenginepage.cpp?h=5.9#n404
对我来说,下面的示例很有效:
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;
}希望这能有所帮助
https://stackoverflow.com/questions/32852900
复制相似问题