From b0042601ed8056c9ed6056c26f71dfa9fbc951ce Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 13 Sep 2019 20:43:09 +0200 Subject: [PATCH] Cleanup QtOpenGL examples Cleanup the OpenGL examples - use nullptr (clang-tidy) - use member-initialization - avoid redundant checks for != nullptr when deleting a pointer Change-Id: I3e4702690ed79e71c3e952d51ceef83b907b45b7 Reviewed-by: Edward Welbourne Reviewed-by: Laszlo Agocs --- examples/gui/openglwindow/main.cpp | 30 +++++------- examples/gui/openglwindow/openglwindow.cpp | 11 ++--- examples/gui/openglwindow/openglwindow.h | 12 ++--- examples/opengl/computegles31/glwindow.cpp | 39 +++------------- examples/opengl/computegles31/glwindow.h | 22 ++++----- examples/opengl/contextinfo/renderwindow.cpp | 4 +- examples/opengl/contextinfo/widget.h | 2 +- examples/opengl/cube/geometryengine.cpp | 2 +- examples/opengl/cube/mainwidget.cpp | 10 +--- examples/opengl/cube/mainwidget.h | 8 ++-- examples/opengl/hellogl2/glwidget.cpp | 14 +++--- examples/opengl/hellogl2/glwidget.h | 18 ++++---- examples/opengl/hellogl2/logo.cpp | 1 - examples/opengl/hellogl2/logo.h | 2 +- examples/opengl/hellogl2/mainwindow.cpp | 3 +- examples/opengl/hellogl2/window.cpp | 8 ++-- examples/opengl/hellogles3/glwindow.cpp | 32 +++---------- examples/opengl/hellogles3/glwindow.h | 26 +++++------ examples/opengl/qopenglwidget/bubble.cpp | 7 +-- examples/opengl/qopenglwidget/bubble.h | 2 +- examples/opengl/qopenglwidget/glwidget.cpp | 8 ---- examples/opengl/qopenglwidget/glwidget.h | 46 +++++++++---------- examples/opengl/textures/glwidget.cpp | 11 ----- examples/opengl/textures/glwidget.h | 14 +++--- .../opengl/threadedqopenglwidget/glwidget.cpp | 7 +-- .../opengl/threadedqopenglwidget/glwidget.h | 18 ++++---- 26 files changed, 132 insertions(+), 225 deletions(-) diff --git a/examples/gui/openglwindow/main.cpp b/examples/gui/openglwindow/main.cpp index 90c93f0d37..d1e5d3871e 100644 --- a/examples/gui/openglwindow/main.cpp +++ b/examples/gui/openglwindow/main.cpp @@ -50,36 +50,30 @@ #include "openglwindow.h" -#include -#include -#include -#include +#include +#include +#include +#include +#include -#include //! [1] class TriangleWindow : public OpenGLWindow { public: - TriangleWindow(); + using OpenGLWindow::OpenGLWindow; void initialize() override; void render() override; private: - GLuint m_posAttr; - GLuint m_colAttr; - GLuint m_matrixUniform; + GLuint m_posAttr = 0; + GLuint m_colAttr = 0; + GLuint m_matrixUniform = 0; - QOpenGLShaderProgram *m_program; - int m_frame; + QOpenGLShaderProgram *m_program = nullptr; + int m_frame = 0; }; - -TriangleWindow::TriangleWindow() - : m_program(0) - , m_frame(0) -{ -} //! [1] //! [2] @@ -144,7 +138,7 @@ void TriangleWindow::render() m_program->bind(); QMatrix4x4 matrix; - matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f); + matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f); matrix.translate(0, 0, -2); matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0); diff --git a/examples/gui/openglwindow/openglwindow.cpp b/examples/gui/openglwindow/openglwindow.cpp index a0c85006bd..bac887dca0 100644 --- a/examples/gui/openglwindow/openglwindow.cpp +++ b/examples/gui/openglwindow/openglwindow.cpp @@ -50,18 +50,13 @@ #include "openglwindow.h" -#include - -#include -#include -#include +#include +#include +#include //! [1] OpenGLWindow::OpenGLWindow(QWindow *parent) : QWindow(parent) - , m_animating(false) - , m_context(0) - , m_device(0) { setSurfaceType(QWindow::OpenGLSurface); } diff --git a/examples/gui/openglwindow/openglwindow.h b/examples/gui/openglwindow/openglwindow.h index 6e6c1d7449..8db943ddde 100644 --- a/examples/gui/openglwindow/openglwindow.h +++ b/examples/gui/openglwindow/openglwindow.h @@ -48,8 +48,8 @@ ** ****************************************************************************/ -#include -#include +#include +#include QT_BEGIN_NAMESPACE class QPainter; @@ -62,7 +62,7 @@ class OpenGLWindow : public QWindow, protected QOpenGLFunctions { Q_OBJECT public: - explicit OpenGLWindow(QWindow *parent = 0); + explicit OpenGLWindow(QWindow *parent = nullptr); ~OpenGLWindow(); virtual void render(QPainter *painter); @@ -82,10 +82,10 @@ protected: void exposeEvent(QExposeEvent *event) override; private: - bool m_animating; + bool m_animating = false; - QOpenGLContext *m_context; - QOpenGLPaintDevice *m_device; + QOpenGLContext *m_context = nullptr; + QOpenGLPaintDevice *m_device = nullptr; }; //! [1] diff --git a/examples/opengl/computegles31/glwindow.cpp b/examples/opengl/computegles31/glwindow.cpp index 7a14cba66d..2194137cfd 100644 --- a/examples/opengl/computegles31/glwindow.cpp +++ b/examples/opengl/computegles31/glwindow.cpp @@ -73,15 +73,6 @@ #endif GLWindow::GLWindow() - : m_texImageInput(0), - m_texImageTmp(0), - m_texImageProcessed(0), - m_shaderDisplay(0), - m_shaderComputeV(0), - m_shaderComputeH(0), - m_blurRadius(0.0f), - m_animate(true), - m_vao(0) { const float animationStart = 0.0; const float animationEnd = 10.0; @@ -324,27 +315,18 @@ void GLWindow::initializeGL() << ((ctx->format().renderableType() == QSurfaceFormat::OpenGLES) ? (" GLES") : (" GL")) << " context"; - if (m_texImageInput) { - delete m_texImageInput; - m_texImageInput = 0; - } QImage img(":/Qt-logo-medium.png"); Q_ASSERT(!img.isNull()); + delete m_texImageInput; m_texImageInput = new QOpenGLTexture(img.convertToFormat(QImage::Format_RGBA8888).mirrored()); - if (m_texImageTmp) { - delete m_texImageTmp; - m_texImageTmp = 0; - } + delete m_texImageTmp; m_texImageTmp = new QOpenGLTexture(QOpenGLTexture::Target2D); m_texImageTmp->setFormat(m_texImageInput->format()); m_texImageTmp->setSize(m_texImageInput->width(),m_texImageInput->height()); m_texImageTmp->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); // WTF? - if (m_texImageProcessed) { - delete m_texImageProcessed; - m_texImageProcessed = 0; - } + delete m_texImageProcessed; m_texImageProcessed = new QOpenGLTexture(QOpenGLTexture::Target2D); m_texImageProcessed->setFormat(m_texImageInput->format()); m_texImageProcessed->setSize(m_texImageInput->width(),m_texImageInput->height()); @@ -354,10 +336,7 @@ void GLWindow::initializeGL() m_texImageProcessed->setMinificationFilter(QOpenGLTexture::Linear); m_texImageProcessed->setWrapMode(QOpenGLTexture::ClampToEdge); - if (m_shaderDisplay) { - delete m_shaderDisplay; - m_shaderDisplay = 0; - } + delete m_shaderDisplay; m_shaderDisplay = new QOpenGLShaderProgram; // Prepend the correct version directive to the sources. The rest is the // same, thanks to the common GLSL syntax. @@ -365,18 +344,12 @@ void GLWindow::initializeGL() m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Fragment, versionedShaderCode(fsDisplaySource)); m_shaderDisplay->link(); - if (m_shaderComputeV) { - delete m_shaderComputeV; - m_shaderComputeV = 0; - } + delete m_shaderComputeV; m_shaderComputeV = new QOpenGLShaderProgram; m_shaderComputeV->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceV)); m_shaderComputeV->link(); - if (m_shaderComputeH) { - delete m_shaderComputeH; - m_shaderComputeH = 0; - } + delete m_shaderComputeH; m_shaderComputeH = new QOpenGLShaderProgram; m_shaderComputeH->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceH)); m_shaderComputeH->link(); diff --git a/examples/opengl/computegles31/glwindow.h b/examples/opengl/computegles31/glwindow.h index f243858a45..5375fbb488 100644 --- a/examples/opengl/computegles31/glwindow.h +++ b/examples/opengl/computegles31/glwindow.h @@ -90,21 +90,21 @@ protected: void setAnimating(bool animate); private: - QPropertyAnimation *m_animationForward; - QPropertyAnimation *m_animationBackward; + QPropertyAnimation *m_animationForward = nullptr; + QPropertyAnimation *m_animationBackward = nullptr; QSequentialAnimationGroup *m_animationGroup; - QOpenGLTexture *m_texImageInput; - QOpenGLTexture *m_texImageTmp; - QOpenGLTexture *m_texImageProcessed; - QOpenGLShaderProgram *m_shaderDisplay; - QOpenGLShaderProgram *m_shaderComputeV; - QOpenGLShaderProgram *m_shaderComputeH; + QOpenGLTexture *m_texImageInput = nullptr; + QOpenGLTexture *m_texImageTmp = nullptr; + QOpenGLTexture *m_texImageProcessed = nullptr; + QOpenGLShaderProgram *m_shaderDisplay = nullptr; + QOpenGLShaderProgram *m_shaderComputeV = nullptr; + QOpenGLShaderProgram *m_shaderComputeH = nullptr; QMatrix4x4 m_proj; QSizeF m_quadSize; - int m_blurRadius; - bool m_animate; - QOpenGLVertexArrayObject *m_vao; + int m_blurRadius = 0; + bool m_animate = true; + QOpenGLVertexArrayObject *m_vao = nullptr; }; #endif diff --git a/examples/opengl/contextinfo/renderwindow.cpp b/examples/opengl/contextinfo/renderwindow.cpp index 21b7434be6..ea9a7a24cb 100644 --- a/examples/opengl/contextinfo/renderwindow.cpp +++ b/examples/opengl/contextinfo/renderwindow.cpp @@ -56,7 +56,7 @@ #include RenderWindow::RenderWindow(const QSurfaceFormat &format) - : m_context(0), + : m_context(nullptr), m_initialized(false), m_forceGLSL110(false), m_angle(0.0f) @@ -67,7 +67,7 @@ RenderWindow::RenderWindow(const QSurfaceFormat &format) m_context->setFormat(requestedFormat()); if (!m_context->create()) { delete m_context; - m_context = 0; + m_context = nullptr; } } diff --git a/examples/opengl/contextinfo/widget.h b/examples/opengl/contextinfo/widget.h index 72abe0e647..85d181a229 100644 --- a/examples/opengl/contextinfo/widget.h +++ b/examples/opengl/contextinfo/widget.h @@ -64,7 +64,7 @@ class Widget : public QWidget Q_OBJECT public: - explicit Widget(QWidget *parent = 0); + explicit Widget(QWidget *parent = nullptr); private slots: void start(); diff --git a/examples/opengl/cube/geometryengine.cpp b/examples/opengl/cube/geometryengine.cpp index 1f9e16a935..24e014201e 100644 --- a/examples/opengl/cube/geometryengine.cpp +++ b/examples/opengl/cube/geometryengine.cpp @@ -174,6 +174,6 @@ void GeometryEngine::drawCubeGeometry(QOpenGLShaderProgram *program) program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); // Draw cube geometry using indices from VBO 1 - glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0); + glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, nullptr); } //! [2] diff --git a/examples/opengl/cube/mainwidget.cpp b/examples/opengl/cube/mainwidget.cpp index 292a4245fa..558ecd1299 100644 --- a/examples/opengl/cube/mainwidget.cpp +++ b/examples/opengl/cube/mainwidget.cpp @@ -52,15 +52,7 @@ #include -#include - -MainWidget::MainWidget(QWidget *parent) : - QOpenGLWidget(parent), - geometries(0), - texture(0), - angularSpeed(0) -{ -} +#include MainWidget::~MainWidget() { diff --git a/examples/opengl/cube/mainwidget.h b/examples/opengl/cube/mainwidget.h index cd2f8098ad..b6a03d454a 100644 --- a/examples/opengl/cube/mainwidget.h +++ b/examples/opengl/cube/mainwidget.h @@ -69,7 +69,7 @@ class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions Q_OBJECT public: - explicit MainWidget(QWidget *parent = 0); + using QOpenGLWidget::QOpenGLWidget; ~MainWidget(); protected: @@ -87,15 +87,15 @@ protected: private: QBasicTimer timer; QOpenGLShaderProgram program; - GeometryEngine *geometries; + GeometryEngine *geometries = nullptr; - QOpenGLTexture *texture; + QOpenGLTexture *texture = nullptr; QMatrix4x4 projection; QVector2D mousePressPosition; QVector3D rotationAxis; - qreal angularSpeed; + qreal angularSpeed = 0; QQuaternion rotation; }; diff --git a/examples/opengl/hellogl2/glwidget.cpp b/examples/opengl/hellogl2/glwidget.cpp index 318adb5043..543e70f8ac 100644 --- a/examples/opengl/hellogl2/glwidget.cpp +++ b/examples/opengl/hellogl2/glwidget.cpp @@ -57,11 +57,7 @@ bool GLWidget::m_transparent = false; GLWidget::GLWidget(QWidget *parent) - : QOpenGLWidget(parent), - m_xRot(0), - m_yRot(0), - m_zRot(0), - m_program(0) + : QOpenGLWidget(parent) { m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile; // --transparent causes the clear color to be transparent. Therefore, on systems that @@ -133,7 +129,7 @@ void GLWidget::cleanup() makeCurrent(); m_logoVbo.destroy(); delete m_program; - m_program = 0; + m_program = nullptr; doneCurrent(); } @@ -250,8 +246,10 @@ void GLWidget::setupVertexAttribs() QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glEnableVertexAttribArray(0); f->glEnableVertexAttribArray(1); - f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0); - f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast(3 * sizeof(GLfloat))); + f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), + nullptr); + f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), + reinterpret_cast(3 * sizeof(GLfloat))); m_logoVbo.release(); } diff --git a/examples/opengl/hellogl2/glwidget.h b/examples/opengl/hellogl2/glwidget.h index 21dd200dc7..f8526fb7ae 100644 --- a/examples/opengl/hellogl2/glwidget.h +++ b/examples/opengl/hellogl2/glwidget.h @@ -65,7 +65,7 @@ class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions Q_OBJECT public: - GLWidget(QWidget *parent = 0); + GLWidget(QWidget *parent = nullptr); ~GLWidget(); static bool isTransparent() { return m_transparent; } @@ -96,18 +96,18 @@ private: void setupVertexAttribs(); bool m_core; - int m_xRot; - int m_yRot; - int m_zRot; + int m_xRot = 0; + int m_yRot = 0; + int m_zRot = 0; QPoint m_lastPos; Logo m_logo; QOpenGLVertexArrayObject m_vao; QOpenGLBuffer m_logoVbo; - QOpenGLShaderProgram *m_program; - int m_projMatrixLoc; - int m_mvMatrixLoc; - int m_normalMatrixLoc; - int m_lightPosLoc; + QOpenGLShaderProgram *m_program = nullptr; + int m_projMatrixLoc = 0; + int m_mvMatrixLoc = 0; + int m_normalMatrixLoc = 0; + int m_lightPosLoc = 0; QMatrix4x4 m_proj; QMatrix4x4 m_camera; QMatrix4x4 m_world; diff --git a/examples/opengl/hellogl2/logo.cpp b/examples/opengl/hellogl2/logo.cpp index a1ec8eaebe..6fcece16d4 100644 --- a/examples/opengl/hellogl2/logo.cpp +++ b/examples/opengl/hellogl2/logo.cpp @@ -52,7 +52,6 @@ #include Logo::Logo() - : m_count(0) { m_data.resize(2500 * 6); diff --git a/examples/opengl/hellogl2/logo.h b/examples/opengl/hellogl2/logo.h index 9e04a57e86..2f3dc7e649 100644 --- a/examples/opengl/hellogl2/logo.h +++ b/examples/opengl/hellogl2/logo.h @@ -69,7 +69,7 @@ private: void add(const QVector3D &v, const QVector3D &n); QVector m_data; - int m_count; + int m_count = 0; }; #endif // LOGO_H diff --git a/examples/opengl/hellogl2/mainwindow.cpp b/examples/opengl/hellogl2/mainwindow.cpp index 6bfdee7785..aa20cd678c 100644 --- a/examples/opengl/hellogl2/mainwindow.cpp +++ b/examples/opengl/hellogl2/mainwindow.cpp @@ -72,5 +72,6 @@ void MainWindow::onAddNew() if (!centralWidget()) setCentralWidget(new Window(this)); else - QMessageBox::information(0, tr("Cannot add new window"), tr("Already occupied. Undock first.")); + QMessageBox::information(nullptr, tr("Cannot add new window"), + tr("Already occupied. Undock first.")); } diff --git a/examples/opengl/hellogl2/window.cpp b/examples/opengl/hellogl2/window.cpp index c3cd10cbfd..5534f2edea 100644 --- a/examples/opengl/hellogl2/window.cpp +++ b/examples/opengl/hellogl2/window.cpp @@ -121,7 +121,7 @@ void Window::keyPressEvent(QKeyEvent *e) void Window::dockUndock() { if (parent()) { - setParent(0); + setParent(nullptr); setAttribute(Qt::WA_DeleteOnClose); move(QApplication::desktop()->width() / 2 - width() / 2, QApplication::desktop()->height() / 2 - height() / 2); @@ -134,10 +134,12 @@ void Window::dockUndock() dockBtn->setText(tr("Undock")); mainWindow->setCentralWidget(this); } else { - QMessageBox::information(0, tr("Cannot dock"), tr("Main window already closed")); + QMessageBox::information(nullptr, tr("Cannot dock"), + tr("Main window already closed")); } } else { - QMessageBox::information(0, tr("Cannot dock"), tr("Main window already occupied")); + QMessageBox::information(nullptr, tr("Cannot dock"), + tr("Main window already occupied")); } } } diff --git a/examples/opengl/hellogles3/glwindow.cpp b/examples/opengl/hellogles3/glwindow.cpp index 9458b74810..c644faa9a6 100644 --- a/examples/opengl/hellogles3/glwindow.cpp +++ b/examples/opengl/hellogles3/glwindow.cpp @@ -57,19 +57,10 @@ #include #include #include -#include #include #include GLWindow::GLWindow() - : m_texture(0), - m_program(0), - m_vbo(0), - m_vao(0), - m_target(0, 0, -1), - m_uniformsDirty(true), - m_r(0), - m_r2(0) { m_world.setToIdentity(); m_world.translate(0, 0, -1); @@ -197,18 +188,12 @@ void GLWindow::initializeGL() { QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); - if (m_texture) { - delete m_texture; - m_texture = 0; - } QImage img(":/qtlogo.png"); Q_ASSERT(!img.isNull()); + delete m_texture; m_texture = new QOpenGLTexture(img.scaled(32, 36).mirrored()); - if (m_program) { - delete m_program; - m_program = 0; - } + delete m_program; m_program = new QOpenGLShaderProgram; // Prepend the correct version directive to the sources. The rest is the // same, thanks to the common GLSL syntax. @@ -223,26 +208,21 @@ void GLWindow::initializeGL() m_lightPosLoc = m_program->uniformLocation("lightPos"); // Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL. - if (m_vao) { - delete m_vao; - m_vao = 0; - } + delete m_vao; m_vao = new QOpenGLVertexArrayObject; if (m_vao->create()) m_vao->bind(); - if (m_vbo) { - delete m_vbo; - m_vbo = 0; - } m_program->bind(); + delete m_vbo; m_vbo = new QOpenGLBuffer; m_vbo->create(); m_vbo->bind(); m_vbo->allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat)); f->glEnableVertexAttribArray(0); f->glEnableVertexAttribArray(1); - f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0); + f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), + nullptr); f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast(3 * sizeof(GLfloat))); m_vbo->release(); diff --git a/examples/opengl/hellogles3/glwindow.h b/examples/opengl/hellogles3/glwindow.h index fba997bee4..dfa0680714 100644 --- a/examples/opengl/hellogles3/glwindow.h +++ b/examples/opengl/hellogles3/glwindow.h @@ -90,23 +90,23 @@ public: private slots: void startSecondStage(); private: - QOpenGLTexture *m_texture; - QOpenGLShaderProgram *m_program; - QOpenGLBuffer *m_vbo; - QOpenGLVertexArrayObject *m_vao; + QOpenGLTexture *m_texture = nullptr; + QOpenGLShaderProgram *m_program = nullptr; + QOpenGLBuffer *m_vbo = nullptr; + QOpenGLVertexArrayObject *m_vao = nullptr; Logo m_logo; - int m_projMatrixLoc; - int m_camMatrixLoc; - int m_worldMatrixLoc; - int m_myMatrixLoc; - int m_lightPosLoc; + int m_projMatrixLoc = 0; + int m_camMatrixLoc = 0; + int m_worldMatrixLoc = 0; + int m_myMatrixLoc = 0; + int m_lightPosLoc = 0; QMatrix4x4 m_proj; QMatrix4x4 m_world; QVector3D m_eye; - QVector3D m_target; - bool m_uniformsDirty; - float m_r; - float m_r2; + QVector3D m_target = {0, 0, -1}; + bool m_uniformsDirty = true; + float m_r = 0; + float m_r2 = 0; }; #endif diff --git a/examples/opengl/qopenglwidget/bubble.cpp b/examples/opengl/qopenglwidget/bubble.cpp index dbaf460f6f..7e69aac3e7 100644 --- a/examples/opengl/qopenglwidget/bubble.cpp +++ b/examples/opengl/qopenglwidget/bubble.cpp @@ -57,15 +57,13 @@ Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) { innerColor = randomColor(); outerColor = randomColor(); - cache = 0; updateBrush(); } //! [0] void Bubble::updateCache() { - if (cache) - delete cache; + delete cache; cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32_Premultiplied); cache->fill(0x00000000); QPainter p(cache); @@ -80,8 +78,7 @@ void Bubble::updateCache() Bubble::~Bubble() { - if (cache) - delete cache; + delete cache; } void Bubble::updateBrush() diff --git a/examples/opengl/qopenglwidget/bubble.h b/examples/opengl/qopenglwidget/bubble.h index 7170803b40..833ea02288 100644 --- a/examples/opengl/qopenglwidget/bubble.h +++ b/examples/opengl/qopenglwidget/bubble.h @@ -80,7 +80,7 @@ private: qreal radius; QColor innerColor; QColor outerColor; - QImage *cache; + QImage *cache = nullptr; }; #endif diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index 5057291f12..89c8469662 100644 --- a/examples/opengl/qopenglwidget/glwidget.cpp +++ b/examples/opengl/qopenglwidget/glwidget.cpp @@ -68,14 +68,6 @@ const int bubbleNum = 8; GLWidget::GLWidget(MainWindow *mw, bool button, const QColor &background) : m_mainWindow(mw), - m_showBubbles(true), - m_qtLogo(true), - m_frames(0), - m_program1(0), - m_program2(0), - m_texture(0), - m_transparent(false), - m_btn(0), m_hasButton(button), m_background(background) { diff --git a/examples/opengl/qopenglwidget/glwidget.h b/examples/opengl/qopenglwidget/glwidget.h index 0ad2581cb8..99288261c0 100644 --- a/examples/opengl/qopenglwidget/glwidget.h +++ b/examples/opengl/qopenglwidget/glwidget.h @@ -98,34 +98,34 @@ private: void extrude(qreal x1, qreal y1, qreal x2, qreal y2); MainWindow *m_mainWindow; - qreal m_fAngle; - qreal m_fScale; - bool m_showBubbles; + qreal m_fAngle = 0; + qreal m_fScale = 1; + bool m_showBubbles = true; QVector m_vertices; QVector m_normals; - bool m_qtLogo; - QList m_bubbles; - int m_frames; + bool m_qtLogo = true; + QVector m_bubbles; + int m_frames = 0; QElapsedTimer m_time; - QOpenGLShader *m_vshader1; - QOpenGLShader *m_fshader1; - QOpenGLShader *m_vshader2; - QOpenGLShader *m_fshader2; - QOpenGLShaderProgram *m_program1; - QOpenGLShaderProgram *m_program2; - QOpenGLTexture *m_texture; + QOpenGLShader *m_vshader1 = nullptr; + QOpenGLShader *m_fshader1 = nullptr; + QOpenGLShader *m_vshader2 = nullptr; + QOpenGLShader *m_fshader2 = nullptr; + QOpenGLShaderProgram *m_program1 = nullptr; + QOpenGLShaderProgram *m_program2 = nullptr; + QOpenGLTexture *m_texture = nullptr; QOpenGLBuffer m_vbo1; QOpenGLBuffer m_vbo2; - int m_vertexAttr1; - int m_normalAttr1; - int m_matrixUniform1; - int m_vertexAttr2; - int m_normalAttr2; - int m_texCoordAttr2; - int m_matrixUniform2; - int m_textureUniform2; - bool m_transparent; - QPushButton *m_btn; + int m_vertexAttr1 = 0; + int m_normalAttr1 = 0; + int m_matrixUniform1 = 0; + int m_vertexAttr2 = 0; + int m_normalAttr2 = 0; + int m_texCoordAttr2 = 0; + int m_matrixUniform2 = 0; + int m_textureUniform2 = 0; + bool m_transparent = false; + QPushButton *m_btn = nullptr; bool m_hasButton; QColor m_background; }; diff --git a/examples/opengl/textures/glwidget.cpp b/examples/opengl/textures/glwidget.cpp index 1644233614..307190f308 100644 --- a/examples/opengl/textures/glwidget.cpp +++ b/examples/opengl/textures/glwidget.cpp @@ -53,17 +53,6 @@ #include #include -GLWidget::GLWidget(QWidget *parent) - : QOpenGLWidget(parent), - clearColor(Qt::black), - xRot(0), - yRot(0), - zRot(0), - program(0) -{ - memset(textures, 0, sizeof(textures)); -} - GLWidget::~GLWidget() { makeCurrent(); diff --git a/examples/opengl/textures/glwidget.h b/examples/opengl/textures/glwidget.h index 0db2695c6a..585d44dbfe 100644 --- a/examples/opengl/textures/glwidget.h +++ b/examples/opengl/textures/glwidget.h @@ -63,7 +63,7 @@ class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions Q_OBJECT public: - explicit GLWidget(QWidget *parent = 0); + using QOpenGLWidget::QOpenGLWidget; ~GLWidget(); QSize minimumSizeHint() const override; @@ -85,13 +85,13 @@ protected: private: void makeObject(); - QColor clearColor; + QColor clearColor = Qt::black; QPoint lastPos; - int xRot; - int yRot; - int zRot; - QOpenGLTexture *textures[6]; - QOpenGLShaderProgram *program; + int xRot = 0; + int yRot = 0; + int zRot = 0; + QOpenGLTexture *textures[6] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; + QOpenGLShaderProgram *program = nullptr; QOpenGLBuffer vbo; }; diff --git a/examples/opengl/threadedqopenglwidget/glwidget.cpp b/examples/opengl/threadedqopenglwidget/glwidget.cpp index cc528a734a..2101575fd4 100644 --- a/examples/opengl/threadedqopenglwidget/glwidget.cpp +++ b/examples/opengl/threadedqopenglwidget/glwidget.cpp @@ -115,12 +115,7 @@ void GLWidget::grabContext() m_renderer->unlockRenderer(); } -Renderer::Renderer(GLWidget *w) - : m_inited(false), - m_glwidget(w), - m_exiting(false) -{ -} +Renderer::Renderer(GLWidget *w) : m_glwidget(w) {} void Renderer::paintQtLogo() { diff --git a/examples/opengl/threadedqopenglwidget/glwidget.h b/examples/opengl/threadedqopenglwidget/glwidget.h index c33f7e51a7..8dc84dd0b1 100644 --- a/examples/opengl/threadedqopenglwidget/glwidget.h +++ b/examples/opengl/threadedqopenglwidget/glwidget.h @@ -88,29 +88,29 @@ private: void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4); void extrude(qreal x1, qreal y1, qreal x2, qreal y2); - bool m_inited; - qreal m_fAngle; - qreal m_fScale; + bool m_inited = false; + qreal m_fAngle = 0; + qreal m_fScale = 1; QVector vertices; QVector normals; QOpenGLShaderProgram program; QOpenGLBuffer vbo; - int vertexAttr; - int normalAttr; - int matrixUniform; - GLWidget *m_glwidget; + int vertexAttr = 0; + int normalAttr = 0; + int matrixUniform = 0; + GLWidget *m_glwidget = nullptr; QMutex m_renderMutex; QElapsedTimer m_elapsed; QMutex m_grabMutex; QWaitCondition m_grabCond; - bool m_exiting; + bool m_exiting = false; }; class GLWidget : public QOpenGLWidget { Q_OBJECT public: - explicit GLWidget(QWidget *parent = 0); + explicit GLWidget(QWidget *parent = nullptr); ~GLWidget(); protected: