Search code examples
c++qtqt5qwebviewqaction

How to use QWebEnginePage::OpenLinkInNewTab [Qt5.8]


When I click on link to any question in my feed on Quora using this code, the link doesn't open but it doesn't print "Hello". Could you please tell me where am I wrong? I'm pretty sure that link on quora emits the OpenLinkInNewTab signal. Please help, thanks.

class WebView : public QObject {
    void newTabRequested() {
        std::cout<<"Hello"<<std::endl;
    }

public:
    char* home_page;
    QAction* newTabAction=new QAction();
    QWebEngineView* view=new QWebEngineView();

    WebView(char* page=(char*)"https://google.com") {
        this->home_page=page;
        this->exitFullScreen->setShortcut(Qt::Key_Escape);

        createWebView();

        this->view->settings()
            ->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows,true);

        this->newTabAction=this->view->pageAction(QWebEnginePage::OpenLinkInNewTab);

        connect(this->newTabAction,&QAction::toggled,this,&WebView::newTabRequested);
    }

    void createWebView() {
        this->view->load(QUrl(this->home_page));
    }
};

Solution

  • I think the problem is that newTabRequested is not a slot. Try

    class WebView : public QObject{
        Q_OBJECT
    
    private slots:
        void newTabRequested(){
            std::cout<<"Hello"<<std::endl;
        }
    
        // ...
    }