Search code examples
c++qtsignals-slots

Signals and Slots in qt5.7 - QWebEnginePage


I have trouble in connecting QWebEnginePage to fullScreenRequested , I am trying in the following way ant it gives the error

main.cpp:58: error: expected primary-expression before ',' token connect(this->view->page(), SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), &QWebEngineFullScreenRequest, SLOT(&QWebEngineFullScreenRequest::accept()));

My Code:

class WebView:public QObject{
public:
    char* home_page;
    QWebEngineView* view=new QWebEngineView();
    WebView(char* page=(char*)"https://google.com"){
        this->home_page=page;
        createWebView();
        this->view->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled,true);
        connect(this->view->page(),SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)),&QWebEngineFullScreenRequest,SLOT(&QWebEngineFullScreenRequest::accept()));
    }
    void createWebView(){
        this->view->load(QUrl(this->home_page));
    }
    QWebEngineView* returnView(){
        return this->view;
    }
    void home(){
        this->view->load(QUrl(this->home_page));
    }
};

Please help me to resolve this issue. Thanks


Solution

  • Your problem is that signal/slot connection takes a source object, as well as a destination object as parameters, and you mixed the 2 ways of connecting.

    It either

    connect(&src, &FirstClass::signalName, &dest, &SecondClass::slotName);
    

    Or

    connect(&src, SIGNAL(signalName(argType)), &dest, SLOT(slotName(artType)));
    

    In your case &QWebEngineFullScreenRequest is not an object, instead you are trying to take the address of a class. You need an instance of QWebEngineFullScreenRequest class to connect to it.

    Correct way:

        WebView(...)
        {
            //...
            connect(this->view->page(), &QWebEnginePage::fullScreenRequested, this, &WebView::acceptFullScreenRequest);
        }
    
    private slots:
        void acceptFullScreenRequest(QWebEngineFullScreenRequest request) {
            request.accept();
        }
    

    A few other remarks:

    • Try to separate class declaration in a header (.h), from the definition (.cpp) file.
    • Instead of char* page=(char*)"https://google.com", its better for literals to use const char*, or even better QString as you are using Qt
    • QWebEngineView* view=new QWebEngineView(); would be better to instantiate it inside your WebView constructor
    • this-> is unecessary

    WebView(QObject* parent = nullptr, QString page = "https://google.com"):
        QObject(parent),
        home_page(page),
        view(new QWebEngineView())
    {
    //...
    }