examples: Use QOverload to select overloaded signals and slots

We can use QOverload since Qt 5.7 (it depends on Q_COMPILER_VARIADIC_TEMPLATES
which is required since Qt 5.7).
Use it in the examples to show the best practice.
qOverload currently can't be used because it requires c++14.

Change-Id: I94a3c0db9d551fe169fa3d19c07ec0b329d5946c
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Alexander Volkov 2016-12-20 21:37:37 +03:00
parent 63e7ff97e9
commit fb7bfbf18d
15 changed files with 28 additions and 52 deletions

View File

@ -122,8 +122,7 @@ Client::Client(QWidget *parent)
//! [2] //! [3] //! [2] //! [3]
connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune); connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
//! [2] //! [4] //! [2] //! [4]
typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError); connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
connect(tcpSocket, static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error),
//! [3] //! [3]
this, &Client::displayError); this, &Client::displayError);
//! [4] //! [4]

View File

@ -95,9 +95,7 @@ PaintedWindow::PaintedWindow()
connect(screen(), &QScreen::orientationChanged, this, &PaintedWindow::orientationChanged); connect(screen(), &QScreen::orientationChanged, this, &PaintedWindow::orientationChanged);
connect(m_animation, &QAbstractAnimation::finished, this, &PaintedWindow::rotationDone); connect(m_animation, &QAbstractAnimation::finished, this, &PaintedWindow::rotationDone);
typedef void (PaintedWindow::*PaintedWindowVoidSlot)(); connect(this, &PaintedWindow::rotationChanged, this, QOverload<>::of(&PaintedWindow::paint));
connect(this, &PaintedWindow::rotationChanged,
this, static_cast<PaintedWindowVoidSlot>(&PaintedWindow::paint));
} }
void PaintedWindow::exposeEvent(QExposeEvent *) void PaintedWindow::exposeEvent(QExposeEvent *)

View File

@ -61,8 +61,6 @@
#include "glwidget.h" #include "glwidget.h"
typedef void (QWidget::*QWidgetVoidSlot)();
MainWindow::MainWindow() MainWindow::MainWindow()
: m_nextX(1), m_nextY(1) : m_nextX(1), m_nextY(1)
{ {
@ -131,14 +129,11 @@ MainWindow::MainWindow()
QMenu *helpMenu = menuBar()->addMenu("&Help"); QMenu *helpMenu = menuBar()->addMenu("&Help");
helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt); helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
connect(m_timer, &QTimer::timeout, connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
glwidget, static_cast<QWidgetVoidSlot>(&QWidget::update));
connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling); connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent); connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
connect(updateInterval, QOverload<int>::of(&QSpinBox::valueChanged),
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
connect(updateInterval, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
this, &MainWindow::updateIntervalChanged); this, &MainWindow::updateIntervalChanged);
connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged); connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled); connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
@ -162,7 +157,7 @@ void MainWindow::addNew()
return; return;
GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256)); GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256));
m_glWidgets << w; m_glWidgets << w;
connect(m_timer, &QTimer::timeout, w, static_cast<QWidgetVoidSlot>(&QWidget::update)); connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
m_layout->addWidget(w, m_nextY, m_nextX, 1, 1); m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
if (m_nextX == 3) { if (m_nextX == 3) {
m_nextX = 1; m_nextX = 1;

View File

@ -173,8 +173,6 @@ void OpenGLWindow::keyPressEvent(QKeyEvent *e)
void OpenGLWindow::setAnimating(bool enabled) void OpenGLWindow::setAnimating(bool enabled)
{ {
typedef void (QPaintDeviceWindow::*QPaintDeviceWindowVoidSlot)();
if (enabled) { if (enabled) {
// Animate continuously, throttled by the blocking swapBuffers() call the // Animate continuously, throttled by the blocking swapBuffers() call the
// QOpenGLWindow internally executes after each paint. Once that is done // QOpenGLWindow internally executes after each paint. Once that is done
@ -182,11 +180,11 @@ void OpenGLWindow::setAnimating(bool enabled)
// obviously assumes that the swap interval (see // obviously assumes that the swap interval (see
// QSurfaceFormat::setSwapInterval()) is non-zero. // QSurfaceFormat::setSwapInterval()) is non-zero.
connect(this, &QOpenGLWindow::frameSwapped, connect(this, &QOpenGLWindow::frameSwapped,
this, static_cast<QPaintDeviceWindowVoidSlot>(&QPaintDeviceWindow::update)); this, QOverload<>::of(&QPaintDeviceWindow::update));
update(); update();
} else { } else {
disconnect(this, &QOpenGLWindow::frameSwapped, disconnect(this, &QOpenGLWindow::frameSwapped,
this, static_cast<QPaintDeviceWindowVoidSlot>(&QPaintDeviceWindow::update)); this, QOverload<>::of(&QPaintDeviceWindow::update));
} }
} }

View File

@ -70,8 +70,7 @@ Screenshot::Screenshot()
delaySpinBox->setSuffix(tr(" s")); delaySpinBox->setSuffix(tr(" s"));
delaySpinBox->setMaximum(60); delaySpinBox->setMaximum(60);
typedef void (QSpinBox::*QSpinBoxIntSignal)(int); connect(delaySpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
connect(delaySpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
this, &Screenshot::updateCheckBox); this, &Screenshot::updateCheckBox);
hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox); hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox);

View File

@ -80,8 +80,7 @@ Window::Window()
connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage); connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage);
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible); connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
typedef void (QComboBox::*QComboIntSignal)(int); connect(iconComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
connect(iconComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged),
this, &Window::setIcon); this, &Window::setIcon);
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked); connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated); connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);

View File

@ -89,11 +89,9 @@ Window::Window()
connect(filterPatternLineEdit, &QLineEdit::textChanged, connect(filterPatternLineEdit, &QLineEdit::textChanged,
this, &Window::filterRegExpChanged); this, &Window::filterRegExpChanged);
connect(filterSyntaxComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
typedef void (QComboBox::*QComboIntSignal)(int);
connect(filterSyntaxComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged),
this, &Window::filterRegExpChanged); this, &Window::filterRegExpChanged);
connect(filterColumnComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged), connect(filterColumnComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &Window::filterColumnChanged); this, &Window::filterColumnChanged);
connect(filterCaseSensitivityCheckBox, &QAbstractButton::toggled, connect(filterCaseSensitivityCheckBox, &QAbstractButton::toggled,
this, &Window::filterRegExpChanged); this, &Window::filterRegExpChanged);

View File

@ -113,10 +113,9 @@ MainWindow::MainWindow()
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutBox); connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutBox);
//! [4] //! [4]
typedef void (QSpinBox::*QSpinBoxIntSignal)(int); connect(pixelSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
connect(pixelSizeSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
delegate, &PixelDelegate::setPixelSize); delegate, &PixelDelegate::setPixelSize);
connect(pixelSizeSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged), connect(pixelSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
this, &MainWindow::updateView); this, &MainWindow::updateView);
//! [4] //! [4]

View File

@ -342,13 +342,11 @@ void TextEdit::setupTextActions()
comboStyle->addItem("Ordered List (Roman lower)"); comboStyle->addItem("Ordered List (Roman lower)");
comboStyle->addItem("Ordered List (Roman upper)"); comboStyle->addItem("Ordered List (Roman upper)");
typedef void (QComboBox::*QComboIntSignal)(int); connect(comboStyle, QOverload<int>::of(&QComboBox::activated), this, &TextEdit::textStyle);
connect(comboStyle, static_cast<QComboIntSignal>(&QComboBox::activated), this, &TextEdit::textStyle);
typedef void (QComboBox::*QComboStringSignal)(const QString &);
comboFont = new QFontComboBox(tb); comboFont = new QFontComboBox(tb);
tb->addWidget(comboFont); tb->addWidget(comboFont);
connect(comboFont, static_cast<QComboStringSignal>(&QComboBox::activated), this, &TextEdit::textFamily); connect(comboFont, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textFamily);
comboSize = new QComboBox(tb); comboSize = new QComboBox(tb);
comboSize->setObjectName("comboSize"); comboSize->setObjectName("comboSize");
@ -360,7 +358,7 @@ void TextEdit::setupTextActions()
comboSize->addItem(QString::number(size)); comboSize->addItem(QString::number(size));
comboSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize())); comboSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize()));
connect(comboSize, static_cast<QComboStringSignal>(&QComboBox::activated), this, &TextEdit::textSize); connect(comboSize, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textSize);
} }
bool TextEdit::load(const QString &f) bool TextEdit::load(const QString &f)

View File

@ -159,8 +159,7 @@ PreviewForm::PreviewForm(QWidget *parent)
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
okButton = buttonBox->button(QDialogButtonBox::Ok); okButton = buttonBox->button(QDialogButtonBox::Ok);
typedef void(QComboBox::*ComboBoxIntSignal)(int); connect(encodingComboBox, QOverload<int>::of(&QComboBox::activated),
connect(encodingComboBox, static_cast<ComboBoxIntSignal>(&QComboBox::activated),
this, &PreviewForm::updateTextEdit); this, &PreviewForm::updateTextEdit);
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

View File

@ -94,10 +94,10 @@ RegularExpressionDialog::RegularExpressionDialog(QWidget *parent)
connect(optimizeOnFirstUsageOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh); connect(optimizeOnFirstUsageOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh);
connect(dontAutomaticallyOptimizeOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh); connect(dontAutomaticallyOptimizeOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh);
connect(offsetSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), connect(offsetSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
this, &RegularExpressionDialog::refresh); this, &RegularExpressionDialog::refresh);
connect(matchTypeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), connect(matchTypeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &RegularExpressionDialog::refresh); this, &RegularExpressionDialog::refresh);
connect(anchoredMatchOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh); connect(anchoredMatchOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh);

View File

@ -106,10 +106,9 @@ LocationDialog::LocationDialog(QWidget *parent)
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
typedef void (QComboBox::*QComboIntSignal)(int); connect(formatComboBox, QOverload<int>::of(&QComboBox::activated),
connect(formatComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
this, &LocationDialog::updateLocationsTable); this, &LocationDialog::updateLocationsTable);
connect(scopeComboBox, static_cast<QComboIntSignal>(&QComboBox::activated), connect(scopeComboBox, QOverload<int>::of(&QComboBox::activated),
this, &LocationDialog::updateLocationsTable); this, &LocationDialog::updateLocationsTable);
connect(organizationComboBox->lineEdit(), connect(organizationComboBox->lineEdit(),
&QLineEdit::editingFinished, &QLineEdit::editingFinished,
@ -117,7 +116,7 @@ LocationDialog::LocationDialog(QWidget *parent)
connect(applicationComboBox->lineEdit(), connect(applicationComboBox->lineEdit(),
&QLineEdit::editingFinished, &QLineEdit::editingFinished,
this, &LocationDialog::updateLocationsTable); this, &LocationDialog::updateLocationsTable);
connect(applicationComboBox, static_cast<QComboIntSignal>(&QComboBox::activated), connect(applicationComboBox, QOverload<int>::of(&QComboBox::activated),
this, &LocationDialog::updateLocationsTable); this, &LocationDialog::updateLocationsTable);
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

View File

@ -74,8 +74,7 @@ MainWindow::MainWindow()
filterCombo->addItem(tr("Monospaced"), QVariant::fromValue(QFontComboBox::MonospacedFonts)); filterCombo->addItem(tr("Monospaced"), QVariant::fromValue(QFontComboBox::MonospacedFonts));
filterCombo->addItem(tr("Proportional"), QVariant::fromValue(QFontComboBox::ProportionalFonts)); filterCombo->addItem(tr("Proportional"), QVariant::fromValue(QFontComboBox::ProportionalFonts));
filterCombo->setCurrentIndex(0); filterCombo->setCurrentIndex(0);
typedef void (QComboBox::*QComboBoxIntSignal)(int); connect(filterCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
connect(filterCombo, static_cast<QComboBoxIntSignal>(&QComboBox::currentIndexChanged),
this, &MainWindow::filterChanged); this, &MainWindow::filterChanged);
QLabel *fontLabel = new QLabel(tr("Font:")); QLabel *fontLabel = new QLabel(tr("Font:"));
@ -114,10 +113,9 @@ MainWindow::MainWindow()
this, &MainWindow::findSizes); this, &MainWindow::findSizes);
connect(fontCombo, &QFontComboBox::currentFontChanged, connect(fontCombo, &QFontComboBox::currentFontChanged,
characterWidget, &CharacterWidget::updateFont); characterWidget, &CharacterWidget::updateFont);
typedef void (QComboBox::*QComboBoxStringSignal)(const QString &); connect(sizeCombo, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
connect(sizeCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged),
characterWidget, &CharacterWidget::updateSize); characterWidget, &CharacterWidget::updateSize);
connect(styleCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged), connect(styleCombo, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
characterWidget, &CharacterWidget::updateStyle); characterWidget, &CharacterWidget::updateStyle);
//! [4] //! [5] //! [4] //! [5]
connect(characterWidget, &CharacterWidget::characterSelected, connect(characterWidget, &CharacterWidget::characterSelected,

View File

@ -71,8 +71,7 @@ QWidget *ImageDelegate::createEditor(QWidget *parent,
else if (index.column() == 2) else if (index.column() == 2)
comboBox->addItems(IconPreviewArea::iconStateNames()); comboBox->addItems(IconPreviewArea::iconStateNames());
typedef void (QComboBox::*QComboBoxIntSignal)(int); connect(comboBox, QOverload<int>::of(&QComboBox::activated),
connect(comboBox, static_cast<QComboBoxIntSignal>(&QComboBox::activated),
this, &ImageDelegate::emitCommitData); this, &ImageDelegate::emitCommitData);
return comboBox; return comboBox;

View File

@ -362,8 +362,7 @@ QWidget *MainWindow::createIconSizeGroupBox()
sizeButtonGroup = new QButtonGroup(this); sizeButtonGroup = new QButtonGroup(this);
sizeButtonGroup->setExclusive(true); sizeButtonGroup->setExclusive(true);
typedef void (QButtonGroup::*QButtonGroupIntBoolSignal)(int, bool); connect(sizeButtonGroup, QOverload<int, bool>::of(&QButtonGroup::buttonToggled),
connect(sizeButtonGroup, static_cast<QButtonGroupIntBoolSignal>(&QButtonGroup::buttonToggled),
this, &MainWindow::changeSize); this, &MainWindow::changeSize);
QRadioButton *smallRadioButton = new QRadioButton; QRadioButton *smallRadioButton = new QRadioButton;
@ -391,8 +390,7 @@ QWidget *MainWindow::createIconSizeGroupBox()
//! [26] //! [26]
//! [27] //! [27]
typedef void (QSpinBox::*QSpinBoxIntSignal)(int); connect(otherSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
connect(otherSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
this, &MainWindow::triggerChangeSize); this, &MainWindow::triggerChangeSize);
QHBoxLayout *otherSizeLayout = new QHBoxLayout; QHBoxLayout *otherSizeLayout = new QHBoxLayout;