【问题标题】:QWebEngineView createWindowQWebEngineView createWindow
【发布时间】:2015-09-29 20:09:41
【问题描述】:

好吧,让我绞尽脑汁想弄明白 QWebEngine。

我了解实现虚拟功能的概念,但我不确定如何将用户单击的 url 获取为页面或视图请求的 newTab/newWindow 链接。

QWebEngineView * WebEngineTabView::createWindow(QWebEnginePage::WebWindowType type)
{
// signal Main window for a new view( @URL )
emit requestNewTab(page()->requestedUrl());
}

这是一个教育 GPL 浏览器应用程序 非常感谢任何帮助

【问题讨论】:

    标签: qt qtwebengine


    【解决方案1】:

    您在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();
        }
    }
    

    如果你还想委派这个工作,通知主窗口/app/whatever,你可能可以拦截点击和存储链接,但我不确定调用顺序,另外你必须注意请求窗口的情况只是一个“新标签”(没有 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 );
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果你查看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;
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多