Important: Please read the Qt Code of Conduct -

[Solved] Catching click on links in WebView

  • Hi,

    I want to catch click on links in WebView in QML. Right now there is no linkClicked signal in WebView. What is the ideal solution in this case ? Should I develop my own plugin which binds QWebView and generates linkClicked signals ?

  • -Try QWebPage instead. You can get access to the QWebPage instance in your view using QWebView::page().-

    Sorry, you are talking about QML. Don't know in that case.

  • Hi,

    Done writing plugin !!! Just took code from Qt's implementation of QDeclarativeWebView and added linkClicked signal.

  • Hi,

    Would you mind giving us some more detail please ?
    I looked into QML plugin documentation but I don't know what to use. The high-level API don't seems to have the right base class. Did you extend a complete application showing a fullscreen webview ?

    Thanks for your help.


  • Hi,

    I modified an implementation of QDeclarativeWebView. It's available in following directory (in Qt 4.7.2)


  • Thanks.
    However Qt source is under GPL, I can't use that for a commercial application.

  • Ok I found another workaround using QGraphicsProxyWidget :

    #include "rswebview.h"
    #include <QWebFrame>
    #include <QDebug>
    MyWebView::MyWebView( QGraphicsProxyWidget *parent) :
    webview = new QWebView();
    webview->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
    webview->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
    QObject::connect(webview->page()->mainFrame(), SIGNAL(contentsSizeChanged(QSize)), this, SIGNAL(contentSizeChanged())); // important for using the webview in a flickable
    QObject::connect(webview,SIGNAL(linkClicked(QUrl)),this, SIGNAL(linkClicked(QUrl)));
    QObject::connect(webview,SIGNAL(loadFinished(bool)),this, SLOT(onLoadFinished(bool)));
    // ... connect signals that you need ...
    QObject::connect(webview,SIGNAL(loadProgress(int)),this, SIGNAL(loadProgress(int)));
    QObject::connect(webview,SIGNAL(urlChanged(QUrl)),this, SIGNAL(urlChanged(QUrl)));

    void MyWebView::load(const QUrl& url)

    //Implement methods that you need

    #include <QGraphicsProxyWidget>
    #include <QWebView>

    class MyWebView : public QGraphicsProxyWidget

    Q_PROPERTY(QUrl url READ url WRITE setUrl)
    Q_PROPERTY(QString html WRITE setHtml)

    //... add properties that you need

    explicit MyWebView( QGraphicsProxyWidget *parent = 0);

    void load(const QUrl& url);
    void setHtml(const QString& html, const QUrl& baseUrl = QUrl());
    QString html() const;

    //... methods that you need ...
    QString title() const;
    void setUrl(const QUrl &url);
    QUrl url() const;

    void loadStarted();
    void loadFailed();
    void loadProgress(int progress);
    //... add signals that you need ...
    void loadFinished();
    void linkClicked(const QUrl&);
    void urlChanged(const QUrl&);

    public slots:
    void stop();
    void back();
    //... add slots that you need ...
    void reload();

    QWebView *webview;

    in your main.cpp
    qmlRegisterType<MyWebView>("mylib", 1, 0, "MyWebView");

    in QML :
    import mylib 1.0

    height: contentSize.height
    // ... use as before

    The only problem I get is to assign the right height to the webview.
    Because even if you place it in a Flickable and set anchors to fill parent, it wont expend.
    The solution is to bind the height to webview->page()->mainFrame()->contentsSize().height

Log in to reply