Lieefu Way
Lieefu Way

Reputation: 63

How to emulate linkClicked(QUrl) signal in QWebEngineView?

I am porting my project from Qt WebKit to Qt WebEngine in Qt5.6. I want to emit linkClicked(QUrl) signal when a href is clicked on the QWebView, but QWebEngineView has no signal linkClicked(QUrl).

How to emulate linkClickedSignal(QUrl)?

Porting from Qt WebKit to Qt WebEngine.

Upvotes: 4

Views: 2622

Answers (2)

Lieefu Way
Lieefu Way

Reputation: 63

Thank you @Alexis P. I have got it.

class MyWebPage : public QWebEnginePage
{
    Q_OBJECT
public:
    MyWebPage(QObject* parent = 0) : QWebEnginePage(parent){}

    bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
    {
        if (type == QWebEnginePage::NavigationTypeLinkClicked)
        {
            //QDesktopServices::openUrl(url);
            emit linkClicked(url);
            return false;
        }
        return true;
    }

signals:
    void linkClicked(const QUrl&);

};

In my window class:

webView = new QWebEngineView(ui->verticalLayoutWidget);
webView->setPage(new MyWebPage());
ui->verticalLayout->addWidget(webView);
connect(webView- >page(),SIGNAL(linkClicked(QUrl)),this,SLOT(linkClicked(QUrl)));

Upvotes: 2

IAmInPLS
IAmInPLS

Reputation: 4125

I am not sure it will be useful for you, but in my app using QWebEngineView, I have clickable links which must open the corresponding website in a browser.

The way I am doing it is like that :

class MyQWebEnginePage : public QWebEnginePage
{
    Q_OBJECT

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

    bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
    {
        if (type == QWebEnginePage::NavigationTypeLinkClicked)
        {
            QDesktopServices::openUrl(url);
            return false;
        }
        return true;
    }
};

As you can see, I just reimplemented the virtual method acceptNavigationRequest of QWebEnginePage in order to retrieve the url from the link clicked : url. I don't know it is what you want to achieve, but I hope that helps.

Upvotes: 1

Related Questions