[Qt5-feedback] Make some undocumented features documented

Kishore kitts.mailinglists at gmail.com
Thu May 26 12:05:25 CEST 2011


On Thursday 26 May 2011 12:16:35 PM Reece Dunn wrote:
> On 26 May 2011 07:26, Kishore <kitts.mailinglists at gmail.com> 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?
> 
> That code is dependent on Qt5 implementing the functionality Thiago
> described and a C++0x enabled compiler with at least lambda support
> (e.g. VS2010 and gcc 4.5).
> 
> Without lambda support, the Qt5 functionality would allow you to write:
> 
> ---
> void debug_int(int i) {
>    gDebug << i;
> }
> 
> QObject::connect(sender, &SenderClass::signalName, &debug_int);
> ---
> 
> Lambdas are like creating function objects (classes implementing
> 'operator()'). So instead of something like:
> 
> ---
> struct printer : public std::unary_function<void, int
> {
>    void operator()(int i) {
>       qDebug << i;
>    }
> };
> 
> QObject::connect(sender, &SenderClass::signalName, printer());
> ---
> 
> you can simply write printer as: "[](int i) { qDebug << i; }"
> 
> e.g.
> 
> ---
> std::for_each(v.begin(), v.end(), [](int i) { qDebug << i; });
> ---
> 
> BTW: It would be awesome if Qt could support this!
> 
> HTH,

Thanks all for the explanation. Indeed, c++11 lambda is interesting. I shall 
try spend time and learn more of the new features! :)
-- 
Cheers!
Kishore


More information about the Qt5-feedback mailing list