I am currently trying to develop a basic browser web in order to surf on a specific internet site using qt framework. I created a MyWindow class which inherits QWebView, this in order to handle the opening of potential pop-ups in a new browser window. In this MyWindow class I also recreated the createWindow function. Moreover, I also created QNetworkAccessManager and QNetworkCookieJar objects inside MyWindow class and without recreating any further new function. I thought this was sufficient to surf on the website I was targeting, as it is has a log-in form in its homepage, while you can surf on the other pages of the same site just using the information contained inside server-generated cookies at log-in. It works well during "normal" navigation, while I always get an error when clicking on links such as
<a class="lien_default lienPerso" href="javascript:popupPerso('foo.php?login=bar')">bar</a>
In this case, a new window is prompted (I discovered that the javascript function is a simple window.open) but it seems it cannot retrieve the information from cookies: current session is not used and the new window asks for logging-in again. Only after logging-in in this pop-up window I can surf the correct linked page. My intention of course was to use the information of each current session to access the information of this link. And this behaviour (i.e. no second log-in request) is actually what you get while surfing this web page with standard browsers. I also discovered that these links are not handled by linkClicked signal due to javascript code.
Please find here under my code:
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();
}
Thank you
Hicarus
The page created in MyWindow::createWindow
doesn't seem to get its networkAccessManager set from which it could get the cookies, try something like:
QWebPage *newWeb = new QWebPage(webView);
newWeb->setNetworkAccessManager(page()->networkAccessManager());
Another option would be to create a subclass of QWebPage where you would do this logic instead of doing it in multiple places, and return such an object from createWindow.