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:
parent
63e7ff97e9
commit
fb7bfbf18d
@ -122,8 +122,7 @@ Client::Client(QWidget *parent)
|
||||
//! [2] //! [3]
|
||||
connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
|
||||
//! [2] //! [4]
|
||||
typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError);
|
||||
connect(tcpSocket, static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error),
|
||||
connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
|
||||
//! [3]
|
||||
this, &Client::displayError);
|
||||
//! [4]
|
||||
|
@ -95,9 +95,7 @@ PaintedWindow::PaintedWindow()
|
||||
|
||||
connect(screen(), &QScreen::orientationChanged, this, &PaintedWindow::orientationChanged);
|
||||
connect(m_animation, &QAbstractAnimation::finished, this, &PaintedWindow::rotationDone);
|
||||
typedef void (PaintedWindow::*PaintedWindowVoidSlot)();
|
||||
connect(this, &PaintedWindow::rotationChanged,
|
||||
this, static_cast<PaintedWindowVoidSlot>(&PaintedWindow::paint));
|
||||
connect(this, &PaintedWindow::rotationChanged, this, QOverload<>::of(&PaintedWindow::paint));
|
||||
}
|
||||
|
||||
void PaintedWindow::exposeEvent(QExposeEvent *)
|
||||
|
@ -61,8 +61,6 @@
|
||||
|
||||
#include "glwidget.h"
|
||||
|
||||
typedef void (QWidget::*QWidgetVoidSlot)();
|
||||
|
||||
MainWindow::MainWindow()
|
||||
: m_nextX(1), m_nextY(1)
|
||||
{
|
||||
@ -131,14 +129,11 @@ MainWindow::MainWindow()
|
||||
QMenu *helpMenu = menuBar()->addMenu("&Help");
|
||||
helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
|
||||
|
||||
connect(m_timer, &QTimer::timeout,
|
||||
glwidget, static_cast<QWidgetVoidSlot>(&QWidget::update));
|
||||
connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
|
||||
|
||||
connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
|
||||
connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
|
||||
|
||||
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
||||
connect(updateInterval, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
||||
connect(updateInterval, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
this, &MainWindow::updateIntervalChanged);
|
||||
connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
|
||||
connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
|
||||
@ -162,7 +157,7 @@ void MainWindow::addNew()
|
||||
return;
|
||||
GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256));
|
||||
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);
|
||||
if (m_nextX == 3) {
|
||||
m_nextX = 1;
|
||||
|
@ -173,8 +173,6 @@ void OpenGLWindow::keyPressEvent(QKeyEvent *e)
|
||||
|
||||
void OpenGLWindow::setAnimating(bool enabled)
|
||||
{
|
||||
typedef void (QPaintDeviceWindow::*QPaintDeviceWindowVoidSlot)();
|
||||
|
||||
if (enabled) {
|
||||
// Animate continuously, throttled by the blocking swapBuffers() call the
|
||||
// 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
|
||||
// QSurfaceFormat::setSwapInterval()) is non-zero.
|
||||
connect(this, &QOpenGLWindow::frameSwapped,
|
||||
this, static_cast<QPaintDeviceWindowVoidSlot>(&QPaintDeviceWindow::update));
|
||||
this, QOverload<>::of(&QPaintDeviceWindow::update));
|
||||
update();
|
||||
} else {
|
||||
disconnect(this, &QOpenGLWindow::frameSwapped,
|
||||
this, static_cast<QPaintDeviceWindowVoidSlot>(&QPaintDeviceWindow::update));
|
||||
this, QOverload<>::of(&QPaintDeviceWindow::update));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,8 +70,7 @@ Screenshot::Screenshot()
|
||||
delaySpinBox->setSuffix(tr(" s"));
|
||||
delaySpinBox->setMaximum(60);
|
||||
|
||||
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
||||
connect(delaySpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
||||
connect(delaySpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
this, &Screenshot::updateCheckBox);
|
||||
|
||||
hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox);
|
||||
|
@ -80,8 +80,7 @@ Window::Window()
|
||||
|
||||
connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage);
|
||||
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
|
||||
typedef void (QComboBox::*QComboIntSignal)(int);
|
||||
connect(iconComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(iconComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &Window::setIcon);
|
||||
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
|
||||
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);
|
||||
|
@ -89,11 +89,9 @@ Window::Window()
|
||||
|
||||
connect(filterPatternLineEdit, &QLineEdit::textChanged,
|
||||
this, &Window::filterRegExpChanged);
|
||||
|
||||
typedef void (QComboBox::*QComboIntSignal)(int);
|
||||
connect(filterSyntaxComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(filterSyntaxComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &Window::filterRegExpChanged);
|
||||
connect(filterColumnComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(filterColumnComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &Window::filterColumnChanged);
|
||||
connect(filterCaseSensitivityCheckBox, &QAbstractButton::toggled,
|
||||
this, &Window::filterRegExpChanged);
|
||||
|
@ -113,10 +113,9 @@ MainWindow::MainWindow()
|
||||
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
|
||||
connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutBox);
|
||||
//! [4]
|
||||
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
||||
connect(pixelSizeSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
||||
connect(pixelSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
delegate, &PixelDelegate::setPixelSize);
|
||||
connect(pixelSizeSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
||||
connect(pixelSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
this, &MainWindow::updateView);
|
||||
//! [4]
|
||||
|
||||
|
@ -342,13 +342,11 @@ void TextEdit::setupTextActions()
|
||||
comboStyle->addItem("Ordered List (Roman lower)");
|
||||
comboStyle->addItem("Ordered List (Roman upper)");
|
||||
|
||||
typedef void (QComboBox::*QComboIntSignal)(int);
|
||||
connect(comboStyle, static_cast<QComboIntSignal>(&QComboBox::activated), this, &TextEdit::textStyle);
|
||||
connect(comboStyle, QOverload<int>::of(&QComboBox::activated), this, &TextEdit::textStyle);
|
||||
|
||||
typedef void (QComboBox::*QComboStringSignal)(const QString &);
|
||||
comboFont = new QFontComboBox(tb);
|
||||
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->setObjectName("comboSize");
|
||||
@ -360,7 +358,7 @@ void TextEdit::setupTextActions()
|
||||
comboSize->addItem(QString::number(size));
|
||||
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)
|
||||
|
@ -159,8 +159,7 @@ PreviewForm::PreviewForm(QWidget *parent)
|
||||
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||
okButton = buttonBox->button(QDialogButtonBox::Ok);
|
||||
|
||||
typedef void(QComboBox::*ComboBoxIntSignal)(int);
|
||||
connect(encodingComboBox, static_cast<ComboBoxIntSignal>(&QComboBox::activated),
|
||||
connect(encodingComboBox, QOverload<int>::of(&QComboBox::activated),
|
||||
this, &PreviewForm::updateTextEdit);
|
||||
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
||||
|
@ -94,10 +94,10 @@ RegularExpressionDialog::RegularExpressionDialog(QWidget *parent)
|
||||
connect(optimizeOnFirstUsageOptionCheckBox, &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);
|
||||
|
||||
connect(matchTypeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
||||
connect(matchTypeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &RegularExpressionDialog::refresh);
|
||||
|
||||
connect(anchoredMatchOptionCheckBox, &QCheckBox::toggled, this, &RegularExpressionDialog::refresh);
|
||||
|
@ -106,10 +106,9 @@ LocationDialog::LocationDialog(QWidget *parent)
|
||||
|
||||
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||
|
||||
typedef void (QComboBox::*QComboIntSignal)(int);
|
||||
connect(formatComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
|
||||
connect(formatComboBox, QOverload<int>::of(&QComboBox::activated),
|
||||
this, &LocationDialog::updateLocationsTable);
|
||||
connect(scopeComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
|
||||
connect(scopeComboBox, QOverload<int>::of(&QComboBox::activated),
|
||||
this, &LocationDialog::updateLocationsTable);
|
||||
connect(organizationComboBox->lineEdit(),
|
||||
&QLineEdit::editingFinished,
|
||||
@ -117,7 +116,7 @@ LocationDialog::LocationDialog(QWidget *parent)
|
||||
connect(applicationComboBox->lineEdit(),
|
||||
&QLineEdit::editingFinished,
|
||||
this, &LocationDialog::updateLocationsTable);
|
||||
connect(applicationComboBox, static_cast<QComboIntSignal>(&QComboBox::activated),
|
||||
connect(applicationComboBox, QOverload<int>::of(&QComboBox::activated),
|
||||
this, &LocationDialog::updateLocationsTable);
|
||||
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
||||
|
@ -74,8 +74,7 @@ MainWindow::MainWindow()
|
||||
filterCombo->addItem(tr("Monospaced"), QVariant::fromValue(QFontComboBox::MonospacedFonts));
|
||||
filterCombo->addItem(tr("Proportional"), QVariant::fromValue(QFontComboBox::ProportionalFonts));
|
||||
filterCombo->setCurrentIndex(0);
|
||||
typedef void (QComboBox::*QComboBoxIntSignal)(int);
|
||||
connect(filterCombo, static_cast<QComboBoxIntSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(filterCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, &MainWindow::filterChanged);
|
||||
|
||||
QLabel *fontLabel = new QLabel(tr("Font:"));
|
||||
@ -114,10 +113,9 @@ MainWindow::MainWindow()
|
||||
this, &MainWindow::findSizes);
|
||||
connect(fontCombo, &QFontComboBox::currentFontChanged,
|
||||
characterWidget, &CharacterWidget::updateFont);
|
||||
typedef void (QComboBox::*QComboBoxStringSignal)(const QString &);
|
||||
connect(sizeCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(sizeCombo, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
|
||||
characterWidget, &CharacterWidget::updateSize);
|
||||
connect(styleCombo, static_cast<QComboBoxStringSignal>(&QComboBox::currentIndexChanged),
|
||||
connect(styleCombo, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
|
||||
characterWidget, &CharacterWidget::updateStyle);
|
||||
//! [4] //! [5]
|
||||
connect(characterWidget, &CharacterWidget::characterSelected,
|
||||
|
@ -71,8 +71,7 @@ QWidget *ImageDelegate::createEditor(QWidget *parent,
|
||||
else if (index.column() == 2)
|
||||
comboBox->addItems(IconPreviewArea::iconStateNames());
|
||||
|
||||
typedef void (QComboBox::*QComboBoxIntSignal)(int);
|
||||
connect(comboBox, static_cast<QComboBoxIntSignal>(&QComboBox::activated),
|
||||
connect(comboBox, QOverload<int>::of(&QComboBox::activated),
|
||||
this, &ImageDelegate::emitCommitData);
|
||||
|
||||
return comboBox;
|
||||
|
@ -362,8 +362,7 @@ QWidget *MainWindow::createIconSizeGroupBox()
|
||||
sizeButtonGroup = new QButtonGroup(this);
|
||||
sizeButtonGroup->setExclusive(true);
|
||||
|
||||
typedef void (QButtonGroup::*QButtonGroupIntBoolSignal)(int, bool);
|
||||
connect(sizeButtonGroup, static_cast<QButtonGroupIntBoolSignal>(&QButtonGroup::buttonToggled),
|
||||
connect(sizeButtonGroup, QOverload<int, bool>::of(&QButtonGroup::buttonToggled),
|
||||
this, &MainWindow::changeSize);
|
||||
|
||||
QRadioButton *smallRadioButton = new QRadioButton;
|
||||
@ -391,8 +390,7 @@ QWidget *MainWindow::createIconSizeGroupBox()
|
||||
//! [26]
|
||||
|
||||
//! [27]
|
||||
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
||||
connect(otherSpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
||||
connect(otherSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||
this, &MainWindow::triggerChangeSize);
|
||||
|
||||
QHBoxLayout *otherSizeLayout = new QHBoxLayout;
|
||||
|
Loading…
x
Reference in New Issue
Block a user