qt5.7中的信号与槽 - QWebEnginePage

Signals and Slots in qt5.7 - QWebEnginePage

我在将 QWebEnginePage 连接到 fullScreenRequested 时遇到问题,我正在尝试以下方式,但它给出了错误

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

我的代码:

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

请帮我解决这个问题。谢谢

您的问题是 signal/slot 连接采用源 object 以及目标 object 作为参数,而您混合了两种连接方式。

要么

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

或者

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

在你的情况下 &QWebEngineFullScreenRequest 不是 object,而是你试图获取 class 的地址。您需要 QWebEngineFullScreenRequest class 的实例才能连接到它。

正确方法:

    WebView(...)
    {
        //...
        connect(this->view->page(), &QWebEnginePage::fullScreenRequested, this, &WebView::acceptFullScreenRequest);
    }

private slots:
    void acceptFullScreenRequest(QWebEngineFullScreenRequest request) {
        request.accept();
    }

其他几点说明:

  • 尝试将 header (.h) 中的 class 声明与定义 (.cpp) 文件分开。
  • 而不是char* page=(char*)"https://google.com",文字使用const char*更好,或者更好的QString,因为你正在使用Qt
  • QWebEngineView* view=new QWebEngineView(); 最好在 WebView 构造函数
  • 中实例化它
  • this->不需要

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