[Qt5-feedback] Make some undocumented features documented

Konrad Rosenbaum konrad at silmor.de
Thu May 26 08:59:07 CEST 2011


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt.nokia.com/pipermail/qt5-feedback/attachments/20110526/981a1ecd/attachment.bin 


More information about the Qt5-feedback mailing list