Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate linkClicked(QUrl) signal in QWebEngineView?

Tags:

c++

qt

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.

like image 870
Lieefu Way Avatar asked Oct 27 '25 03:10

Lieefu Way


2 Answers

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)));
like image 116
Lieefu Way Avatar answered Oct 29 '25 17:10

Lieefu Way


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.

like image 20
IAmInPLS Avatar answered Oct 29 '25 18:10

IAmInPLS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!