简体   繁体   中英

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));
    }
};

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;
    }

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM