1

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));
    }
};
cbuchart
  • 10,847
  • 9
  • 53
  • 93

1 Answers1

0

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

    // ...
}
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • your method is also giving an error `undefined reference to vtable for WebView` –  Mar 29 '17 at 20:43
  • It's compiling for me. Is the class in a header (.h) file or in a .cpp? If the second, then you have to make a workaround and manually include the moc'ed file at the end. – cbuchart Mar 29 '17 at 20:51
  • I've just noticed too that the `createWebView` method is called _before_ connecting the slot. In that function you load the page. I'm not an expert in QWebEngine but, shouldn't the page be loaded _after_ connecting and setting the rest of attributes? – cbuchart Mar 29 '17 at 20:54
  • is it related to `QWebEngineView::createWindow(QWebEnginePage::WindowType type)` ? –  Mar 30 '17 at 07:50
  • As I said, not an QWEV expert, but it seems so, maybe somebody else may clarify this better – cbuchart Mar 30 '17 at 09:50