difference between connect functions
-
Hi.
Until today I thought these lines do the sameconnect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged);/*doesn't work*/ connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/*works*/
and I was using first one cause it is more legible(at least in my eyes).
Unfortunately it doesn't because like you can see in comments only one of them works.
My question is very simple - why?mainwindow.h:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); ~MainWindow() {} QComboBox *combo = new QComboBox; QGridLayout *mainLayout = new QGridLayout; QWidget *mainWidget = new QWidget; public slots: void ComboChanged(int i); };
mainwindow.cpp:
#include "mainwindow.h" MainWindow::MainWindow(){ combo->addItem("index 0"); combo->addItem("index 1"); mainLayout->addWidget(combo); mainWidget->setLayout(mainLayout); setCentralWidget(mainWidget); // connect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged); - doesn't work connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/* - works*/ } void MainWindow::ComboChanged(int i){qDebug() << "everything works fine";}
-
only one of them works.
it's explained in the docs: https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged
I thought these lines do the same
They do achieve the same result in 2 different ways: https://wiki.qt.io/New_Signal_Slot_Syntax
Basically in new code you should never use the old
SIGNAL()
/SLOT()
method -
Thank you so much for this link.
I think I understand everything except one.
"There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."
what if I delete an object?
for example change the last line in above code:void MainWindow::ComboChanged(int i){delete combo;}
-
@Corvette653 combo is not the receiver, combo is the emitter. You should not delete widgets like this, do
combo->deleteLater();
instead.
-
@jsulm
does this line automatically disconnect or should I do this by myself like here?//connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged); //... void MainWindow::ComboChanged(int i){ disconnect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged); combo->deleteLater(); }
-
@Corvette653 No need to disconnect manually.
-
@Corvette653 said in difference between connect functions:
"There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."
That refers to the 3 arguments version of
connect
. Take this example:connect(lineEdit,&QLineEdit::textChanged,[label](const QString& text){label->setText(text);});
if you calldelete label;
Qt can't know it should not execute the slot anymore and will crash when trying to calllabel->setText
. As mentioned in the docs, in the following phrase, the solution is to addlabel
as a context argument (connect(lineEdit,&QLineEdit::textChanged,label,[label](const QString& text){label->setText(text);});
) so that Qt knows that whenlabel
is destroyed it should not execute the slot anymore