Add examples for how not to use signals. (#2377)

This commit is contained in:
karliss 2020-08-08 18:35:40 +03:00 committed by GitHub
parent d6d2e90028
commit 32adf0b5ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -178,17 +178,40 @@ Example:
QObject *object = nullptr; QObject *object = nullptr;
Connecting Signals Connecting Qt Signals
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
To connect a signal to a slot, this is the preferred syntax: Use one of the following methods for connecting signals to slots:
.. code:: cpp .. code:: cpp
connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed); // typically you will make connection in the constructor to a member of current class
connect(this->ui->button1, &QPushButton::clicked,
this, &MyObject::buttonClicked); // Good
// you can also connect directly other object slots
connect(checkbox, &QCheckBox::toggled, widget, &QWidget::setEnabled); // Good
// use lambda for passing extra arguments
connect(button1, &QPushButton::clicked, this, [this](){ foo(getBar()); }); // Good
This syntax performs compile-time type checks and allows the use of lambda This syntax performs compile-time type checks and allows the use of lambda
functions. Other approaches for connecting signals silently break at runtime. functions. Other approaches for connecting signals can silently break at runtime.
Don't use the older macro based syntax or automatic name based connections.
.. code:: cpp
// SIGNAL and SLOT macros
connect(sender, SIGNAL(clicked), this, SLOT(buttonClicked)); // BAD
// automatic name based connection
slot:
void on_actionNew_triggered(); // BAD
// 3 argument connect without receiver object
connect(sender, &SomeObject::signal, [this](){ this->foo(getBar()); }); // BAD
General Coding Advices General Coding Advices
---------------------- ----------------------