2013-07-13 62 views
1

我正在Windows 8上使用Qt 5.1進行簡單的Web重做。例如,如果我導航到Google,登錄然後搜索「hello world」,我想保存信息並稍後重做(即,重做提交表單和登錄)。重新實現acceptNavigationRequest()

我試過的是這個。

。創建新的Qt GUI應用程序;

。 Add class:

Name: MyWebView , Base class: QWebView 
Inherits QWidget 
header: mywebview.h 
src: mywebview.cpp 

。 Add class:

Name: MyWebPage, Base class: QWebPage 
Inherits QObject 
header: mywebpage.h 
src: mywebpage.cpp 

。修改MyWebPage.h:

add 
public: 
bool QWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, NavigationType type); 

。修改MyWebPage.cpp:

add 
    #include <QMessageBox> 
    add 
    bool MyWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type) 
{ 
    QMessageBox::about(0,"it works","it works"); 
} 

。修改MyWebView.h:

add 
#include "mywebpage.h" 
add 
public: 
MyWebPage *mwp; 
MyWebPage * page(){ return mwp;} 

。將下面的代碼添加到untitled1.pro中:

QT  += webkitwidgets 

。將main.cpp更改爲:

#include "mainwindow.h" 
#include <QApplication> 
#include "mywebview.h" 
int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 
    MyWebView wv; 
    wv.show(); 
    return a.exec(); 
} 

。構建並運行。

我希望每次點擊按鈕(表單或鏈接)時,「它的工作」消息顯示爲QMessageBox::about,但它從未發生過。

如何正確地重新實現acceptNavigationRequest

回答

0

我改變mywebview.cpp到

#include "mywebview.h" 
#include "mywebpage.h" 
MyWebView::MyWebView(QWidget *parent) : 
    QWebView(parent), mwp(new MyWebPage(this)) 
{ 
    setPage(mwp); 
} 

,它工作正常