Reputation: 3
I am new at Qt and using Qt5.5 to create a http client application which will load a window with a fixed URL. After login in this window the other windows of same site should get the same session. At this purpose I have used global pointer jar
of QNetworkCookieJar
class and implemented on the following code for every window
Window1::Window1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window1)
{
ui->setupUi(this);
QUrl webURL("http://someURL");
ui->webView->show();
ui->webView->load(webURL);
ui->webView->page()->networkAccessManager()->setCookieJar(jar);
}
It solves the multiple authentication problem, but when I close any window and reopen it instantly it causes appcrash.
Please give me a suggestion on my problem. Thanks in advance.
Upvotes: 0
Views: 117
Reputation: 3
Thanks ThorngardSO.. I have found a solution using your answer. Here is my solution,
if(jar.isNull()==true){
qDebug()<<"object null";
QPointer <QNetworkCookieJar> jar_new = new QNetworkCookieJar(0);
ui->webView->page()->networkAccessManager()->setCookieJar(jar_new);
jar=jar_new;
}
else
ui->webView->page()->networkAccessManager()->setCookieJar(jar);
Upvotes: 0
Reputation: 1211
From the Qt5 docs (http://doc.qt.io/qt-5/qnetworkaccessmanager.html#setCookieJar):
Note: QNetworkAccessManager takes ownership of the cookieJar object.
So the accessmanager will delete your jar
instance. There might be your problem! I don't know enough about the webview/page/accessmanager to be sure about the lifetime of the manager, but it seems to be bound to the webview/your ui, so when it's closed+destroyed, your cookiejar will have gone, too.
As QNetworkCookieJar
inherits from QObject, you might want to use a guarded QPointer<QNetworkCookieJar> jar
instead of a simple QNetworkCookieJar* jar
variable. Then, you will notice that your guarded pointer becomes null after the first window is closed/destroyed. That would a) verify my claim from the last paragraph and b) guard you against stale pointer accesses in the future (in fact, your program would no longer crash).
Upvotes: 1