Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Catching click on links in WebView
Forum Updated to NodeBB v4.3 + New Features

[Solved] Catching click on links in WebView

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    situ117
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        situ117
        wrote on last edited by
        #3

        Hi,

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          remy_david
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            situ117
            wrote on last edited by
            #5

            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/

            1 Reply Last reply
            0
            • R Offline
              R Offline
              remy_david
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                remy_david
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved