May 9, 2011

situ117 situ117
Lab Rat
42 posts

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

6 replies

May 10, 2011

Andre Andre
Robot Herder
6295 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 10, 2011

situ117 situ117
Lab Rat
42 posts

Hi,

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

September 13, 2011

remy_david remy_david
Lab Rat
24 posts

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

September 14, 2011

situ117 situ117
Lab Rat
42 posts

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/

September 14, 2011

remy_david remy_david
Lab Rat
24 posts

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

September 15, 2011

remy_david remy_david
Lab Rat
24 posts

Ok I found another workaround using QGraphicsProxyWidget :

MyWebView.cpp

  1. #include "rswebview.h"
  2. #include <QWebFrame>
  3. #include <QDebug>
  4. MyWebView::MyWebView( QGraphicsProxyWidget *parent) :
  5.     QGraphicsProxyWidget(parent)
  6. {
  7.     webview = new QWebView();
  8.     webview->setAttribute(Qt::WA_NoSystemBackground);
  9.     webview->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
  10.     webview->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
  11.     webview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
  12.     QObject::connect(webview->page()->mainFrame(), SIGNAL(contentsSizeChanged(QSize)), this, SIGNAL(contentSizeChanged())); // important for using the webview in a flickable    
  13.     QObject::connect(webview,SIGNAL(linkClicked(QUrl)),this, SIGNAL(linkClicked(QUrl)));
  14.     QObject::connect(webview,SIGNAL(loadFinished(bool)),this, SLOT(onLoadFinished(bool)));
  15. // ... connect signals that you need ...
  16.     QObject::connect(webview,SIGNAL(loadProgress(int)),this, SIGNAL(loadProgress(int)));
  17.     QObject::connect(webview,SIGNAL(urlChanged(QUrl)),this, SIGNAL(urlChanged(QUrl)));
  18.     setWidget(webview);
  19. }
  20.  
  21. void MyWebView::load(const QUrl& url)
  22. {
  23.     webview->load(url);
  24. }
  25.  
  26. //Implement methods that you need

myWebView.h

  1. #include <QGraphicsProxyWidget>
  2. #include <QWebView>
  3.  
  4. class MyWebView : public QGraphicsProxyWidget
  5. {
  6.     Q_OBJECT
  7.  
  8.     Q_PROPERTY(QUrl url READ url WRITE setUrl)
  9.     Q_PROPERTY(QString html WRITE setHtml)
  10. //... add properties that you need
  11.  
  12. public:
  13.     explicit MyWebView( QGraphicsProxyWidget *parent = 0);
  14.  
  15.     void load(const QUrl& url);
  16.     void setHtml(const QString& html, const QUrl& baseUrl = QUrl());
  17.     QString html() const;
  18. //... methods that you need ...
  19.     QString title() const;
  20.     void setUrl(const QUrl &url);
  21.     QUrl url() const;
  22.  
  23. signals:
  24.     void loadStarted();
  25.     void loadFailed();
  26.     void loadProgress(int progress);
  27. //... add signals that you need ...
  28.     void loadFinished();
  29.     void linkClicked(const QUrl&);
  30.     void urlChanged(const QUrl&);
  31.  
  32. public slots:
  33.     void stop();
  34.     void back();
  35. //... add slots that you need ...
  36.     void reload();
  37.  
  38. private:
  39.     QWebView *webview;
  40. };

in your main.cpp

  1. qmlRegisterType<MyWebView>("mylib", 1, 0, "MyWebView");

in QML :

  1. import mylib 1.0
  2.  
  3. MyWebView
  4. {
  5. height: contentSize.height
  6. // ... use as before
  7. }

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

 
  ‹‹ How to zoom in multiple objects?      TreeView in QML? ››

You must log in to post a reply. Not a member yet? Register here!