From 1a872e5ff2ce538960c54986e09dcb5eb8d4bd63 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2019 20:42:01 +0200 Subject: HTTP example: use std::unique_ptr instead of QScopedPointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows the QFile factory there to actually return the payload in a unique_ptr instead of falling back to a raw pointer. The use of a unique_ptr member requires that the destructor be out-of-line, since QFile is only forward-declared in the header file. This is good hygiene, so do it for ProgressDialog, too. Change-Id: Idb6ed327f9592526bb7d0d5b2cfbffe9f08f3eea Reviewed-by: Edward Welbourne Reviewed-by: MÃ¥rten Nordheim --- examples/network/http/httpwindow.cpp | 24 ++++++++++++++++-------- examples/network/http/httpwindow.h | 8 ++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/network/http/httpwindow.cpp b/examples/network/http/httpwindow.cpp index 39ffb3cc87..c7bf0c0dff 100644 --- a/examples/network/http/httpwindow.cpp +++ b/examples/network/http/httpwindow.cpp @@ -48,13 +48,14 @@ ** ****************************************************************************/ +#include "httpwindow.h" + +#include "ui_authenticationdialog.h" + #include #include #include -#include "httpwindow.h" -#include "ui_authenticationdialog.h" - #if QT_CONFIG(ssl) const char defaultUrl[] = "https://www.qt.io/"; #else @@ -74,6 +75,10 @@ ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent) setMinimumSize(QSize(400, 75)); } +ProgressDialog::~ProgressDialog() +{ +} + void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes) { setMaximum(totalBytes); @@ -137,6 +142,10 @@ HttpWindow::HttpWindow(QWidget *parent) urlLineEdit->setFocus(); } +HttpWindow::~HttpWindow() +{ +} + void HttpWindow::startRequest(const QUrl &requestedUrl) { url = requestedUrl; @@ -204,9 +213,9 @@ void HttpWindow::downloadFile() startRequest(newUrl); } -QFile *HttpWindow::openFileForWrite(const QString &fileName) +std::unique_ptr HttpWindow::openFileForWrite(const QString &fileName) { - QScopedPointer file(new QFile(fileName)); + std::unique_ptr file(new QFile(fileName)); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Error"), tr("Unable to save the file %1: %2.") @@ -214,7 +223,7 @@ QFile *HttpWindow::openFileForWrite(const QString &fileName) file->errorString())); return nullptr; } - return file.take(); + return file; } void HttpWindow::cancelDownload() @@ -231,8 +240,7 @@ void HttpWindow::httpFinished() if (file) { fi.setFile(file->fileName()); file->close(); - delete file; - file = nullptr; + file.reset(); } if (httpRequestAborted) { diff --git a/examples/network/http/httpwindow.h b/examples/network/http/httpwindow.h index 20ad2bb4da..f7bd0047de 100644 --- a/examples/network/http/httpwindow.h +++ b/examples/network/http/httpwindow.h @@ -55,6 +55,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE class QFile; class QLabel; @@ -72,6 +74,7 @@ class ProgressDialog : public QProgressDialog { public: explicit ProgressDialog(const QUrl &url, QWidget *parent = nullptr); + ~ProgressDialog(); public slots: void networkReplyProgress(qint64 bytesRead, qint64 totalBytes); @@ -83,6 +86,7 @@ class HttpWindow : public QDialog public: explicit HttpWindow(QWidget *parent = nullptr); + ~HttpWindow(); void startRequest(const QUrl &requestedUrl); @@ -98,7 +102,7 @@ private slots: #endif private: - QFile *openFileForWrite(const QString &fileName); + std::unique_ptr openFileForWrite(const QString &fileName); QLabel *statusLabel; QLineEdit *urlLineEdit; @@ -110,7 +114,7 @@ private: QUrl url; QNetworkAccessManager qnam; QNetworkReply *reply; - QFile *file; + std::unique_ptr file; bool httpRequestAborted; }; -- cgit v1.2.3