简体   繁体   English

QWebEngineView在外部浏览器中打开

[英]QWebEngineView Open In External Browser

I'm in the process of moving my code from QtWebKit to QtWebEngine. 我正在将我的代码从QtWebKit移动到QtWebEngine。 In general, the transition has been fairly smooth, however, I'm stuck on one particular issue. 总的来说,过渡相当顺利,但是,我坚持一个特定的问题。 I use a QWebEngineView to display a Google Maps page. 我使用QWebEngineView显示Google Maps页面。 Some of the markers placed have have infowindows that pop up "Click Here for More Information" which opens the link in an external browser. 放置的一些标记有信息窗口,弹出“单击此处获取更多信息”,在外部浏览器中打开链接。

Using QtWebKit, this was fairly easy through the setLinkDelegation policy. 使用QtWebKit,通过setLinkDelegation策略可以轻松实现。 However, it seems a little more complex here. 但是,这里似乎有点复杂。 I've tried to follow the example but somehow I need to redefine QWebEnginePage within QWebEngineView. 我试图按照这个例子,但不知怎的,我需要在QWebEngineView中重新定义QWebEnginePage。 Below is what I've come up with so far. 以下是我到目前为止所提出的内容。 Any idea how I can actually connect this all up? 知道如何实际连接这一切吗?

Thanks 谢谢

#ifndef MYQWEBENGINEVIEW_H
#define MYQWEBENGINEVIEW_H

#include <QWebEngineView>
#include <QDesktopServices>

class MyQWebEnginePage : public QWebEnginePage
{
    Q_OBJECT

public:
    MyQWebEnginePage(QObject* parent = 0) : QWebEnginePage(parent){}

    bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
    {
         qDebug() << "acceptNavigationRequest("<<url << "," << type << "," << isMainFrame<<")";

        if (type == QWebEnginePage::NavigationTypeLinkClicked)
        {
            QDesktopServices::openUrl(url);
            return false;
        }
        return true;
    }
};


class MyQWebEngineView : public QWebEngineView
{
    Q_OBJECT
public:
    MyQWebEngineView(QWidget* parent = 0);
    MyQWebEnginePage* page() const;

};

#endif // MYQWEBENGINEVIEW_H

You don't need the second part. 你不需要第二部分。 Try this: 尝试这个:

QWebEngineView *view = new QWebEngineView();
MyQWebEnginePage *page = new MyQWebEnginePage();
view->setPage(page);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM