HTTP example: use std::unique_ptr instead of QScopedPointer

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 <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2019-05-14 20:42:01 +02:00
parent 6e121d81cb
commit 1a872e5ff2
2 changed files with 22 additions and 10 deletions

View File

@ -48,13 +48,14 @@
** **
****************************************************************************/ ****************************************************************************/
#include "httpwindow.h"
#include "ui_authenticationdialog.h"
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include <QUrl> #include <QUrl>
#include "httpwindow.h"
#include "ui_authenticationdialog.h"
#if QT_CONFIG(ssl) #if QT_CONFIG(ssl)
const char defaultUrl[] = "https://www.qt.io/"; const char defaultUrl[] = "https://www.qt.io/";
#else #else
@ -74,6 +75,10 @@ ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent)
setMinimumSize(QSize(400, 75)); setMinimumSize(QSize(400, 75));
} }
ProgressDialog::~ProgressDialog()
{
}
void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes) void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes)
{ {
setMaximum(totalBytes); setMaximum(totalBytes);
@ -137,6 +142,10 @@ HttpWindow::HttpWindow(QWidget *parent)
urlLineEdit->setFocus(); urlLineEdit->setFocus();
} }
HttpWindow::~HttpWindow()
{
}
void HttpWindow::startRequest(const QUrl &requestedUrl) void HttpWindow::startRequest(const QUrl &requestedUrl)
{ {
url = requestedUrl; url = requestedUrl;
@ -204,9 +213,9 @@ void HttpWindow::downloadFile()
startRequest(newUrl); startRequest(newUrl);
} }
QFile *HttpWindow::openFileForWrite(const QString &fileName) std::unique_ptr<QFile> HttpWindow::openFileForWrite(const QString &fileName)
{ {
QScopedPointer<QFile> file(new QFile(fileName)); std::unique_ptr<QFile> file(new QFile(fileName));
if (!file->open(QIODevice::WriteOnly)) { if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Error"), QMessageBox::information(this, tr("Error"),
tr("Unable to save the file %1: %2.") tr("Unable to save the file %1: %2.")
@ -214,7 +223,7 @@ QFile *HttpWindow::openFileForWrite(const QString &fileName)
file->errorString())); file->errorString()));
return nullptr; return nullptr;
} }
return file.take(); return file;
} }
void HttpWindow::cancelDownload() void HttpWindow::cancelDownload()
@ -231,8 +240,7 @@ void HttpWindow::httpFinished()
if (file) { if (file) {
fi.setFile(file->fileName()); fi.setFile(file->fileName());
file->close(); file->close();
delete file; file.reset();
file = nullptr;
} }
if (httpRequestAborted) { if (httpRequestAborted) {

View File

@ -55,6 +55,8 @@
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QUrl> #include <QUrl>
#include <memory>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QFile; class QFile;
class QLabel; class QLabel;
@ -72,6 +74,7 @@ class ProgressDialog : public QProgressDialog {
public: public:
explicit ProgressDialog(const QUrl &url, QWidget *parent = nullptr); explicit ProgressDialog(const QUrl &url, QWidget *parent = nullptr);
~ProgressDialog();
public slots: public slots:
void networkReplyProgress(qint64 bytesRead, qint64 totalBytes); void networkReplyProgress(qint64 bytesRead, qint64 totalBytes);
@ -83,6 +86,7 @@ class HttpWindow : public QDialog
public: public:
explicit HttpWindow(QWidget *parent = nullptr); explicit HttpWindow(QWidget *parent = nullptr);
~HttpWindow();
void startRequest(const QUrl &requestedUrl); void startRequest(const QUrl &requestedUrl);
@ -98,7 +102,7 @@ private slots:
#endif #endif
private: private:
QFile *openFileForWrite(const QString &fileName); std::unique_ptr<QFile> openFileForWrite(const QString &fileName);
QLabel *statusLabel; QLabel *statusLabel;
QLineEdit *urlLineEdit; QLineEdit *urlLineEdit;
@ -110,7 +114,7 @@ private:
QUrl url; QUrl url;
QNetworkAccessManager qnam; QNetworkAccessManager qnam;
QNetworkReply *reply; QNetworkReply *reply;
QFile *file; std::unique_ptr<QFile> file;
bool httpRequestAborted; bool httpRequestAborted;
}; };