examples/: compile with QT_NO_CONTEXTLESS_CONNECT

Examples are usually a good way to get to know a new codebase, do not
teach developers who are new to Qt about the 3-arg connect() to begin
with.

Drive-by changes:
- `this` can't be implicitly captured with [=] in a lambda, instead
  capture by reference
- Update docs related to the sqlbrowser example; the overloaded signal
  it mentions has been removed in Qt6
- In the sqlbrowser example, rename addConnection() (no-arg) overload to
  openNewConnectionDialog, suggested in code review

Change-Id: I30c9f35bda4ac2f460d767ab7f84422ae3ed09f7
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Ahmad Samir 2023-07-21 01:04:40 +03:00
parent 9016def4dc
commit 09b852b1d8
9 changed files with 24 additions and 31 deletions

View File

@ -3,6 +3,8 @@
qt_examples_build_begin(EXTERNAL_BUILD) qt_examples_build_begin(EXTERNAL_BUILD)
add_compile_definitions(QT_NO_CONTEXTLESS_CONNECT)
add_subdirectory(corelib) add_subdirectory(corelib)
add_subdirectory(embedded) add_subdirectory(embedded)
if(TARGET Qt6::DBus) if(TARGET Qt6::DBus)

View File

@ -28,29 +28,29 @@ int main(int argc, char *argv[])
// Initialize subscription data // Initialize subscription data
QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly"); QRadioButton *monthly = w.findChild<QRadioButton *>("btnMonthly");
QObject::connect(monthly, &QRadioButton::clicked, [&] { QObject::connect(monthly, &QRadioButton::clicked, monthly, [&] {
subscription.setDuration(BindableSubscription::Monthly); subscription.setDuration(BindableSubscription::Monthly);
}); });
QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly"); QRadioButton *quarterly = w.findChild<QRadioButton *>("btnQuarterly");
QObject::connect(quarterly, &QRadioButton::clicked, [&] { QObject::connect(quarterly, &QRadioButton::clicked, quarterly, [&] {
subscription.setDuration(BindableSubscription::Quarterly); subscription.setDuration(BindableSubscription::Quarterly);
}); });
QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly"); QRadioButton *yearly = w.findChild<QRadioButton *>("btnYearly");
QObject::connect(yearly, &QRadioButton::clicked, [&] { QObject::connect(yearly, &QRadioButton::clicked, yearly, [&] {
subscription.setDuration(BindableSubscription::Yearly); subscription.setDuration(BindableSubscription::Yearly);
}); });
// Initialize user data // Initialize user data
QPushButton *germany = w.findChild<QPushButton *>("btnGermany"); QPushButton *germany = w.findChild<QPushButton *>("btnGermany");
QObject::connect(germany, &QPushButton::clicked, [&] { QObject::connect(germany, &QPushButton::clicked, germany, [&] {
user.setCountry(BindableUser::Country::Germany); user.setCountry(BindableUser::Country::Germany);
}); });
QPushButton *finland = w.findChild<QPushButton *>("btnFinland"); QPushButton *finland = w.findChild<QPushButton *>("btnFinland");
QObject::connect(finland, &QPushButton::clicked, [&] { QObject::connect(finland, &QPushButton::clicked, finland, [&] {
user.setCountry(BindableUser::Country::Finland); user.setCountry(BindableUser::Country::Finland);
}); });
QPushButton *norway = w.findChild<QPushButton *>("btnNorway"); QPushButton *norway = w.findChild<QPushButton *>("btnNorway");
QObject::connect(norway, &QPushButton::clicked, [&] { QObject::connect(norway, &QPushButton::clicked, norway, [&] {
user.setCountry(BindableUser::Country::Norway); user.setCountry(BindableUser::Country::Norway);
}); });

View File

@ -64,25 +64,25 @@ int main(int argc, char *argv[])
// Track the price changes // Track the price changes
//! [connect-price-changed] //! [connect-price-changed]
QObject::connect(&subscription, &Subscription::priceChanged, [&] { QObject::connect(&subscription, &Subscription::priceChanged, priceDisplay, [&] {
QLocale lc{QLocale::AnyLanguage, user.country()}; QLocale lc{QLocale::AnyLanguage, user.country()};
priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration())); priceDisplay->setText(lc.toCurrencyString(subscription.price() / subscription.duration()));
}); });
//! [connect-price-changed] //! [connect-price-changed]
//! [connect-validity-changed] //! [connect-validity-changed]
QObject::connect(&subscription, &Subscription::isValidChanged, [&] { QObject::connect(&subscription, &Subscription::isValidChanged, priceDisplay, [&] {
priceDisplay->setEnabled(subscription.isValid()); priceDisplay->setEnabled(subscription.isValid());
}); });
//! [connect-validity-changed] //! [connect-validity-changed]
//! [connect-user] //! [connect-user]
QObject::connect(&user, &User::countryChanged, [&] { QObject::connect(&user, &User::countryChanged, &subscription, [&] {
subscription.calculatePrice(); subscription.calculatePrice();
subscription.updateValidity(); subscription.updateValidity();
}); });
QObject::connect(&user, &User::ageChanged, [&] { QObject::connect(&user, &User::ageChanged, &subscription, [&] {
subscription.updateValidity(); subscription.updateValidity();
}); });
//! [connect-user] //! [connect-user]

View File

@ -154,10 +154,6 @@
We need to use lambdas for connecting the \c enableButtons slot We need to use lambdas for connecting the \c enableButtons slot
because its signature does not match \c QTextEdit::textChanged because its signature does not match \c QTextEdit::textChanged
and \c QComboBox::currentIndexChanged. and \c QComboBox::currentIndexChanged.
Since the latter has another overload with the signature
\c {const QString &} and the selected signal would be ambiguous,
we need to use \c QOverload<int>::of to select a specific overload
for \c currentIndexChanged.
We add all the widgets into a layout, store the item ID and the We add all the widgets into a layout, store the item ID and the
name of the displayed image file for future reference, and set name of the displayed image file for future reference, and set

View File

@ -37,12 +37,8 @@ InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *items,
//! [3] //! [3]
//! [4] //! [4]
connect(descriptionEditor, &QTextEdit::textChanged, [=]() { connect(descriptionEditor, &QTextEdit::textChanged, this, [this]() { enableButtons(); });
enableButtons(); connect(imageFileEditor, &QComboBox::currentIndexChanged, this, [this]() { enableButtons(); });
});
connect(imageFileEditor, &QComboBox::currentIndexChanged, [=]() {
enableButtons();
});
QFormLayout *formLayout = new QFormLayout; QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(itemLabel, itemText); formLayout->addRow(itemLabel, itemText);

View File

@ -71,7 +71,7 @@ QSqlError Browser::addConnection(const QString &driver, const QString &dbName, c
return err; return err;
} }
void Browser::addConnection() void Browser::openNewConnectionDialog()
{ {
QSqlConnectionDialog dialog(this); QSqlConnectionDialog dialog(this);
if (dialog.exec() != QDialog::Accepted) if (dialog.exec() != QDialog::Accepted)

View File

@ -32,7 +32,7 @@ public slots:
void exec(); void exec();
void showTable(const QString &table); void showTable(const QString &table);
void showMetaData(const QString &table); void showMetaData(const QString &table);
void addConnection(); void openNewConnectionDialog();
void currentChanged() { updateActions(); } void currentChanged() { updateActions(); }
void about(); void about();

View File

@ -33,18 +33,17 @@ int main(int argc, char *argv[])
mainWin.setCentralWidget(&browser); mainWin.setCentralWidget(&browser);
QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File")); QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
fileMenu->addAction(QObject::tr("Add &Connection..."), fileMenu->addAction(QObject::tr("Add &Connection..."), &browser,
[&]() { browser.addConnection(); }); &Browser::openNewConnectionDialog);
fileMenu->addSeparator(); fileMenu->addSeparator();
fileMenu->addAction(QObject::tr("&Quit"), []() { qApp->quit(); }); fileMenu->addAction(QObject::tr("&Quit"), qApp, &QApplication::quit);
QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help")); QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
helpMenu->addAction(QObject::tr("About"), [&]() { browser.about(); }); helpMenu->addAction(QObject::tr("About"), &browser, &Browser::about);
helpMenu->addAction(QObject::tr("About Qt"), []() { qApp->aboutQt(); }); helpMenu->addAction(QObject::tr("About Qt"), qApp, &QApplication::aboutQt);
QObject::connect(&browser, &Browser::statusMessage, [&mainWin](const QString &text) { QObject::connect(&browser, &Browser::statusMessage, &mainWin,
mainWin.statusBar()->showMessage(text); [&mainWin](const QString &text) { mainWin.statusBar()->showMessage(text); });
});
addConnectionsFromCommandline(app.arguments(), &browser); addConnectionsFromCommandline(app.arguments(), &browser);
mainWin.show(); mainWin.show();

View File

@ -38,7 +38,7 @@ Renderer::Renderer(VulkanWindow *w, int initialCount)
m_blockMesh.load(QStringLiteral(":/block.buf")); m_blockMesh.load(QStringLiteral(":/block.buf"));
m_logoMesh.load(QStringLiteral(":/qt_logo.buf")); m_logoMesh.load(QStringLiteral(":/qt_logo.buf"));
QObject::connect(&m_frameWatcher, &QFutureWatcherBase::finished, [this] { QObject::connect(&m_frameWatcher, &QFutureWatcherBase::finished, m_window, [this] {
if (m_framePending) { if (m_framePending) {
m_framePending = false; m_framePending = false;
m_window->frameReady(); m_window->frameReady();