0

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().

speakman
  • 936
  • 9
  • 11

2 Answers2

1

In constructor of you WebPage add:

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

And add slot

void WebPage::windowCloseRequested()
{
    this->view()->close();
}
Andrew
  • 173
  • 2
  • 8
  • Please see my own answer. You can just connect windowCloseRequest() signal to the deleteLater() slot. – speakman Nov 20 '12 at 10:06
0

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"
speakman
  • 936
  • 9
  • 11