2014-10-12 85 views
1

我目前正在嘗試開發一個基本的瀏覽器網站,以便使用qt框架在特定的互聯網網站上瀏覽。我創建了一個繼承QWebView的MyWindow類,這是爲了處理在新的瀏覽器窗口中打開潛在的彈出窗口。在這個MyWindow類中,我還重新創建了createWindow函數。此外,我還在MyWindow類中創建了QNetworkAccessManager和QNetworkCookieJar對象,並且不需要重新創建任何新的函數。我認爲這足以在我定位的網站上瀏覽,因爲它在其主頁上有一個登錄表單,而您只需使用服務器生成的cookie中包含的信息瀏覽同一網站的其他頁面即可登錄。它在「正常」的導航效果很好,而我在等環節Javascript不使用QWebView cookies/session

<a class="lien_default lienPerso" href="javascript:popupPerso('foo.php?login=bar')">bar</a> 

在這種情況下,一個新的窗口提示點擊時,一定要得到一個錯誤(我發現JavaScript函數是一個簡單的window.open ),但它似乎無法從cookie中檢索信息:當前會話未被使用,並且新窗口要求再次登錄。只有在彈出窗口登錄後,我才能瀏覽正確的鏈接頁面。我的目的當然是使用當前每個會話的信息來訪問這個鏈接的信息。這種行爲(即沒有第二次登錄請求)實際上是您在使用標準瀏覽器瀏覽此網頁時獲得的。 我還發現,由於JavaScript代碼,這些鏈接不能通過linkClicked信號處理。

請在這裏找到下我的代碼:

的main.cpp

#include <QApplication> 
#include "mainwindow.h" 


int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow mWind; 
    mWind.show(); 


    return a.exec(); 
} 

mywindow.cpp

#include <QWebView> 
#include "mywindow.h" 

    MyWindow::MyWindow (QWidget * parent):QWebView(parent) 
    { 


    } 

    QWebView *MyWindow::createWindow(QWebPage::WebWindowType type) 
    { 
     Q_UNUSED(type); 
     QWebView *webView = new QWebView; 
     QWebPage *newWeb = new QWebPage(webView); 
     webView->setAttribute(Qt::WA_DeleteOnClose, true); 
     webView->setPage(newWeb); 

     return webView; 
    } 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include "mywindow.h" 
#include <QWebView> 
#include <QWebFrame> 
#include <QNetworkAccessManager> 
#include <QNetworkCookieJar> 


MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 

{ 
    QUrl url([url of the website I was targeting]); 



    ui->setupUi(this); 
    QWebSettings *settings = ui->w->settings();     // w is an object of MyWindow class that i promoted in Design Panel 
    settings->setAttribute(QWebSettings::JavascriptEnabled, true); 
    settings->setAttribute(QWebSettings::PluginsEnabled, true); 
    settings->setAttribute(QWebSettings::AutoLoadImages, true); 
    settings->setAttribute(QWebSettings::JavaEnabled, false); 
    settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true); 
    ui->w->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); // This was a test on links 

    QNetworkCookieJar* cookieJar = new QNetworkCookieJar(); 
    QNetworkAccessManager* nam = new QNetworkAccessManager(this); 
    nam->setCookieJar(cookieJar); 


    ui->w->page()->setNetworkAccessManager(nam); 
    ui->w->load(url); 
    ui->w->show(); 
    net=nam; 

    connect(ui->w,SIGNAL(linkClicked(QUrl)),this,SLOT(openUrl(QUrl))); 


} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::openUrl(QUrl url) 
{ 

    QWebView *n = ui->w->createWindow(QWebPage::WebBrowserWindow); 
    n->page()->setNetworkAccessManager(net); 
    n->load(url); 
    n->show(); 

} 

謝謝

Hicarus

回答

1

MyWindow::createWindow創建似乎並沒有得到其networkAccessManager設置從中可以得到的餅乾,你可以試試頁:

QWebPage *newWeb = new QWebPage(webView); 
newWeb->setNetworkAccessManager(page()->networkAccessManager()); 

另一種選擇是創建的子類QWebPage在哪裏你會做這個邏輯,而不是在多個地方做,並從createWindow返回這樣一個對象。

+0

第一個選項完美地工作,謝謝。我錯誤地認爲createWindow只是通過使用openUrl函數的linkClicked信號來調用,然後我仔細閱讀了QWebView文檔。 – hicarus 2014-10-12 14:09:30