Doc/QtCore: use new signal/slot signature in snippets

Use the new signal/slot syntax in the snippets where possible. Also
change some 0 to nullptr.

Change-Id: Ie3da2721d3cec33704f73f4d39c06a767717b095
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Christian Ehrlicher 2020-01-25 21:19:47 +01:00
parent aec4e05e9e
commit ae9056587a
15 changed files with 67 additions and 65 deletions

View File

@ -56,7 +56,7 @@ QApplication::sendEvent(mainWindow, &event);
//! [1] //! [1]
QPushButton *quitButton = new QPushButton("Quit"); QPushButton *quitButton = new QPushButton("Quit");
connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()), Qt::QueuedConnection); connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection);
//! [1] //! [1]
@ -79,12 +79,12 @@ Q_COREAPP_STARTUP_FUNCTION(preRoutineMyDebugTool)
//! [4] //! [4]
static int *global_ptr = 0; static int *global_ptr = nullptr;
static void cleanup_ptr() static void cleanup_ptr()
{ {
delete [] global_ptr; delete [] global_ptr;
global_ptr = 0; global_ptr = nullptr;
} }
void init_ptr() void init_ptr()
@ -125,9 +125,9 @@ private:
//! [6] //! [6]
static inline QString tr(const char *sourceText, static inline QString tr(const char *sourceText,
const char *comment = 0); const char *comment = nullptr);
static inline QString trUtf8(const char *sourceText, static inline QString trUtf8(const char *sourceText,
const char *comment = 0); const char *comment = nullptr);
//! [6] //! [6]

View File

@ -56,7 +56,7 @@ QState *s1 = new QState();
s1->assignProperty(&button, "text", "Click me"); s1->assignProperty(&button, "text", "Click me");
QFinalState *s2 = new QFinalState(); QFinalState *s2 = new QFinalState();
s1->addTransition(&button, SIGNAL(clicked()), s2); s1->addTransition(&button, &QPushButton::clicked, s2);
machine.addState(s1); machine.addState(s1);
machine.addState(s2); machine.addState(s2);

View File

@ -52,7 +52,7 @@
// Instantiate the objects and connect to the finished signal. // Instantiate the objects and connect to the finished signal.
MyClass myObject; MyClass myObject;
QFutureWatcher<int> watcher; QFutureWatcher<int> watcher;
connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished())); connect(&watcher, QFutureWatcher<int>::finished, &myObject, &MyClass::handleFinished);
// Start the computation. // Start the computation.
QFuture<int> future = QtConcurrent::run(...); QFuture<int> future = QtConcurrent::run(...);

View File

@ -56,10 +56,10 @@ progressBar->setRange(0, 100);
// Construct a 1-second timeline with a frame range of 0 - 100 // Construct a 1-second timeline with a frame range of 0 - 100
QTimeLine *timeLine = new QTimeLine(1000, this); QTimeLine *timeLine = new QTimeLine(1000, this);
timeLine->setFrameRange(0, 100); timeLine->setFrameRange(0, 100);
connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int))); connect(timeLine, &QTimeLine::frameChanged, progressBar, &QProgressBar::setValue);
// Clicking the push button will start the progress bar animation // Clicking the push button will start the progress bar animation
pushButton = new QPushButton(tr("Start animation"), this); pushButton = new QPushButton(tr("Start animation"), this);
connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start())); connect(pushButton, &QPushButton::clicked, timeLine, &QTimeLine::start);
... ...
//! [0] //! [0]

View File

@ -48,11 +48,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui>
#include <QtWidgets>
#include "buttonwidget.h" #include "buttonwidget.h"
#include <QtWidgets>
//! [0] //! [0]
ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent) ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -62,15 +61,16 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
QGridLayout *gridLayout = new QGridLayout; QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) { for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]); QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map())); connect(button, &QPushButton::clicked,
signalMapper, &QSignalMapper::map);
//! [0] //! [1] //! [0] //! [1]
signalMapper->setMapping(button, texts[i]); signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3); gridLayout->addWidget(button, i / 3, i % 3);
} }
connect(signalMapper, SIGNAL(mapped(QString)), connect(signalMapper, QOverload<const QString &>::of(&QSignalMapper::mapped),
//! [1] //! [2] //! [1] //! [2]
this, SIGNAL(clicked(QString))); this, &ButtonWidget::clicked);
setLayout(gridLayout); setLayout(gridLayout);
} }
@ -84,7 +84,7 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
for (int i = 0; i < texts.size(); ++i) { for (int i = 0; i < texts.size(); ++i) {
QString text = texts[i]; QString text = texts[i];
QPushButton *button = new QPushButton(text); QPushButton *button = new QPushButton(text);
connect(button, &QPushButton::clicked, [=] { clicked(text); }); connect(button, &QPushButton::clicked, [this, text] { clicked(text); });
gridLayout->addWidget(button, i / 3, i % 3); gridLayout->addWidget(button, i / 3, i % 3);
} }
setLayout(gridLayout); setLayout(gridLayout);

View File

@ -51,7 +51,7 @@
#ifndef BUTTONWIDGET_H #ifndef BUTTONWIDGET_H
#define BUTTONWIDGET_H #define BUTTONWIDGET_H
#include <qwidget.h> #include <QWidget>
class QSignalMapper; class QSignalMapper;
class QString; class QString;
@ -63,7 +63,7 @@ class ButtonWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
ButtonWidget(const QStringList &texts, QWidget *parent = 0); ButtonWidget(const QStringList &texts, QWidget *parent = nullptr);
signals: signals:
void clicked(const QString &text); void clicked(const QString &text);

View File

@ -68,10 +68,10 @@ MainWindow::MainWindow()
readSettings(); readSettings();
connect(textEdit->document(), SIGNAL(contentsChanged()), connect(textEdit->document(), &QTextEdit::contentsChanged,
this, SLOT(documentWasModified())); this, &QAction::documentWasModified);
setCurrentFile(""); setCurrentFile(QString());
setUnifiedTitleAndToolBarOnMac(true); setUnifiedTitleAndToolBarOnMac(true);
} }
//! [2] //! [2]
@ -95,7 +95,7 @@ void MainWindow::newFile()
{ {
if (maybeSave()) { if (maybeSave()) {
textEdit->clear(); textEdit->clear();
setCurrentFile(""); setCurrentFile(QString());
} }
} }
//! [6] //! [6]
@ -162,31 +162,31 @@ void MainWindow::createActions()
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcuts(QKeySequence::New); newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file")); newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
//! [19] //! [19]
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open); openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file")); openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open())); connect(openAct, &QAction::triggered, this, &MainWindow::open);
//! [18] //! [19] //! [18] //! [19]
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save); saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the document to disk")); saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); connect(saveAct, &QAction::triggered, this, &MainWindow::save);
saveAsAct = new QAction(tr("Save &As..."), this); saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs); saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->setStatusTip(tr("Save the document under a new name")); saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); connect(saveAsAct, &QAction::triggered, this, &MainWindow::saveAs);
//! [20] //! [20]
exitAct = new QAction(tr("E&xit"), this); exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit); exitAct->setShortcuts(QKeySequence::Quit);
//! [20] //! [20]
exitAct->setStatusTip(tr("Exit the application")); exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); connect(exitAct, &QAction::triggered, this, &MainWindow::close);
//! [21] //! [21]
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
@ -194,38 +194,38 @@ void MainWindow::createActions()
cutAct->setShortcuts(QKeySequence::Cut); cutAct->setShortcuts(QKeySequence::Cut);
cutAct->setStatusTip(tr("Cut the current selection's contents to the " cutAct->setStatusTip(tr("Cut the current selection's contents to the "
"clipboard")); "clipboard"));
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); connect(cutAct, &QAction::triggered, textEdit, &QTextEdit::cut);
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
copyAct->setShortcuts(QKeySequence::Copy); copyAct->setShortcuts(QKeySequence::Copy);
copyAct->setStatusTip(tr("Copy the current selection's contents to the " copyAct->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard")); "clipboard"));
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); connect(copyAct, &QAction::triggered, textEdit, &QTextEdit::copy);
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
pasteAct->setShortcuts(QKeySequence::Paste); pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection")); "selection"));
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); connect(pasteAct, &QAction::triggered, textEdit, &QTextEdit::paste);
aboutAct = new QAction(tr("&About"), this); aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box")); aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
//! [22] //! [22]
aboutQtAct = new QAction(tr("About &Qt"), this); aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
//! [22] //! [22]
//! [23] //! [23]
cutAct->setEnabled(false); cutAct->setEnabled(false);
//! [23] //! [24] //! [23] //! [24]
copyAct->setEnabled(false); copyAct->setEnabled(false);
connect(textEdit, SIGNAL(copyAvailable(bool)), connect(textEdit, &QTextEdit::copyAvailable,
cutAct, SLOT(setEnabled(bool))); cutAct, &QAction::setEnabled);
connect(textEdit, SIGNAL(copyAvailable(bool)), connect(textEdit, &QTextEdit::copyAvailable,
copyAct, SLOT(setEnabled(bool))); copyAct, &QAction::setEnabled);
} }
//! [24] //! [24]

View File

@ -48,13 +48,14 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
int main(int argv, char **args) int main(int argv, char **args)
{ {
QApplication app(argv, args); QApplication app(argv, args);
QLabel *label = new QLabel; QLabel *label = new QLabel;
QPushButton *button = new QPushButton;
//![0] //![0]
QStateMachine machine; QStateMachine machine;
@ -70,14 +71,14 @@ int main(int argv, char **args)
//![4] //![4]
//![5] //![5]
QObject::connect(s3, SIGNAL(entered()), button, SLOT(showMaximized())); QObject::connect(s3, &QState::entered, button, &QPushButton:showMaximized);
QObject::connect(s3, SIGNAL(exited()), button, SLOT(showMinimized())); QObject::connect(s3, &QState::exited, button, &QPushButton::showMinimized);
//![5] //![5]
//![1] //![1]
s1->addTransition(button, SIGNAL(clicked()), s2); s1->addTransition(button, &QPushButton::clicked, s2);
s2->addTransition(button, SIGNAL(clicked()), s3); s2->addTransition(button, &QPushButton::clicked, s3);
s3->addTransition(button, SIGNAL(clicked()), s1); s3->addTransition(button, &QPushButton::clicked, s1);
//![1] //![1]
//![2] //![2]

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
int main(int argv, char **args) int main(int argv, char **args)
{ {
@ -66,16 +66,17 @@ int main(int argv, char **args)
//![0] //![0]
//![2] //![2]
s12->addTransition(quitButton, SIGNAL(clicked()), s12); s12->addTransition(quitButton, &QPushButton::clicked, s12);
//![2] //![2]
//![1] //![1]
QFinalState *s2 = new QFinalState(); QFinalState *s2 = new QFinalState();
s1->addTransition(quitButton, SIGNAL(clicked()), s2); s1->addTransition(quitButton, &QPushButton::clicked, s2);
machine.addState(s2); machine.addState(s2);
machine.setInitialState(s1); machine.setInitialState(s1);
QObject::connect(&machine, SIGNAL(finished()), QApplication::instance(), SLOT(quit())); QObject::connect(&machine, &QStateMachine::finished,
QCoreApplication::instance(), &QCoreApplication::quit);
//![1] //![1]
QButton *interruptButton = new QPushButton("Interrupt Button"); QButton *interruptButton = new QPushButton("Interrupt Button");
@ -90,11 +91,11 @@ int main(int argv, char **args)
mbox->addButton(QMessageBox::Ok); mbox->addButton(QMessageBox::Ok);
mbox->setText("Interrupted!"); mbox->setText("Interrupted!");
mbox->setIcon(QMessageBox::Information); mbox->setIcon(QMessageBox::Information);
QObject::connect(s3, SIGNAL(entered()), mbox, SLOT(exec())); QObject::connect(s3, &QState::entered, mbox, &QMessageBox::exec);
s3->addTransition(s1h); s3->addTransition(s1h);
machine.addState(s3); machine.addState(s3);
s1->addTransition(interruptButton, SIGNAL(clicked()), s3); s1->addTransition(interruptButton, &QPushButton::clicked, s3);
//![3] //![3]
return app.exec(); return app.exec();

View File

@ -62,7 +62,7 @@ int main(int argv, char **args)
//![0] //![0]
//![1] //![1]
s1->addTransition(s1, SIGNAL(finished()), s2); s1->addTransition(s1, &QState::finished, s2);
//![1] //![1]
return app.exec(); return app.exec();

View File

@ -98,7 +98,7 @@ int main(int argv, char **args)
s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100)); s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100));
s1->addTransition(button, SIGNAL(clicked()), s2); s1->addTransition(button, &QPushButton::clicked, s2);
//![3] //![3]
} }
@ -111,7 +111,7 @@ int main(int argv, char **args)
s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100)); s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100));
QSignalTransition *transition = s1->addTransition(button, SIGNAL(clicked()), s2); QSignalTransition *transition = s1->addTransition(button, &QPushButton::clicked, s2);
transition->addAnimation(new QPropertyAnimation(button, "geometry")); transition->addAnimation(new QPropertyAnimation(button, "geometry"));
//![4] //![4]
@ -130,9 +130,9 @@ int main(int argv, char **args)
QState *s2 = new QState(); QState *s2 = new QState();
s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
connect(s2, SIGNAL(entered()), messageBox, SLOT(exec())); connect(s2, &QState::entered, messageBox, SLOT(exec()));
s1->addTransition(button, SIGNAL(clicked()), s2); s1->addTransition(button, &QPushButton::clicked, s2);
//![5] //![5]
} }
@ -151,10 +151,10 @@ int main(int argv, char **args)
s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); s2->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
QState *s3 = new QState(); QState *s3 = new QState();
connect(s3, SIGNAL(entered()), messageBox, SLOT(exec())); connect(s3, &QState::entered, messageBox, SLOT(exec()));
s1->addTransition(button, SIGNAL(clicked()), s2); s1->addTransition(button, &QPushButton::clicked, s2);
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); s2->addTransition(s2, &QState::propertiesAssigned, s3);
//![6] //![6]
} }

View File

@ -61,7 +61,7 @@ AnalogClock::AnalogClock(QWidget *parent)
//! [3] //! [4] //! [3] //! [4]
QTimer *timer = new QTimer(this); QTimer *timer = new QTimer(this);
//! [4] //! [5] //! [4] //! [5]
connect(timer, SIGNAL(timeout()), this, SLOT(update())); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
//! [5] //! [6] //! [5] //! [6]
timer->start(1000); timer->start(1000);
//! [6] //! [6]

View File

@ -61,13 +61,13 @@ Foo::Foo()
//! [0] //! [0]
QTimer *timer = new QTimer(this); QTimer *timer = new QTimer(this);
//! [0] //! [1] //! [0] //! [1]
connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption())); connect(timer, &QTimer::timeout, this, &Foo::updateCaption);
//! [1] //! [2] //! [1] //! [2]
timer->start(1000); timer->start(1000);
//! [2] //! [2]
//! [3] //! [3]
QTimer::singleShot(200, this, SLOT(updateCaption())); QTimer::singleShot(200, this, &Foo::updateCaption);
//! [3] //! [3]
{ {
@ -75,7 +75,7 @@ Foo::Foo()
//! [4] //! [4]
QTimer *timer = new QTimer(this); QTimer *timer = new QTimer(this);
//! [4] //! [5] //! [4] //! [5]
connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing())); connect(timer, &QTimer::timeout, this, &Foo::processOneThing);
//! [5] //! [6] //! [5] //! [6]
timer->start(); timer->start();
//! [6] //! [6]

View File

@ -356,11 +356,11 @@
state2->assignProperty(button, "geometry", QRect(250, 250, 100, 30)); state2->assignProperty(button, "geometry", QRect(250, 250, 100, 30));
QSignalTransition *transition1 = state1->addTransition(button, QSignalTransition *transition1 = state1->addTransition(button,
SIGNAL(clicked()), state2); &QPushButton::clicked, state2);
transition1->addAnimation(new QPropertyAnimation(button, "geometry")); transition1->addAnimation(new QPropertyAnimation(button, "geometry"));
QSignalTransition *transition2 = state2->addTransition(button, QSignalTransition *transition2 = state2->addTransition(button,
SIGNAL(clicked()), state1); &QPushButton::clicked, state1);
transition2->addAnimation(new QPropertyAnimation(button, "geometry")); transition2->addAnimation(new QPropertyAnimation(button, "geometry"));
machine->start(); machine->start();

View File

@ -323,12 +323,12 @@
QState *s1 = new QState(&machine); QState *s1 = new QState(&machine);
QPushButton button; QPushButton button;
QSignalTransition *trans = new QSignalTransition(&button, SIGNAL(clicked())); QSignalTransition *trans = new QSignalTransition(&button, &QPushButton::clicked);
s1->addTransition(trans); s1->addTransition(trans);
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("The button was clicked; carry on."); msgBox.setText("The button was clicked; carry on.");
QObject::connect(trans, SIGNAL(triggered()), &msgBox, SLOT(exec())); QObject::connect(trans, QSignalTransition::triggered, &msgBox, &QMessageBox::exec);
machine.setInitialState(s1); machine.setInitialState(s1);
\endcode \endcode