Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Webengine - open in external browser

    QtWebEngine
    2
    5
    4241
    Loading More Posts
    • 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.
    • G
      goalie39 last edited by goalie39

      Hi All -

      I'm currently migrating my application from QtWebKit to QtWebEngine. The transition has mostly been smooth, but there are a couple things I'm trying to figure out how to duplicate. One thing I used to do was set links to open in the user's external browser. Something like "Click here for more info" and it would direct them to the external site in chrome/explorer/safari/etc. I did this via:

      ui->map->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks)
      connect( ui->map->page(), SIGNAL(linkClicked(const QUrl &)),this, SLOT(OpenExternalBrowser(const QUrl &)));
      

      I saw in the documentation it mentions overloading the QWebEnginePage::acceptNavigationRequest() function, however, I'm not totally sure how to achieve the behavior there.

      Thanks for the help

      1 Reply Last reply Reply Quote 0
      • J
        janalleman last edited by

        Correct, you add the acceptNavigationRequest() to your QWebEnginePage derived class, then supply the guts for the function to open the user's default browser.
        E.g.:

        class MyWebPage : public QWebEnginePage
        {
        	Q_OBJECT
        
        public:
        	MyWebPage(QObject* parent = 0) : QWebEnginePage(parent){}
        
        	bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
        	{
        		 qDebug() << "acceptNavigationRequest("<<url << "," << type << "," << isMainFrame<<")";
        
        		if (type == QWebEnginePage::NavigationTypeLinkClicked)
        		{
        			QDesktopServices::openUrl(url);
        			return false;
        		}
        		return true;
        	}
        }
        
        
        1 Reply Last reply Reply Quote 0
        • G
          goalie39 last edited by

          Thanks. I think I'm getting there, but still having some troubles. In the end, this all ends up in a QWebEngineView widget. So I've done the following:

          myqwebenginepage.h

          #include <QWebEnginePage>
          
          class MyQWebEnginePage : public QWebEnginePage
          {
              Q_OBJECT
          public:
              MyQWebEnginePage(QObject* parent = 0);
              bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame);
          };
          

          myqwebenginepage.cpp

          #include "myqwebenginepage.h"
          #include <QDesktopServices>
          
          MyQWebEnginePage::MyQWebEnginePage(QObject* parent)
          {
          
          }
          
          bool MyQWebEnginePage::acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
          {
               qDebug() << "acceptNavigationRequest("<<url << "," << type << "," << isMainFrame<<")";
          
              if (type == QWebEnginePage::NavigationTypeLinkClicked)
              {
                  QDesktopServices::openUrl(url);
                  return false;
              }
              return true;
          }
          
          

          myqwebengineview.h

          #include <QWebEngineView>
          #include <myqwebenginepage.h>
          
          class MyQWebEngineView : public QWebEngineView
          {
              Q_OBJECT
          public:
              explicit MyQWebEngineView(QWidget* parent = 0);
              MyQWebEnginePage* page() const;
          };
          
          

          myqwebengineview.cpp

          #include "myqwebengineview.h"
          
          MyQWebEngineView::MyQWebEngineView(QWidget* parent)
          {
          
          }
          

          I then go into my UI file and flip out qwebengineview with myqwebengineview widget. The result is a series of unresolved externals, but I'm not sure why.

          MyApp_main.obj:-1: error: LNK2001: unresolved external symbol "public: class MyQWebEnginePage * __cdecl MyQWebEngineView::page(void)const " (?page@MyQWebEngineView@@QEBAPEAVMyQWebEnginePage@@XZ)
          

          Thanks again for your help

          1 Reply Last reply Reply Quote 0
          • G
            goalie39 last edited by

            After some more light reading, I've wound up with this:

            class MyQWebEnginePage : public QWebEnginePage
            {
                Q_OBJECT
            
            public:
                MyQWebEnginePage(QObject* parent = 0) : QWebEnginePage(parent){qDebug() << "here";}
            
                bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
                {
                     qDebug() << "acceptNavigationRequest("<<url << "," << type << "," << isMainFrame<<")";
            
                    if (type == QWebEnginePage::NavigationTypeLinkClicked)
                    {
                        QDesktopServices::openUrl(url);
                        return false;
                    }
                    return true;
                }
            };
            
            class MyQWebEngineView : public QWebEngineView
            {
                Q_OBJECT
            
            public:
                MyQWebEngineView(QWidget* parent = 0) : QWebEngineView(parent){}
                MyQWebEnginePage* page() const;
            };
            

            Still getting a similar error in my test application though. Getting warmer?

            mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: class MyQWebEnginePage * __cdecl MyQWebEngineView::page(void)const " (?page@MyQWebEngineView@@QEBAPEAVMyQWebEnginePage@@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
            
            1 Reply Last reply Reply Quote 0
            • G
              goalie39 last edited by

              Ok, I got it worked out. I just needed to use the following:

              main.cpp

              ...some code...
              QPointer<MyQWebEnginePage> thisPage = new MyQWebEnginePage;
              ui->WebEngineView->setPage(thisPage);
              ...some more code...
              

              myqwebenginepage.h

              #ifndef MYQWEBENGINEPAGE_H
              #define MYQWEBENGINEPAGE_H
              
              #include <QWebEnginePage>
              #include <QDesktopServices>
              
              class MyQWebEnginePage : public QWebEnginePage
              {
                  Q_OBJECT
              
              public:
                  MyQWebEnginePage(QObject* parent = 0) : QWebEnginePage(parent){}
              
                  bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool isMainFrame)
                  {
                      if (type == QWebEnginePage::NavigationTypeLinkClicked)
                      {
                          QDesktopServices::openUrl(url);
                          return false;
                      }
                      return true;
                  }
              };
              
              #endif // MYQWEBENGINEPAGE_H
              

              Thanks for the feedback.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post