- 1. Example
- 1. widget.h
- 2. widget.cpp
- 3. Output
We all know that in Qt there are two syntaxes of signals and slots:
- Old Syntax on SIGNAL SLOT Macros
- New syntax on pointers
But also, as it is not necessary to interfere with beer with vodka, with the same success it is not necessary to mix two syntaxes within the framework of one project.
Of course, there are projects that write a lot of code and make a complete refactoring on changing the whole syntax is quite an overhead job, but if you, gradually completing the project, switch to a new syntax, try to update all the code places with the given one connection to a new syntax.
The fact is that for both cases ( SINGAL SLOT macros and syntax on pointers ) other contents of moc files are formed, which leads to the fact that mixed usage of connect and disconnect methods does not work as expected. And to be precise, the disconnect method will not work if connect was called using macros, and disconnect was called using pointers.
Example
We will create a trial project in which there will be a window and one button. Add a slot in the window. And in the window class designer, let's check the four combinations of connecting the button signal to the window slot:
- connect SIGNAL SLOT - disconnect SIGNAL SLOT
- connect SIGNAL SLOT - disconnect on pointers
- connect on pointers - disconnect on pointers
- connect on pointers - disconnect SIGNAL SLOT
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); public slots: void checkSlot() {} // Slot for checking private: Ui::Widget *ui; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); qDebug() << "First test"; qDebug() << connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(checkSlot())); qDebug() << disconnect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(checkSlot())); qDebug() << "Second test"; qDebug() << connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(checkSlot())); qDebug() << disconnect(ui->pushButton, &QPushButton::clicked, this, &Widget::checkSlot); qDebug() << "Third test"; qDebug() << connect(ui->pushButton,&QPushButton::clicked, this, &Widget::checkSlot); qDebug() << disconnect(ui->pushButton, &QPushButton::clicked, this, &Widget::checkSlot); qDebug() << "Fourth test"; qDebug() << connect(ui->pushButton,&QPushButton::clicked, this, &Widget::checkSlot); qDebug() << disconnect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(checkSlot())); } Widget::~Widget() { delete ui; }
Output
First test true true Second test true false Third test true true Fourth test true true
Thus, it turns out that the pair connect SINGAL SLOT - disconnect on the pointers gives the wrong result that was expected. And in fact the slot remains connected.
Therefore, beginners recommend that I pay close attention to this nuance of working with signals and slots in Qt.
Who hasn’t mixed beer with vodka! 🤢🤮
vodka + beer = strong coctail, but it is bad ))
disconnect - обязательно делать, во всех случаях?
Если удаляете объект, то все коннекты отключаются автоматически, здесь следить не требуется.