¿Cómo hacer que QWebView conserve los valores de retorno de las ventanas modales?

What makes this small browser app fail to return the window.returnValue given in the modal window? The Qt Browser demo does make it work, but I can't figure why it does and this mini browser app doesn't.

#include <QApplication>
#include <QWebView>
#include <QWebPage>
#include <QUrl>

class WebPage : public QWebPage
{
public:
    QWebPage *createWindow(QWebPage::WebWindowType type)
    {
        QWebView *wv = new QWebView;
        if (type == QWebPage::WebModalDialog)
            wv->setWindowModality(Qt::ApplicationModal);
        return wv->page();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWebView view;

    QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);

    view.setPage(new WebPage);
    view.load(QUrl("http://help.dottoro.com/external/examples/ljdlgxbu/showModalDialog_1.htm"));
    view.show();

    return a.exec();
}

showModalDialog() is synchronous and should return the value set as window.returnValue in the modal dialog created by the call. The small browser app opens the dialog window successfully, but the (modal)window.returnValue is not set as return value for showModalDialog().

preguntado el 28 de agosto de 12 a las 10:08

2 Respuestas

In constructor of you WebPage add:

connect(this, SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));

And add slot

void WebPage::windowCloseRequested()
{
    this->view()->close();
}

respondido 08 nov., 12:15

Please see my own answer. You can just connect windowCloseRequest() signal to the deleteLater() slot. - Speakman

Seems like the windowCloseRequested() signal wasn't handled correctly. This example will work perfectly:

#include <QApplication>
#include <QWebView>
#include <QWebPage>
#include <QUrl>

class WebView : public QWebView
{
    Q_OBJECT
public:
    WebView(QWidget *parent = 0);
};

class WebPage : public QWebPage
{
    Q_OBJECT
public:
    WebPage(QObject *parent = 0) : QWebPage(parent) {
    }

    virtual QWebPage *createWindow(QWebPage::WebWindowType)
    {
        QWebView *view = new WebView();

        return view->page();
    }
};

WebView::WebView(QWidget *parent) : QWebView(parent) {
    setPage(new WebPage(this));
    connect(this->page(), SIGNAL(windowCloseRequested()), this, SLOT(deleteLater()));
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WebView view;

    QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    view.load(QUrl("http://help.dottoro.com/external/examples/ljdlgxbu/showModalDialog_1.htm"));
    view.show();

    return a.exec();
}

#include "main.moc"

Respondido el 03 de Septiembre de 12 a las 14:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.