Cleanup QtCore examples

Cleanup QtCore examples:
 - use new signal/slot syntax
 - use 0 instead nullptr
 - remove unneeded includes
 - use initializer lists
 - replace foreach with range-based-for loop

Change-Id: I5581f485fa0e9ccf3f4ce6f643908832bc6959bb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Christian Ehrlicher 2019-02-03 17:09:10 +01:00 committed by Liang Qi
parent 7910dd0a54
commit 4715ca7bc5
29 changed files with 59 additions and 77 deletions

View File

@ -53,14 +53,12 @@
#include <QDialog> #include <QDialog>
#include <QDataStream> #include <QDataStream>
#include <QLocalSocket>
#include <qlocalsocket.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QLabel; class QLabel;
class QLineEdit; class QLineEdit;
class QPushButton; class QPushButton;
class QLocalSocket;
QT_END_NAMESPACE QT_END_NAMESPACE
class Client : public QDialog class Client : public QDialog

View File

@ -49,9 +49,6 @@
****************************************************************************/ ****************************************************************************/
#include <QApplication> #include <QApplication>
#include <QtCore>
#include <stdlib.h>
#include "server.h" #include "server.h"

View File

@ -48,15 +48,11 @@
** **
****************************************************************************/ ****************************************************************************/
#include "server.h"
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include <stdlib.h>
#include "server.h"
#include <qlocalserver.h>
#include <qlocalsocket.h>
Server::Server(QWidget *parent) Server::Server(QWidget *parent)
: QDialog(parent) : QDialog(parent)
{ {

View File

@ -51,7 +51,6 @@
#include "dialog.h" #include "dialog.h"
#include <QFileDialog> #include <QFileDialog>
#include <QBuffer> #include <QBuffer>
#include <QtCore/QDebug>
/*! /*!
\class Dialog \class Dialog
@ -81,10 +80,10 @@ Dialog::Dialog(QWidget *parent)
: QDialog(parent), sharedMemory("QSharedMemoryExample") : QDialog(parent), sharedMemory("QSharedMemoryExample")
{ {
ui.setupUi(this); ui.setupUi(this);
connect(ui.loadFromFileButton, SIGNAL(clicked()), SLOT(loadFromFile())); connect(ui.loadFromFileButton, &QPushButton::clicked,
connect(ui.loadFromSharedMemoryButton, this, &Dialog::loadFromFile);
SIGNAL(clicked()), connect(ui.loadFromSharedMemoryButton, &QPushButton::clicked,
SLOT(loadFromMemory())); this, &Dialog::loadFromMemory);
setWindowTitle(tr("SharedMemory Example")); setWindowTitle(tr("SharedMemory Example"));
} }
//! [0] //! [0]

View File

@ -51,8 +51,8 @@
#ifndef DIALOG_H #ifndef DIALOG_H
#define DIALOG_H #define DIALOG_H
#include <qdialog.h> #include <QDialog>
#include <qsharedmemory.h> #include <QSharedMemory>
#include "ui_dialog.h" #include "ui_dialog.h"
//! [0] //! [0]
@ -61,7 +61,7 @@ class Dialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
Dialog(QWidget *parent = 0); Dialog(QWidget *parent = nullptr);
public slots: public slots:
void loadFromFile(); void loadFromFile();

View File

@ -55,9 +55,6 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCommandLineOption> #include <QCommandLineOption>
#include <QDebug>
#include <QMimeDatabase>
#include <QMimeType>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@ -50,9 +50,8 @@
#include "mimetypemodel.h" #include "mimetypemodel.h"
#include <QIcon>
#include <QDebug> #include <QDebug>
#include <QIcon>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QTextStream> #include <QTextStream>
#include <QVariant> #include <QVariant>

View File

@ -56,7 +56,6 @@
#include <QCborArray> #include <QCborArray>
#include <QCborValue> #include <QCborValue>
#include <QDataStream> #include <QDataStream>
#include <QDebug>
#include <QFloat16> #include <QFloat16>
#include <QFile> #include <QFile>
#include <QMetaType> #include <QMetaType>

View File

@ -185,7 +185,7 @@ void Game::write(QJsonObject &json) const
json["player"] = playerObject; json["player"] = playerObject;
QJsonArray levelArray; QJsonArray levelArray;
for (const Level level : mLevels) { for (const Level &level : mLevels) {
QJsonObject levelObject; QJsonObject levelObject;
level.write(levelObject); level.write(levelObject);
levelArray.append(levelObject); levelArray.append(levelObject);

View File

@ -97,7 +97,7 @@ void Level::write(QJsonObject &json) const
{ {
json["name"] = mName; json["name"] = mName;
QJsonArray npcArray; QJsonArray npcArray;
for (const Character npc : mNpcs) { for (const Character &npc : mNpcs) {
QJsonObject npcObject; QJsonObject npcObject;
npc.write(npcObject); npc.write(npcObject);
npcArray.append(npcObject); npcArray.append(npcObject);

View File

@ -48,14 +48,13 @@
** **
****************************************************************************/ ****************************************************************************/
#include "mandelbrotwidget.h"
#include <QPainter> #include <QPainter>
#include <QKeyEvent> #include <QKeyEvent>
#include <math.h> #include <math.h>
#include "mandelbrotwidget.h"
//! [0] //! [0]
const double DefaultCenterX = -0.637011f; const double DefaultCenterX = -0.637011f;
const double DefaultCenterY = -0.0395159f; const double DefaultCenterY = -0.0395159f;
@ -75,7 +74,8 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent)
pixmapScale = DefaultScale; pixmapScale = DefaultScale;
curScale = DefaultScale; curScale = DefaultScale;
connect(&thread, SIGNAL(renderedImage(QImage,double)), this, SLOT(updatePixmap(QImage,double))); connect(&thread, &RenderThread::renderedImage,
this, &MandelbrotWidget::updatePixmap);
setWindowTitle(tr("Mandelbrot")); setWindowTitle(tr("Mandelbrot"));
#ifndef QT_NO_CURSOR #ifndef QT_NO_CURSOR

View File

@ -62,7 +62,7 @@ class MandelbrotWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
MandelbrotWidget(QWidget *parent = 0); MandelbrotWidget(QWidget *parent = nullptr);
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;

View File

@ -50,7 +50,7 @@
#include "renderthread.h" #include "renderthread.h"
#include <QtWidgets> #include <QImage>
#include <cmath> #include <cmath>
//! [0] //! [0]

View File

@ -66,7 +66,7 @@ class RenderThread : public QThread
Q_OBJECT Q_OBJECT
public: public:
RenderThread(QObject *parent = 0); RenderThread(QObject *parent = nullptr);
~RenderThread(); ~RenderThread();
void render(double centerX, double centerY, double scaleFactor, QSize resultSize); void render(double centerX, double centerY, double scaleFactor, QSize resultSize);

View File

@ -48,8 +48,6 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QColor>
#include <QRect>
#include "block.h" #include "block.h"
Block::Block() Block::Block()
@ -57,9 +55,8 @@ Block::Block()
} }
Block::Block(const Block &other) Block::Block(const Block &other)
: m_rect(other.m_rect), m_color(other.m_color)
{ {
m_rect = other.m_rect;
m_color = other.m_color;
} }
Block::~Block() Block::~Block()
@ -67,9 +64,8 @@ Block::~Block()
} }
Block::Block(const QRect &rect, const QColor &color) Block::Block(const QRect &rect, const QColor &color)
: m_rect(rect), m_color(color)
{ {
m_rect = rect;
m_color = color;
} }
QColor Block::color() const QColor Block::color() const

View File

@ -52,7 +52,6 @@
#define BLOCK_H #define BLOCK_H
#include <QColor> #include <QColor>
#include <QDebug>
#include <QMetaType> #include <QMetaType>
#include <QRect> #include <QRect>

View File

@ -62,7 +62,7 @@ class RenderThread : public QThread
Q_OBJECT Q_OBJECT
public: public:
RenderThread(QObject *parent = 0); RenderThread(QObject *parent = nullptr);
~RenderThread(); ~RenderThread();
void processImage(const QImage &image); void processImage(const QImage &image);

View File

@ -48,30 +48,34 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "window.h" #include "window.h"
#include <QtWidgets>
//! [Window constructor start] //! [Window constructor start]
Window::Window() Window::Window(QWidget *parent)
: QWidget(parent), thread(new RenderThread(this))
{ {
thread = new RenderThread();
//! [Window constructor start] //! [set up widgets and connections] //! [Window constructor start] //! [set up widgets and connections]
label = new QLabel(); label = new QLabel(this);
label->setAlignment(Qt::AlignCenter); label->setAlignment(Qt::AlignCenter);
loadButton = new QPushButton(tr("&Load image...")); loadButton = new QPushButton(tr("&Load image..."), this);
resetButton = new QPushButton(tr("&Stop")); resetButton = new QPushButton(tr("&Stop"), this);
resetButton->setEnabled(false); resetButton->setEnabled(false);
connect(loadButton, SIGNAL(clicked()), this, SLOT(loadImage())); connect(loadButton, &QPushButton::clicked,
connect(resetButton, SIGNAL(clicked()), thread, SLOT(stopProcess())); this, QOverload<>::of(&Window::loadImage));
connect(thread, SIGNAL(finished()), this, SLOT(resetUi())); connect(resetButton, &QPushButton::clicked,
thread, &RenderThread::stopProcess);
connect(thread, &RenderThread::finished,
this, &Window::resetUi);
//! [set up widgets and connections] //! [connecting signal with custom type] //! [set up widgets and connections] //! [connecting signal with custom type]
connect(thread, SIGNAL(sendBlock(Block)), this, SLOT(addBlock(Block))); connect(thread, &RenderThread::sendBlock,
this, &Window::addBlock);
//! [connecting signal with custom type] //! [connecting signal with custom type]
QHBoxLayout *buttonLayout = new QHBoxLayout(); QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(); buttonLayout->addStretch();
buttonLayout->addWidget(loadButton); buttonLayout->addWidget(loadButton);
buttonLayout->addWidget(resetButton); buttonLayout->addWidget(resetButton);

View File

@ -65,7 +65,7 @@ class Window : public QWidget
Q_OBJECT Q_OBJECT
public: public:
Window(); Window(QWidget *parent = nullptr);
void loadImage(const QImage &image); void loadImage(const QImage &image);
public slots: public slots:

View File

@ -49,7 +49,6 @@
****************************************************************************/ ****************************************************************************/
#include "randomlistmodel.h" #include "randomlistmodel.h"
#include <QRandomGenerator> #include <QRandomGenerator>
#include <stdlib.h>
static const int bufferSize(500); static const int bufferSize(500);
static const int lookAhead(100); static const int lookAhead(100);

View File

@ -59,7 +59,7 @@ class RandomListModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
RandomListModel(QObject *parent = 0); RandomListModel(QObject *parent = nullptr);
~RandomListModel(); ~RandomListModel();
int rowCount(const QModelIndex & = QModelIndex()) const override; int rowCount(const QModelIndex & = QModelIndex()) const override;

View File

@ -49,6 +49,7 @@
****************************************************************************/ ****************************************************************************/
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug>
#include <QVariant> #include <QVariant>
#include "message.h" #include "message.h"

View File

@ -50,15 +50,16 @@
#include "message.h" #include "message.h"
#include <QDebug>
//! [Message class implementation] //! [Message class implementation]
Message::Message() Message::Message()
{ {
} }
Message::Message(const Message &other) Message::Message(const Message &other)
: m_body(other.m_body), m_headers(other.m_headers)
{ {
m_body = other.m_body;
m_headers = other.m_headers;
} }
Message::~Message() Message::~Message()
@ -67,9 +68,8 @@ Message::~Message()
//! [Message class implementation] //! [Message class implementation]
Message::Message(const QString &body, const QStringList &headers) Message::Message(const QString &body, const QStringList &headers)
: m_body(body), m_headers(headers)
{ {
m_body = body;
m_headers = headers;
} }
//! [custom type streaming operator] //! [custom type streaming operator]

View File

@ -51,7 +51,6 @@
#ifndef MESSAGE_H #ifndef MESSAGE_H
#define MESSAGE_H #define MESSAGE_H
#include <QDebug>
#include <QMetaType> #include <QMetaType>
#include <QStringList> #include <QStringList>

View File

@ -57,19 +57,20 @@ int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
Window window1;
QStringList headers; QStringList headers;
headers << "Subject: Hello World" headers << "Subject: Hello World"
<< "From: address@example.com"; << "From: address@example.com";
QString body = "This is a test.\r\n"; QString body = "This is a test.\r\n";
Message message(body, headers); Message message(body, headers);
Window window1;
window1.setMessage(message); window1.setMessage(message);
Window window2; Window window2;
QObject::connect(&window1, SIGNAL(messageSent(Message)), QObject::connect(&window1, &Window::messageSent,
&window2, SLOT(setMessage(Message))); &window2, &Window::setMessage);
QObject::connect(&window2, SIGNAL(messageSent(Message)), QObject::connect(&window2, &Window::messageSent,
&window1, SLOT(setMessage(Message))); &window1, &Window::setMessage);
window1.show(); window1.show();
window2.show(); window2.show();
return app.exec(); return app.exec();

View File

@ -55,9 +55,8 @@ Message::Message()
} }
Message::Message(const Message &other) Message::Message(const Message &other)
: m_body(other.m_body), m_headers(other.m_headers)
{ {
m_body = other.m_body;
m_headers = other.m_headers;
} }
Message::~Message() Message::~Message()
@ -65,9 +64,8 @@ Message::~Message()
} }
Message::Message(const QString &body, const QStringList &headers) Message::Message(const QString &body, const QStringList &headers)
: m_body(body), m_headers(headers)
{ {
m_body = body;
m_headers = headers;
} }
QString Message::body() const QString Message::body() const

View File

@ -51,7 +51,6 @@
#ifndef MESSAGE_H #ifndef MESSAGE_H
#define MESSAGE_H #define MESSAGE_H
#include <QDebug>
#include <QMetaType> #include <QMetaType>
#include <QStringList> #include <QStringList>

View File

@ -52,14 +52,15 @@
#include "window.h" #include "window.h"
//! [Window constructor] //! [Window constructor]
Window::Window() Window::Window(QWidget *parent)
: QWidget(parent), editor(new QTextEdit(this))
{ {
editor = new QTextEdit();
QPushButton *sendButton = new QPushButton(tr("&Send message")); QPushButton *sendButton = new QPushButton(tr("&Send message"));
connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMessage())); connect(sendButton, &QPushButton::clicked,
this, &Window::sendMessage);
QHBoxLayout *buttonLayout = new QHBoxLayout(); QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(); buttonLayout->addStretch();
buttonLayout->addWidget(sendButton); buttonLayout->addWidget(sendButton);
buttonLayout->addStretch(); buttonLayout->addStretch();

View File

@ -62,7 +62,7 @@ class Window : public QWidget
Q_OBJECT Q_OBJECT
public: public:
Window(); Window(QWidget *parent = nullptr);
signals: signals:
void messageSent(const Message &message); void messageSent(const Message &message);