Solved Connect doesn't work with lambda function
-
@Aaron-Kim said in Connect doesn't work with lambda function:
auto lambda = ={
static_cast<MainWindow*>(mainWindow)->changeContent(num);
};
connect(this, SIGNAL(clicked()), mainWindow, lambda);dont mix Qt4 and Qt5 Signal/Slot syntax, also, Lambda functions only work with the new syntax.
connect(this, &MenuItem::clicked, mainWindow, [=](){ static_cast<MainWindow*>(mainWindow)->changeContent(num); });
-
@J.Hilk Thanks! I didn't know that SIGNAL() is an old style.
Should I change all the SINGAL() syntaxes to the new one? -
@Aaron-Kim said in [Solved] Connect doesn't work with lambda function:
Should I change all the SINGAL() syntaxes to the new one?
It is recommended to do so, indeed.
One note: to solve the forum topics as SOLVED, please use the button topic tools below your first post, do not write [Solved] yourself.
Thanks!
-
@Aaron-Kim said in [Solved] Connect doesn't work with lambda function:
Should I change all the SINGAL() syntaxes to the new one?
Well, thats up to you.
Take a look at this wiki article and decide for yourself how to proceed further :)
https://wiki.qt.io/New_Signal_Slot_Syntax -
Hi
Just as a note.
The new syntax will catch error at compile time and hence
is preferred over the SIGNAL/SLOT ones as they will compile even
the connect wont work.
Also, there is a slight performance gain runtime with the new syntax.
The downside is super ugly syntax with overloaded functions so make sure to
check out qOverload -
@J.Hilk One more thing, Is there any reason for me to use &QPushButton::clicked, rather than just QPushButton::clicked?
As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works. -
@Aaron-Kim said in Connect doesn't work with lambda function:
As far as I know, the signs & and * have no effect on function pointers. And I checked that QPushButton::clicked also works.
definitely thats not true.
The method signature expects a function-pointer, hence the referencing with&
-
@raven-worx
https://stackoverflow.com/questions/6893285/why-do-function-pointer-definitions-work-with-any-number-of-ampersands-or-as
But this articles says that a function can be implicitly converted to a function pointer. Does the implementation of connect() require explicit function-pointer syntax with an ampersand? -
@Aaron-Kim
my opinion: just stay on the safe side (by using the&
function reference) and don't rely on any implicit compiler features. Also its clear what the code does on the first glance. -
@Aaron-Kim
Hi
They talk about pointer to a (c) function but with the
connect we are talking pointer to a member function.
Also with no &
connect(ui->pushButton, QPushButton::released, this, { qDebug() << "test";} );
looks like a call to a static member function for compiler
so & is need to mean take address of this member. -
Hi,
@J.Hilk said in Connect doesn't work with lambda function:
static_cast<MainWindow*>(mainWindow)->changeContent(num);
Also you should rather use qobject_cast when dealing with QObject derived classes.
-
@SGaist
very true!
My bad though, was copy and pasting from the op 🙈