Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to open the link clicked in a new tab?



  • 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```

  • Moderators

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



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



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


  • Moderators

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



  • 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 : ";
    
    
    }
    

Log in to reply