From 8ab4d2028f971dcddd53fa0fc9313f5abe491fa2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2019 20:42:01 +0200 Subject: Composition example: use std::unique_ptr instead of QScopedPointer The use of a unique_ptr member requires that the destructor be out-of-line, since the payload is only forward-declared in the header file. This is good hygiene, so do it for CompositionWidget, too. Add 'explicit' and missing = nullptr to both constructors as a drive-by. Change-Id: Ied1c89864f90d3f2c13fb4e9a8bbbe2e6fd6f1d7 Reviewed-by: Giuseppe D'Angelo --- examples/widgets/painting/composition/composition.cpp | 12 ++++++++++-- examples/widgets/painting/composition/composition.h | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/widgets/painting/composition/composition.cpp b/examples/widgets/painting/composition/composition.cpp index 9bd71735a0..a220fb42fa 100644 --- a/examples/widgets/painting/composition/composition.cpp +++ b/examples/widgets/painting/composition/composition.cpp @@ -224,6 +224,10 @@ CompositionWidget::CompositionWidget(QWidget *parent) setWindowTitle(tr("Composition Modes")); } +CompositionWidget::~CompositionWidget() +{ +} + void CompositionWidget::nextMode() { @@ -265,6 +269,10 @@ CompositionRenderer::CompositionRenderer(QWidget *parent) #endif } +CompositionRenderer::~CompositionRenderer() +{ +} + QRectF rectangle_around(const QPointF &p, const QSizeF &size = QSize(250, 200)) { QRectF rect(p, size); @@ -371,7 +379,7 @@ void CompositionRenderer::paint(QPainter *painter) if (size() != m_previous_size) { m_previous_size = size(); - QPainter p(m_fbo.data()); + QPainter p(m_fbo.get()); p.setCompositionMode(QPainter::CompositionMode_Source); p.fillRect(QRect(QPoint(0, 0), size()), Qt::transparent); p.setCompositionMode(QPainter::CompositionMode_SourceOver); @@ -382,7 +390,7 @@ void CompositionRenderer::paint(QPainter *painter) painter->beginNativePainting(); { - QPainter p(m_fbo.data()); + QPainter p(m_fbo.get()); p.beginNativePainting(); m_blitter.bind(); const QRect targetRect(QPoint(0, 0), m_fbo->size()); diff --git a/examples/widgets/painting/composition/composition.h b/examples/widgets/painting/composition/composition.h index 19150e2024..52ca7919b0 100644 --- a/examples/widgets/painting/composition/composition.h +++ b/examples/widgets/painting/composition/composition.h @@ -61,6 +61,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE class QPushButton; class QRadioButton; @@ -71,7 +73,8 @@ class CompositionWidget : public QWidget Q_OBJECT public: - CompositionWidget(QWidget *parent); + explicit CompositionWidget(QWidget *parent = nullptr); + ~CompositionWidget(); public slots: void nextMode(); @@ -117,7 +120,8 @@ class CompositionRenderer : public ArthurFrame Q_PROPERTY(bool animation READ animationEnabled WRITE setAnimationEnabled) public: - CompositionRenderer(QWidget *parent); + explicit CompositionRenderer(QWidget *parent = nullptr); + ~CompositionRenderer(); void paint(QPainter *) override; @@ -188,7 +192,7 @@ private: int m_animationTimer; #if QT_CONFIG(opengl) - QScopedPointer m_fbo; + std::unique_ptr m_fbo; int m_pbuffer_size; // width==height==size of pbuffer uint m_base_tex; uint m_compositing_tex; -- cgit v1.2.3 From fbb2ed150585ec3aee936eba21b770a8d2b66a4d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2019 20:42:01 +0200 Subject: Icons example: use std::unique_ptr instead of QScopedPointer There's talk that QScopedPointer may be deprecated. Don't use it in examples anymore. Change-Id: Ia50ef66de84dae2885f71a9dd83c5909bfa253f0 Reviewed-by: Giuseppe D'Angelo --- examples/widgets/widgets/icons/mainwindow.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/widgets/widgets/icons/mainwindow.cpp b/examples/widgets/widgets/icons/mainwindow.cpp index 0cf11c978d..91e98ff896 100644 --- a/examples/widgets/widgets/icons/mainwindow.cpp +++ b/examples/widgets/widgets/icons/mainwindow.cpp @@ -55,6 +55,8 @@ #include "imagedelegate.h" #include "mainwindow.h" +#include + //! [40] enum { OtherSize = QStyle::PM_CustomBase }; //! [40] @@ -514,8 +516,8 @@ void MainWindow::checkCurrentStyle() const QList actions = styleActionGroup->actions(); for (QAction *action : actions) { const QString styleName = action->data().toString(); - QScopedPointer candidate(QStyleFactory::create(styleName)); - Q_ASSERT(!candidate.isNull()); + const std::unique_ptr candidate{QStyleFactory::create(styleName)}; + Q_ASSERT(candidate); if (candidate->metaObject()->className() == QApplication::style()->metaObject()->className()) { action->trigger(); -- cgit v1.2.3 From bc6ffcd0c583919b91cd0e41e1f29ff188fc617a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 May 2019 13:52:23 +0200 Subject: Torrent example: Replace the last Java-style iterator with STL ones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scope a previous iterator variable better so we can re-use 'it' as the iterator name. Change-Id: I46d239ad2d3646168408d1ef29ed74fd07bc663f Reviewed-by: Paul Wicking Reviewed-by: Mårten Nordheim --- examples/network/torrent/torrentclient.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp index b4cbbb7a45..bddf3caa1a 100644 --- a/examples/network/torrent/torrentclient.cpp +++ b/examples/network/torrent/torrentclient.cpp @@ -874,8 +874,7 @@ void TorrentClient::removeClient() // Remove the client from RateController and all structures. RateController::instance()->removeSocket(client); d->connections.removeAll(client); - QMultiMap::Iterator it = d->payloads.find(client); - while (it != d->payloads.end() && it.key() == client) { + for (auto it = d->payloads.find(client); it != d->payloads.end() && it.key() == client; /*erasing*/) { TorrentPiece *piece = it.value(); piece->inProgress = false; piece->requestedBlocks.fill(false); @@ -883,9 +882,12 @@ void TorrentClient::removeClient() } // Remove pending read requests. - QMapIterator it2(d->readIds); - while (it2.findNext(client)) - d->readIds.remove(it2.key()); + for (auto it = d->readIds.begin(), end = d->readIds.end(); it != end; /*erasing*/) { + if (it.value() == client) + it = d->readIds.erase(it); + else + ++it; + } // Delete the client later. disconnect(client, SIGNAL(disconnected()), this, SLOT(removeClient())); -- cgit v1.2.3 From 4715d6db808301e986c2399785c24ec70f68f200 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2019 20:42:01 +0200 Subject: ThreadedOpenGL example: use std::unique_ptr instead of QScopedPointer There's talk that QScopedPointer may be deprecated. Don't use it in examples anymore. Change-Id: I05a486c2a86fcc7015a9c21ed0ce9682b0c24034 Reviewed-by: Giuseppe D'Angelo --- examples/opengl/threadedqopenglwidget/main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/opengl/threadedqopenglwidget/main.cpp b/examples/opengl/threadedqopenglwidget/main.cpp index b9e491040f..983f608543 100644 --- a/examples/opengl/threadedqopenglwidget/main.cpp +++ b/examples/opengl/threadedqopenglwidget/main.cpp @@ -58,6 +58,8 @@ #include "mainwindow.h" #include "glwidget.h" +#include + static QString getGlString(QOpenGLFunctions *functions, GLenum name) { if (const GLubyte *p = functions->glGetString(name)) @@ -104,8 +106,8 @@ int main( int argc, char ** argv ) const QString toolTip = supportsThreading ? glInfo : glInfo + QStringLiteral("\ndoes not support threaded OpenGL."); topLevelGlWidget.setToolTip(toolTip); - QScopedPointer mw1; - QScopedPointer mw2; + std::unique_ptr mw1; + std::unique_ptr mw2; if (!parser.isSet(singleOption)) { if (supportsThreading) { pos += QPoint(100, 100); -- cgit v1.2.3 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 From e4c0fca194f8549f3b0fa8a45cf544e01ae6d1b2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 May 2019 20:42:01 +0200 Subject: RelationalTableModel example: use std::unique_ptr instead of QScopedPointer There's talk that QScopedPointer may be deprecated. Don't use it in examples anymore. Change-Id: I3c4647a569b72cd3a628c9b92ef34c87fd588342 Reviewed-by: Giuseppe D'Angelo --- examples/sql/relationaltablemodel/relationaltablemodel.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/sql/relationaltablemodel/relationaltablemodel.cpp b/examples/sql/relationaltablemodel/relationaltablemodel.cpp index c3c7d48d70..df4eae6e6e 100644 --- a/examples/sql/relationaltablemodel/relationaltablemodel.cpp +++ b/examples/sql/relationaltablemodel/relationaltablemodel.cpp @@ -53,6 +53,8 @@ #include "../connection.h" +#include + void initializeModel(QSqlRelationalTableModel *model) { //! [0] @@ -76,12 +78,12 @@ void initializeModel(QSqlRelationalTableModel *model) model->select(); } -QTableView *createView(const QString &title, QSqlTableModel *model) +std::unique_ptr createView(const QString &title, QSqlTableModel *model) { //! [4] - QTableView *view = new QTableView; + std::unique_ptr view{new QTableView}; view->setModel(model); - view->setItemDelegate(new QSqlRelationalDelegate(view)); + view->setItemDelegate(new QSqlRelationalDelegate(view.get())); //! [4] view->setWindowTitle(title); return view; @@ -118,7 +120,7 @@ int main(int argc, char *argv[]) initializeModel(&model); - QScopedPointer view(createView(QObject::tr("Relational Table Model"), &model)); + std::unique_ptr view = createView(QObject::tr("Relational Table Model"), &model); view->show(); return app.exec(); -- cgit v1.2.3