Cleanup Widgets examples - new signal/slot syntax

Cleanup the Widget examples - use the new signal/slot syntax where
possible - layout, statemachine, tools and touch subdirectory

Change-Id: I466b309b643ef7ffc27be7591fa10f4c75cfd3f8
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
bb10
Christian Ehrlicher 2018-12-07 14:17:38 +01:00 committed by Luca Beldi
parent 451ebdff82
commit 8c04aab896
13 changed files with 100 additions and 93 deletions

View File

@ -69,8 +69,8 @@ Dialog::Dialog()
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
//! [1]
//! [2]
@ -99,7 +99,7 @@ void Dialog::createMenu()
exitAction = fileMenu->addAction(tr("E&xit"));
menuBar->addMenu(fileMenu);
connect(exitAction, SIGNAL(triggered()), this, SLOT(accept()));
connect(exitAction, &QAction::triggered, this, &QDialog::accept);
}
//! [6]

View File

@ -100,7 +100,7 @@ class FactorialLoopTransition : public QSignalTransition
{
public:
FactorialLoopTransition(Factorial *fact)
: QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
: QSignalTransition(fact, &Factorial::xChanged), m_fact(fact)
{}
bool eventTest(QEvent *e) override
@ -130,7 +130,7 @@ class FactorialDoneTransition : public QSignalTransition
{
public:
FactorialDoneTransition(Factorial *fact)
: QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
: QSignalTransition(fact, &Factorial::xChanged), m_fact(fact)
{}
bool eventTest(QEvent *e) override

View File

@ -217,7 +217,8 @@ void Window::buildMachine()
//![5]
machine->setInitialState(inputState);
connect(machine, SIGNAL(finished()), qApp, SLOT(quit()));
connect(machine, &QStateMachine::finished,
qApp, &QApplication::quit);
machine->start();
}

View File

@ -132,11 +132,11 @@ QState *createLightState(LightWidget *light, int duration, QState *parent = 0)
timer->setInterval(duration);
timer->setSingleShot(true);
QState *timing = new QState(lightState);
QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
QObject::connect(timing, &QAbstractState::entered, light, &LightWidget::turnOn);
QObject::connect(timing, &QAbstractState::entered, timer, QOverload<>::of(&QTimer::start));
QObject::connect(timing, &QAbstractState::exited, light, &LightWidget::turnOff);
QFinalState *done = new QFinalState(lightState);
timing->addTransition(timer, SIGNAL(timeout()), done);
timing->addTransition(timer, &QTimer::timeout, done);
lightState->setInitialState(timing);
return lightState;
}
@ -159,14 +159,14 @@ public:
redGoingYellow->setObjectName("redGoingYellow");
QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000);
yellowGoingGreen->setObjectName("yellowGoingGreen");
redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen);
redGoingYellow->addTransition(redGoingYellow, &QState::finished, yellowGoingGreen);
QState *greenGoingYellow = createLightState(widget->greenLight(), 3000);
greenGoingYellow->setObjectName("greenGoingYellow");
yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow);
yellowGoingGreen->addTransition(yellowGoingGreen, &QState::finished, greenGoingYellow);
QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000);
yellowGoingRed->setObjectName("yellowGoingRed");
greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed);
yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow);
greenGoingYellow->addTransition(greenGoingYellow, &QState::finished, yellowGoingRed);
yellowGoingRed->addTransition(yellowGoingRed, &QState::finished, redGoingYellow);
machine->addState(redGoingYellow);
machine->addState(yellowGoingGreen);

View File

@ -69,8 +69,8 @@ int main(int argc, char **argv)
//! [1]
//! [2]
off->addTransition(&button, SIGNAL(clicked()), on);
on->addTransition(&button, SIGNAL(clicked()), off);
off->addTransition(&button, &QAbstractButton::clicked, on);
on->addTransition(&button, &QAbstractButton::clicked, off);
//! [2]
//! [3]

View File

@ -102,10 +102,14 @@ MainWindow::MainWindow(QWidget *parent)
contentsLabel = new QLabel;
contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel()));
connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int)));
connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int)));
connect(modelCombo, QOverload<int>::of(&QComboBox::activated),
this, &MainWindow::changeModel);
connect(modeCombo, QOverload<int>::of(&QComboBox::activated),
this, &MainWindow::changeMode);
connect(caseCombo, QOverload<int>::of(&QComboBox::activated),
this, &MainWindow::changeCase);
connect(maxVisibleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
this, &MainWindow::changeMaxVisible);
//! [2]
//! [3]
@ -136,9 +140,9 @@ void MainWindow::createMenu()
QAction *aboutAct = new QAction(tr("About"), this);
QAction *aboutQtAct = new QAction(tr("About Qt"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
QMenu* fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(exitAction);
@ -271,7 +275,7 @@ void MainWindow::changeModel()
changeCase(caseCombo->currentIndex());
completer->setWrapAround(wrapCheckBox->isChecked());
lineEdit->setCompleter(completer);
connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround);
}
//! [14]

View File

@ -79,9 +79,9 @@ void MainWindow::createMenu()
QAction *aboutAct = new QAction(tr("About"), this);
QAction *aboutQtAct = new QAction(tr("About Qt"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
QMenu* fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(exitAction);

View File

@ -88,8 +88,8 @@ void TextEdit::setCompleter(QCompleter *completer)
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseInsensitive);
QObject::connect(c, SIGNAL(activated(QString)),
this, SLOT(insertCompletion(QString)));
QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated),
this, &TextEdit::insertCompletion);
}
//! [2]

View File

@ -135,15 +135,15 @@ RegExpDialog::RegExpDialog(QWidget *parent)
}
setLayout(mainLayout);
connect(patternComboBox, SIGNAL(editTextChanged(QString)),
this, SLOT(refresh()));
connect(textComboBox, SIGNAL(editTextChanged(QString)),
this, SLOT(refresh()));
connect(caseSensitiveCheckBox, SIGNAL(toggled(bool)),
this, SLOT(refresh()));
connect(minimalCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh()));
connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(refresh()));
connect(patternComboBox, &QComboBox::editTextChanged,
this, &RegExpDialog::refresh);
connect(textComboBox, &QComboBox::editTextChanged,
this, &RegExpDialog::refresh);
connect(caseSensitiveCheckBox, &QAbstractButton::toggled,
this, &RegExpDialog::refresh);
connect(minimalCheckBox, &QAbstractButton::toggled, this, &RegExpDialog::refresh);
connect(syntaxComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &RegExpDialog::refresh);
patternComboBox->addItem(tr("[A-Za-z_]+([A-Za-z_0-9]*)"));
textComboBox->addItem(tr("(10 + delta4) * 32"));

View File

@ -61,8 +61,8 @@ MainWindow::MainWindow(QWidget *parent)
completer = new TreeModelCompleter(this);
completer->setModel(modelFromFile(":/resources/treemodel.txt"));
completer->setSeparator(QLatin1String("."));
QObject::connect(completer, SIGNAL(highlighted(QModelIndex)),
this, SLOT(highlight(QModelIndex)));
QObject::connect(completer, QOverload<const QModelIndex &>::of(&TreeModelCompleter::highlighted),
this, &MainWindow::highlight);
QWidget *centralWidget = new QWidget;
@ -91,18 +91,18 @@ MainWindow::MainWindow(QWidget *parent)
QLineEdit *separatorLineEdit = new QLineEdit;
separatorLineEdit->setText(completer->separator());
connect(separatorLineEdit, SIGNAL(textChanged(QString)),
completer, SLOT(setSeparator(QString)));
connect(separatorLineEdit, &QLineEdit::textChanged,
completer, &TreeModelCompleter::setSeparator);
QCheckBox *wrapCheckBox = new QCheckBox;
wrapCheckBox->setText(tr("Wrap around completions"));
wrapCheckBox->setChecked(completer->wrapAround());
connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool)));
connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround);
contentsLabel = new QLabel;
contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(separatorLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(updateContentsLabel(QString)));
connect(separatorLineEdit, &QLineEdit::textChanged,
this, &MainWindow::updateContentsLabel);
treeView = new QTreeView;
treeView->setModel(completer->model());
@ -111,8 +111,10 @@ MainWindow::MainWindow(QWidget *parent)
//! [1]
//! [2]
connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int)));
connect(modeCombo, QOverload<int>::of(&QComboBox::activated),
this, &MainWindow::changeMode);
connect(caseCombo, QOverload<int>::of(&QComboBox::activated),
this, &MainWindow::changeMode);
lineEdit = new QLineEdit;
lineEdit->setCompleter(completer);
@ -145,9 +147,9 @@ void MainWindow::createMenu()
QAction *aboutAct = new QAction(tr("About"), this);
QAction *aboutQtAct = new QAction(tr("About Qt"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
QMenu* fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(exitAction);

View File

@ -68,25 +68,25 @@ MainWindow::MainWindow(QWidget *parent)
documentTabs->removeTab(0);
delete w;
connect(actionOpen, SIGNAL(triggered()), this, SLOT(openDocument()));
connect(actionClose, SIGNAL(triggered()), this, SLOT(closeDocument()));
connect(actionNew, SIGNAL(triggered()), this, SLOT(newDocument()));
connect(actionSave, SIGNAL(triggered()), this, SLOT(saveDocument()));
connect(actionExit, SIGNAL(triggered()), this, SLOT(close()));
connect(actionRed, SIGNAL(triggered()), this, SLOT(setShapeColor()));
connect(actionGreen, SIGNAL(triggered()), this, SLOT(setShapeColor()));
connect(actionBlue, SIGNAL(triggered()), this, SLOT(setShapeColor()));
connect(actionAddCircle, SIGNAL(triggered()), this, SLOT(addShape()));
connect(actionAddRectangle, SIGNAL(triggered()), this, SLOT(addShape()));
connect(actionAddTriangle, SIGNAL(triggered()), this, SLOT(addShape()));
connect(actionRemoveShape, SIGNAL(triggered()), this, SLOT(removeShape()));
connect(actionAddRobot, SIGNAL(triggered()), this, SLOT(addRobot()));
connect(actionAddSnowman, SIGNAL(triggered()), this, SLOT(addSnowman()));
connect(actionAbout, SIGNAL(triggered()), this, SLOT(about()));
connect(actionAboutQt, SIGNAL(triggered()), this, SLOT(aboutQt()));
connect(actionOpen, &QAction::triggered, this, &MainWindow::openDocument);
connect(actionClose, &QAction::triggered, this, &MainWindow::closeDocument);
connect(actionNew, &QAction::triggered, this, &MainWindow::newDocument);
connect(actionSave, &QAction::triggered, this, &MainWindow::saveDocument);
connect(actionExit, &QAction::triggered, this, &QWidget::close);
connect(actionRed, &QAction::triggered, this, &MainWindow::setShapeColor);
connect(actionGreen, &QAction::triggered, this, &MainWindow::setShapeColor);
connect(actionBlue, &QAction::triggered, this, &MainWindow::setShapeColor);
connect(actionAddCircle, &QAction::triggered, this, &MainWindow::addShape);
connect(actionAddRectangle, &QAction::triggered, this, &MainWindow::addShape);
connect(actionAddTriangle, &QAction::triggered, this, &MainWindow::addShape);
connect(actionRemoveShape, &QAction::triggered, this, &MainWindow::removeShape);
connect(actionAddRobot, &QAction::triggered, this, &MainWindow::addRobot);
connect(actionAddSnowman, &QAction::triggered, this, &MainWindow::addSnowman);
connect(actionAbout, &QAction::triggered, this, &MainWindow::about);
connect(actionAboutQt, &QAction::triggered, this, &MainWindow::aboutQt);
connect(undoLimit, SIGNAL(valueChanged(int)), this, SLOT(updateActions()));
connect(documentTabs, SIGNAL(currentChanged(int)), this, SLOT(updateActions()));
connect(undoLimit, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::updateActions);
connect(documentTabs, &QTabWidget::currentChanged, this, &MainWindow::updateActions);
actionOpen->setShortcut(QString("Ctrl+O"));
actionClose->setShortcut(QString("Ctrl+W"));
@ -226,9 +226,9 @@ void MainWindow::addDocument(Document *doc)
return;
m_undoGroup->addStack(doc->undoStack());
documentTabs->addTab(doc, fixedWindowTitle(doc));
connect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions()));
connect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions()));
connect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions()));
connect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions);
connect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions);
connect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions);
setCurrentDocument(doc);
}
@ -251,9 +251,9 @@ void MainWindow::removeDocument(Document *doc)
documentTabs->removeTab(index);
m_undoGroup->removeStack(doc->undoStack());
disconnect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions()));
disconnect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions()));
disconnect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions()));
disconnect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions);
disconnect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions);
disconnect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions);
if (documentTabs->count() == 0) {
newDocument();

View File

@ -70,8 +70,8 @@ MainWindow::MainWindow()
diagramScene->setBackgroundBrush(pixmapBrush);
diagramScene->setSceneRect(QRect(0, 0, 500, 500));
connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)),
this, SLOT(itemMoved(DiagramItem*,QPointF)));
connect(diagramScene, &DiagramScene::itemMoved,
this, &MainWindow::itemMoved);
setWindowTitle("Undo Framework");
QGraphicsView *view = new QGraphicsView(diagramScene);
@ -95,18 +95,18 @@ void MainWindow::createActions()
{
deleteAction = new QAction(tr("&Delete Item"), this);
deleteAction->setShortcut(tr("Del"));
connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem);
//! [2] //! [3]
//! [3] //! [4]
addBoxAction = new QAction(tr("Add &Box"), this);
//! [4]
addBoxAction->setShortcut(tr("Ctrl+O"));
connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));
connect(addBoxAction, &QAction::triggered, this, &MainWindow::addBox);
addTriangleAction = new QAction(tr("Add &Triangle"), this);
addTriangleAction->setShortcut(tr("Ctrl+T"));
connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
connect(addTriangleAction, &QAction::triggered, this, &MainWindow::addTriangle);
//! [5]
undoAction = undoStack->createUndoAction(this, tr("&Undo"));
@ -118,13 +118,13 @@ void MainWindow::createActions()
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(exitAction, &QAction::triggered, this, &QWidget::close);
aboutAction = new QAction(tr("&About"), this);
QList<QKeySequence> aboutShortcuts;
aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
aboutAction->setShortcuts(aboutShortcuts);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutAction, &QAction::triggered, this, &MainWindow::about);
}
//! [6]
@ -140,10 +140,10 @@ void MainWindow::createMenus()
editMenu->addAction(redoAction);
editMenu->addSeparator();
editMenu->addAction(deleteAction);
connect(editMenu, SIGNAL(aboutToShow()),
this, SLOT(itemMenuAboutToShow()));
connect(editMenu, SIGNAL(aboutToHide()),
this, SLOT(itemMenuAboutToHide()));
connect(editMenu, &QMenu::aboutToShow,
this, &MainWindow::itemMenuAboutToShow);
connect(editMenu, &QMenu::aboutToHide,
this, &MainWindow::itemMenuAboutToHide);
//! [7]
itemMenu = menuBar()->addMenu(tr("&Item"));

View File

@ -127,34 +127,34 @@ void MainWindow::createActions()
{
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
connect(openAct, &QAction::triggered, this, &MainWindow::open);
foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
QString text = tr("%1...").arg(QString(format).toUpper());
QAction *action = new QAction(text, this);
action->setData(format);
connect(action, SIGNAL(triggered()), this, SLOT(save()));
connect(action, &QAction::triggered, this, &MainWindow::save);
saveAsActs.append(action);
}
printAct = new QAction(tr("&Print..."), this);
connect(printAct, SIGNAL(triggered()), scribbleArea, SLOT(print()));
connect(printAct, &QAction::triggered, scribbleArea, &ScribbleArea::print);
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
connect(exitAct, &QAction::triggered, this, &QWidget::close);
clearScreenAct = new QAction(tr("&Clear Screen"), this);
clearScreenAct->setShortcut(tr("Ctrl+L"));
connect(clearScreenAct, SIGNAL(triggered()),
scribbleArea, SLOT(clearImage()));
connect(clearScreenAct, &QAction::triggered,
scribbleArea, &ScribbleArea::clearImage);
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
aboutQtAct = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
}
//! [14]