On Thursday 26 May 2011, Kishore wrote:
> On Thursday 26 May 2011 2:40:20 AM Thiago Macieira wrote:
> > QObject::connect(sender, &SenderClass::signalName,
> >
> > [](int i){ qDebug() << i; });
>
> OT. Could you just explain the above code? Is that just indicative or can
> you implement the method in the connect function call?
>
> Sorry, im just confused since i have never seen code like in your
> illustration and also without the SIGNAL() and SLOT() macros.
It's using function pointers and "[]..." is a C++0x lambda expression.
Instead of declaring a static method and then using a pointer to it, you
declare an anonymous function and get the pointer to it. The classic C++
method would be:
static void my_anonymous_function(int i){qDebug()<<i;}
//...
QObject::connect(sender, &SenderClass::signalName,&my_anonymous_function);
But lambdas are much more useful than this, they can refer to the context
they were created in:
void HelloClass::mymethod(int y)
{
int z=3;
QObject::connect(sender, &SenderClass:signalName,
[=](int i){ qDebug()<<"z plus i minus y equals" << z+i-y; } );
}
The above would be triggered whenever signalName is emitted by sender, use
the signal argument i plus the values of z and y as they were at the time
the lambda was created by the connect call. You can even use references, but
that is more dangerous of course.
Konrad
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Qt5-feedback mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback
