Reputation: 133
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?
Upvotes: 3
Views: 2246
Reputation: 53155
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,
);
Upvotes: 1
Reputation: 30840
From a quick glance at the documentation, the mismatch stems from the extra argument to the clicked
signal.
One option is to use a lambda to inject that argument:
connect(ui->line,&QLineEdit::returnPressed, this, [this]() { ui->button->clicked(false); });
Upvotes: 6