2012-08-28 37 views
0

是什麼讓這個小的瀏覽器應用程序無法返回模態窗口中給出的window.returnValue?該Qt Browser demo確實使它的工作,但我不明白爲什麼它和這個迷你瀏覽器應用程序沒有。如何使QWebView節省模態窗口的返回值?

#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()的返回值。

回答

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

在你的構造函數中添加好康:

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

並添加插槽

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

請看看我自己的答案。您可以將windowCloseRequest()信號連接到deleteLater()插槽。 – speakman