如何让QWebView保存模态窗口的返回值?

时间:2012-08-28 09:39:05

标签: qt qtwebkit

是什么让这个小浏览器应用程序无法返回模态窗口中给出的window.returnValue? Qt Browser demo确实有效,但我无法理解它的原因,而这个迷你浏览器应用程序却没有。

#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()是同步的,应该在调用创建的模式对话框中返回设置为window.returnValue的值。小浏览器应用程序成功打开对话框窗口,但(模态)window.returnValue未设置为showModalDialog()的返回值。

2 个答案:

答案 0 :(得分:1)

在WebPage的构造函数中添加:

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

并添加插槽

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

答案 1 :(得分:0)

似乎没有正确处理windowCloseRequested()信号。这个例子将完美地运作:

#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"