Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Webengine - open in external browser

Webengine - open in external browser

Scheduled Pinned Locked Moved Solved QtWebEngine
5 Posts 2 Posters 5.3k Views
  • 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 Offline
    G Offline
    goalie39
    wrote on last edited by goalie39
    #1

    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
    0
    • J Offline
      J Offline
      janalleman
      wrote on last edited by
      #2

      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
      0
      • G Offline
        G Offline
        goalie39
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          goalie39
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goalie39
            wrote on last edited by
            #5

            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
            0

            • Login

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