QNetworkReply not deleting
-
Hello! I'm trying to delete QNetworkReply after manager's "finished" signal is emitted. When this signal is emitted again, i get deleted QNetworkReply triggered too.
I expect old reply to be deleted and only new one triggering signal. What can i do?
Thanks!t_manager.h:
#include <QObject> #include <QNetworkAccessManager> class T_Manager : public QNetworkAccessManager { Q_OBJECT public: T_Manager(); ~T_Manager(); bool set(QString host_url, int port, QString cert_path); void exchangeGet(QString URI); signals: void receivedData(QString); void receivedError(QString); private: QSslConfiguration ssl_config_; QUrl host_url_; int host_port_; private slots: void managerFinished(QNetworkReply* reply); };
t_manager.cpp:
#include "t_manager.h" #include <QNetworkReply> T_Manager::T_Manager() { } T_Manager::~T_Manager() { } bool T_Manager::set(QString host_url, int port, QString cert_path) { if(host_url.isEmpty() || cert_path.isEmpty()) return false; host_url_.setUrl("https://" + host_url); host_port_ = port; QList<QSslCertificate> certs = QSslCertificate::fromPath(cert_path); if(certs.size() <= 0 || !host_url_.isValid()) return false; ssl_config_.setCaCertificates(certs); return true; } void T_Manager::exchangeGet(QString URI) { QUrl request_url = QUrl(host_url_.toString() + URI); QNetworkRequest request(request_url); request.setSslConfiguration(ssl_config_); QNetworkReply *reply = this->get(request); connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*))); } void T_Manager::managerFinished(QNetworkReply *reply) { if(reply->error()) { emit receivedError(reply->errorString()); reply->disconnect(); reply->deleteLater(); return; } QByteArray bytes = reply->readAll(); QString received_str = QString::fromUtf8(bytes.data(), bytes.size()); int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if(statusCode == 200){ emit receivedData(received_str); } reply->disconnect(); reply->deleteLater(); }
-
@JonB no Qt does not forbid you to do that.
@Ivan-Megazoo do that connection in your constructor and only there.
Beside that, you are already deleting the reply in the slot so why are you trying to delete it even more ?
On a side note, why are you subclassing QNetworkAccessManager ? Your T_Manager class should rather be in the "has a" rather than "is a".
-
@Ivan-Megazoo said in QNetworkReply not deleting:
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));
The problem does not seem about two reply object, I think problem is related connecting two times.
You can try this.
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)),Qt::UniqueConnection );
-
@Ivan-Megazoo said in QNetworkReply not deleting:
When this signal is emitted again, i get deleted QNetworkReply triggered too.
I wish I understood this! :)
If you mean: you call
exchangeGet()
a second time then, as @CKurdu says, you will executeconnect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(managerFinished(QNetworkReply*)));
a second time. Unless Qt prevents duplicates (I don't know), your slot will get called twice, is that what you mean? Why are you doing the
connect()
in this function, instead of outside? -
@JonB no Qt does not forbid you to do that.
@Ivan-Megazoo do that connection in your constructor and only there.
Beside that, you are already deleting the reply in the slot so why are you trying to delete it even more ?
On a side note, why are you subclassing QNetworkAccessManager ? Your T_Manager class should rather be in the "has a" rather than "is a".
-
-
@Ivan-Megazoo
What @SGaist is suggesting is that yourT_Manager
class has aQNetworkAccessManager
as one thing inside it, but it does not perform the function of aQNetworkAccessManager
itself (it does much different) and therefore better is a has aQNetworkAccessManager
rather than is aQNetworkAccessManager
, which is what sub-classing implies.So he is suggesting you rearrange your architecture so that
T_Manager
does not derive from/sub-classQNetworkManager
, instead it would have aQNetworkManager m_netman
member variable, and you do network operations via that member variable.