'How to connect signal to signal with new syntax QT5?

I am trying to connect a signal to a signal using the new syntax:

connect(ui->line,&QLineEdit::returnPressed,ui->button,&QPushButton::clicked);

But the compiler throws an error, with all this the old syntax works:

connect(ui->line,SIGNAL(returnPressed()),ui->button,SIGNAL(clicked()));

I know this can be connected to function click:

connect(ui->line,&QLineEdit::returnPressed,ui->button,&QPushButton::click);

But is there a way to connect it to the signal use new syntax?



Solution 1:[1]

From a quick glance at the documentation, the mismatch stems from the extra argument to the clickedsignal.

One option is to use a lambda to inject that argument:

connect(ui->line,&QLineEdit::returnPressed, this, [this]() { ui->button->clicked(false); });

Solution 2:[2]

You would need to use a lambda for this. But your example shows the confusion with the old syntax well. What would it do? Would it emit the signal with true or false?

I am sure it is documented somewhere, if nowhere else, then in the code. But surely, with the new syntax, being explicit, makes the code more readable, right?

So, I would write something like this if I were you:

#include <QApplication>
#include <QLineEdit>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLineEdit lineEdit;
    QPushButton pushButton;
    QObject::connect(&lineEdit, &QLineEdit::returnPressed, [&pushButton]() {emit pushButton.clicked(true);});
    return app.exec();
}

And for those, who just want to know whether or not the receiver can be a signal with the new connect syntax, the answer is that the receiver can be a signal, not just slot. This is why I also prefer to call this "new connect syntax" rather than "new signal/slot syntax". So, a simple signal to signal "mapping" would be this:

connect(
    sender, &Sender::valueChanged,
    receiver, &Receiver::valueChanged,
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 epsilon
Solution 2 lpapp