我目前正在尝试开发一个基本的浏览器网站,以便使用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
第一个选项完美地工作,谢谢。我错误地认为createWindow只是通过使用openUrl函数的linkClicked信号来调用,然后我仔细阅读了QWebView文档。 – hicarus 2014-10-12 14:09:30