Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to open the link clicked in a new tab?

how to open the link clicked in a new tab?

Scheduled Pinned Locked Moved General and Desktop
webengineview
6 Posts 2 Posters 4.8k Views 2 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.
  • E Offline
    E Offline
    ejos
    wrote on last edited by ejos
    #1

    I want to open the link clicked page in a new tab. Also the the page where the link is clicked is to remain at the same page. How can I do this.

    I tried with QTabWidget to open the linkclicked on new tab.
    But I am not able to restrict the page navigation in the tab where the link was clicked.

    WebEngineView.cpp
    #include "WebEngineView.h"
    
    WebEngineView::WebEngineView(QWidget *parent)
        : QWebEngineView(parent)
    {
        setGeometry(0,35,dw.width(),dw.height()-35);
        connect(this,SIGNAL(urlChanged(QUrl)),this,SLOT(loadInNewTab(QUrl)));
    }
    
    WebEngineView::~WebEngineView()
    {
    }
    
    void WebEngineView::loadInNewTab(QUrl url)
    {
        emit newTab(url);
    }
    
    TabWindow.cpp
    #include "TabWindow.h"
    
    TabWindow::TabWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        weView = new WebEngineView(this);
        weView->setGeometry(0,50,dw.width(),dw.height());
        weView->load(QUrl("http://www.google.co.in"));
    
        QWidget *centralWidget = new QWidget(this);
        tabWidget = new QTabWidget(centralWidget);
        tabWidget->setFixedSize(dw.width(),dw.height());
        loadedUrl = QUrl("http://www.google.co.in");
        tabWidget->addTab(weView,loadedUrl.host());
        setCentralWidget(centralWidget);
        connect(weView,SIGNAL(newTab(QUrl)),this,SLOT(loadNewTab(QUrl)));
    }
    
    TabWindow::~TabWindow()
    {
    }
    
    void TabWindow::loadNewTab(QUrl url)
    {
        weView->blockSignals(true);
        if(loadedUrl.host().compare(url.host(),Qt::CaseInsensitive) != 0)
        {
            weView = new WebEngineView(this);
            weView->load(url);
            tabWidget->addTab(weView,url.host());
            qDebug()<<"current index : "<<tabWidget->currentIndex();
            loadedUrl = url;
        }
    }
    
    TabWindow.h
    #include <QMainWindow>
    #include <QDesktopWidget>
    #include <WebEngineView.h>
    * list item
    class TabWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        TabWindow(QWidget *parent = 0);
        ~TabWindow();
        QDesktopWidget dw;
        WebEngineView *weView;
        WebEngineView *weViewNew;
        QTabWidget *tabWidget;
    signals:
        void newTab(QUrl);
    public slots:
    
        void loadNewTab(QUrl);
    private :
        QUrl loadedUrl;
    };
    
    WebEngineView.h
    #ifndef WEBENGINEVIEW_H
    #define WEBENGINEVIEW_H
    #include <QWebEngineView>
    #include <QDesktopWidget>
    #include <WebEnginePage.h>
    
    class WebEngineView : public QWebEngineView
    {
        Q_OBJECT
    public:
        WebEngineView(QWidget *parent = 0);
        ~WebEngineView();
         QDesktopWidget dw;
        WebEnginePage *webPage;
        WebEngineView *weView;
    signals:
        void newTab(QUrl);
    public slots:
        void loadInNewTab(QUrl);
    
    };
    
    #endif // WEBENGINEVIEW_H```
    p3c0P 1 Reply Last reply
    0
    • E ejos

      I want to open the link clicked page in a new tab. Also the the page where the link is clicked is to remain at the same page. How can I do this.

      I tried with QTabWidget to open the linkclicked on new tab.
      But I am not able to restrict the page navigation in the tab where the link was clicked.

      WebEngineView.cpp
      #include "WebEngineView.h"
      
      WebEngineView::WebEngineView(QWidget *parent)
          : QWebEngineView(parent)
      {
          setGeometry(0,35,dw.width(),dw.height()-35);
          connect(this,SIGNAL(urlChanged(QUrl)),this,SLOT(loadInNewTab(QUrl)));
      }
      
      WebEngineView::~WebEngineView()
      {
      }
      
      void WebEngineView::loadInNewTab(QUrl url)
      {
          emit newTab(url);
      }
      
      TabWindow.cpp
      #include "TabWindow.h"
      
      TabWindow::TabWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          weView = new WebEngineView(this);
          weView->setGeometry(0,50,dw.width(),dw.height());
          weView->load(QUrl("http://www.google.co.in"));
      
          QWidget *centralWidget = new QWidget(this);
          tabWidget = new QTabWidget(centralWidget);
          tabWidget->setFixedSize(dw.width(),dw.height());
          loadedUrl = QUrl("http://www.google.co.in");
          tabWidget->addTab(weView,loadedUrl.host());
          setCentralWidget(centralWidget);
          connect(weView,SIGNAL(newTab(QUrl)),this,SLOT(loadNewTab(QUrl)));
      }
      
      TabWindow::~TabWindow()
      {
      }
      
      void TabWindow::loadNewTab(QUrl url)
      {
          weView->blockSignals(true);
          if(loadedUrl.host().compare(url.host(),Qt::CaseInsensitive) != 0)
          {
              weView = new WebEngineView(this);
              weView->load(url);
              tabWidget->addTab(weView,url.host());
              qDebug()<<"current index : "<<tabWidget->currentIndex();
              loadedUrl = url;
          }
      }
      
      TabWindow.h
      #include <QMainWindow>
      #include <QDesktopWidget>
      #include <WebEngineView.h>
      * list item
      class TabWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          TabWindow(QWidget *parent = 0);
          ~TabWindow();
          QDesktopWidget dw;
          WebEngineView *weView;
          WebEngineView *weViewNew;
          QTabWidget *tabWidget;
      signals:
          void newTab(QUrl);
      public slots:
      
          void loadNewTab(QUrl);
      private :
          QUrl loadedUrl;
      };
      
      WebEngineView.h
      #ifndef WEBENGINEVIEW_H
      #define WEBENGINEVIEW_H
      #include <QWebEngineView>
      #include <QDesktopWidget>
      #include <WebEnginePage.h>
      
      class WebEngineView : public QWebEngineView
      {
          Q_OBJECT
      public:
          WebEngineView(QWidget *parent = 0);
          ~WebEngineView();
           QDesktopWidget dw;
          WebEnginePage *webPage;
          WebEngineView *weView;
      signals:
          void newTab(QUrl);
      public slots:
          void loadInNewTab(QUrl);
      
      };
      
      #endif // WEBENGINEVIEW_H```
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @ejos You should probably consider formatting the code first to make it more readable. Use code tags ``` (3 backticks).

      157

      E 1 Reply Last reply
      0
      • p3c0P p3c0

        @ejos You should probably consider formatting the code first to make it more readable. Use code tags ``` (3 backticks).

        E Offline
        E Offline
        ejos
        wrote on last edited by
        #3

        @p3c0 I was not sure how to format. Thanks for the info

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ejos
          wrote on last edited by
          #4

          QWebEnginePage has createWindow() which is protected function.
          How to re-implement this to open a new tab

          p3c0P 1 Reply Last reply
          0
          • E ejos

            QWebEnginePage has createWindow() which is protected function.
            How to re-implement this to open a new tab

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by p3c0
            #5

            @ejos Have a glance through browser example. It exactly has what you need.

            157

            1 Reply Last reply
            0
            • E Offline
              E Offline
              ejos
              wrote on last edited by ejos
              #6

              Below is the one I tried. Now the link opens in a new window.
              But how do I make it open in a new tab.
              I checked the link which was provided in the previous code. But as beginner, it's too much for me to understand.

              #include "WebEnginePage.h"
              
              WebEnginePage::WebEnginePage(QObject *parent)
                      : QWebEnginePage(parent)
              {
                  connect(this,SIGNAL(urlChanged(QUrl)),this,SLOT(loadInTab(QUrl)));
              }
              
              WebEnginePage::~WebEnginePage()
              {
              
              }
              
              void WebEnginePage::loadInTab(QUrl url)
              {
                  qDebug()<<"Page : "<<url;
                  emit pageInNewTab(url);
              
              }
              
              
              #include "WebEngineView.h"
              
              WebEngineView::WebEngineView(QWidget *parent)
                  : QWebEngineView(parent)
              {
                  webPage = new WebEnginePage(this);
                  setPage(webPage);
                  setGeometry(0,35,dw.width(),dw.height()-35);
              
                  // connect(page(),SIGNAL(urlChanged(QUrl)),this,SLOT(loadInNewTab(QUrl)));
                  connect(page(),SIGNAL(pageInNewTab(QUrl)),this,SLOT(loadInNewTab(QUrl)));
                  hide();
                  connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onPageLoadFinished(bool)));
              }
              
              void WebEngineView::onPageLoadFinished(bool value)
              {
                  qDebug()<<"WebEngineView::onPageLoadFinished";
                  show();
                  emit newTab(this);
              }
              WebEngineView::~WebEngineView()
              {
              
              }
              
              WebEngineView *WebEngineView::createWindow(QWebEnginePage::WebWindowType type)
              {
                  Q_UNUSED(type);
                  qDebug()<<"WebEngineView::createWindow";
                  WebEngineView *webView = new WebEngineView();
                  qDebug()<<"Index : "<<++index;
                  WebEnginePage *newWeb = new WebEnginePage(webView);
                  webView->setAttribute(Qt::WA_DeleteOnClose, true);
                  webView->setPage(newWeb);
                  //webView->show();
              
                  return webView;
              }
              
              
              
              void WebEngineView::loadInNewTab(QUrl url)
              {
                  qDebug()<<"WebEngineView "<<index<<" : "<<url;
                 // createWindow(QWebEnginePage::WebBrowserTab);
              }
              int WebEngineView::getIndex() const
              {
                  return index;
              }
              
              void WebEngineView::setIndex(int value)
              {
                  index = value;
              }
              
              
              
              #include "TabWindow.h"
              
              TabWindow::TabWindow(QWidget *parent)
                  : QMainWindow(parent)
              {
                  weView = new WebEngineView(this);
                  weView->setGeometry(0,50,dw.width(),dw.height());
                  loadedUrl = QUrl("http://www.google.co.in");
                  //weView->load(QUrl(loadedUrl));
                  weView->webPage->load(loadedUrl);
                  QWidget *centralWidget = new QWidget(this);
                  tabWidget = new QTabWidget(centralWidget);
                  tabWidget->setTabsClosable(true);
                  //tabWidget->setElideMode(Qt::ElideRight);
              
                  tabWidget->setFixedSize(dw.width(),dw.height());
                  loadedUrl = QUrl("http://www.google.co.in");
                  tabWidget->addTab(weView,loadedUrl.host());
                  setCentralWidget(centralWidget);
                  currentIndex = 0;
                  weView->setIndex(currentIndex);
              
                  //weViewNew = new WebEngineView(this);
                  connect(weView,SIGNAL(newTab(WebEngineView&)),this,SLOT(loadNewTab(WebEngineView7)));
                  connect(tabWidget,SIGNAL(tabCloseRequested(int)),this,SLOT(closeTab(int)));
              }
              
              TabWindow::~TabWindow()
              {
              
              }
              
              void TabWindow::closeTab(int index)
              {
                  tabWidget->removeTab(index);
              }
              
              
              void TabWindow::loadNewTab(WebEngineView *webView)
              {
                  //webView->show();
                  tabWidget->addTab(webView,webView->webPage->url().host());
                  //tabWidget->setCurrentIndex(currentIndex);
                  qDebug()<<"TabWindow::loadNewTab : ";
              
              
              }
              
              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