[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.

    RD



  • Hi,

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

    qt-everywhere-opensource-src-4.7.2/src/3rdparty/webkit/WebKit/qt/declarative/



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



  • Ok I found another workaround using QGraphicsProxyWidget :

    MyWebView.cpp
    @
    #include "rswebview.h"
    #include <QWebFrame>
    #include <QDebug>
    MyWebView::MyWebView( QGraphicsProxyWidget *parent) :
    QGraphicsProxyWidget(parent)
    {
    webview = new QWebView();
    webview->setAttribute(Qt::WA_NoSystemBackground);
    webview->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
    webview->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
    webview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    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)));
    setWidget(webview);
    }

    void MyWebView::load(const QUrl& url)
    {
    webview->load(url);
    }

    //Implement methods that you need
    @

    myWebView.h
    @
    #include <QGraphicsProxyWidget>
    #include <QWebView>

    class MyWebView : public QGraphicsProxyWidget
    {
    Q_OBJECT

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

    //... add properties that you need

    public:
    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;

    signals:
    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();

    private:
    QWebView *webview;
    };
    @

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

    in QML :
    @
    import mylib 1.0

    MyWebView
    {
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.