veroslav
29th August 2012, 07:30
Hi all,
I am using QNetworkAccessManager in order to download a file from the Internet in the following way:
void MainWindow::readUrlResource(const QString &url) {
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
//Get notified when the download completes
connect(networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(urlResourceDownloadCompleted(QNetworkReply*)) );
networkManager->get(request);
}
The function above is part of my main window (inheriting from QMainWindow) and it is what is sent as "this" to QNetworkAccessManager constructor. Also, the urlResourceDownloadCompleted() slot looks like this:
void MainWindow::urlResourceDownloadCompleted(QNetworkR eply *reply) {
//TODO: Handle the case when an error occurs during download
qDebug() << "Download completed signal received";
reply->deleteLater();
}
I am wondering whether I need to explicitly delete networkManager in some way or will QMainWindow (parent to it) take care of this eventually? I will have QMainWindow visible for a long time on screen so if it is it taking care of deletion, I wouldn't want to wait for so long, as I might want to create more networkManagers during this time.
If I need to delete the networkManager pointer manually, which is the best place to do so?
I am just trying my best to avoid potential memory leak due to not deleting networkManager pointer.
Thank you in advance. I am very new to Qt and I have some minor experience in C++ but I am having much fun so far and would like to learn thing properly.
Kind Regards,
Veroslav
I am using QNetworkAccessManager in order to download a file from the Internet in the following way:
void MainWindow::readUrlResource(const QString &url) {
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
//Get notified when the download completes
connect(networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(urlResourceDownloadCompleted(QNetworkReply*)) );
networkManager->get(request);
}
The function above is part of my main window (inheriting from QMainWindow) and it is what is sent as "this" to QNetworkAccessManager constructor. Also, the urlResourceDownloadCompleted() slot looks like this:
void MainWindow::urlResourceDownloadCompleted(QNetworkR eply *reply) {
//TODO: Handle the case when an error occurs during download
qDebug() << "Download completed signal received";
reply->deleteLater();
}
I am wondering whether I need to explicitly delete networkManager in some way or will QMainWindow (parent to it) take care of this eventually? I will have QMainWindow visible for a long time on screen so if it is it taking care of deletion, I wouldn't want to wait for so long, as I might want to create more networkManagers during this time.
If I need to delete the networkManager pointer manually, which is the best place to do so?
I am just trying my best to avoid potential memory leak due to not deleting networkManager pointer.
Thank you in advance. I am very new to Qt and I have some minor experience in C++ but I am having much fun so far and would like to learn thing properly.
Kind Regards,
Veroslav