QWebView allow pop-ups?

2019-08-15 08:15发布

问题:

In my project, I have a QWebView that loads a page that opens a pop-up window. But the window won't open. I looked into the createWindow function but I have no clue how to subclass a widget. These are some settings I put onto the webView:

QWebSettings *settings = ui->webView_2->settings();
settings->setAttribute(QWebSettings::JavascriptEnabled, true);
settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);

What is the easiest way to allow my webView to allow pop-up windows?

Thanks for your time :)

回答1:

You need to reimplement QWebView's createWindow method. The QWebView returned will be set to the required URL automatically.

For example:

QWebView* WebView::createWindow(QWebPage::WebWindowType type)
{
    // WindowDialog is just a simple QDialog with a QWebView
    WindowDialog* dlg = new WindowDialog(this);
    dlg->show();

    // A method to retrieve a pointer to the QWebView of the dialog is needed
    return dlg->webView();
}

Keep in mind that cookies do not share between the two QWebViews, so you also need to implement your own cookie manager. One way to do it would be to inherit QNetworkCookieJar, and keep a static "global" instance.