如何使用QWebEnginePage :: OpenLinkInNewTab [Qt5.8]

时间:2017-03-29 20:16:50

标签: c++ qt qt5 qwebview qaction

当我使用此代码点击Quora Feed中任何问题的链接时,该链接无法打开但不会打印“Hello”。你能告诉我我哪里错了吗?我很确定quora上的链接会发出OpenLinkInNewTab信号。请帮助,谢谢。

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

1 个答案:

答案 0 :(得分:0)

我认为问题是newTabRequested不是一个插槽。尝试

class WebView : public QObject{
    Q_OBJECT

private slots:
    void newTabRequested(){
        std::cout<<"Hello"<<std::endl;
    }

    // ...
}