[Development] Proposal: Deprecating platforms in Qt 5.6 that don't support lambda

André Pönitz apoenitz at t-online.de
Thu Feb 19 22:28:25 CET 2015


On Thu, Feb 19, 2015 at 03:41:42PM -0500, Matthew Woehlke wrote:
> On 2015-02-19 15:21, Marc Mutz wrote:
> > On Thursday 19 February 2015 13:29:48 Daniel Teske wrote:
> >> more than 400 lambdas in Creator's source
> > 
> > Sounds like lambdas are overused (as any new language feature is overused 
> > before it's fully understood by the resp. language community).
> 
> Maybe, maybe not.
> 
> I'm not sure I've even *written* 400 lambdas yet :-), but I find myself
> using them most often in QObject::connect. Basically, a lambda saves
> writing a protected (or worse, *private*) slot by allowing the relevant
> code to be written inline. These are rarely more than a few lines long,
> and it's not unusual for them to be one-liners, e.g.:
> 
>   connect(d->UI.scrollBar, &QAbstractSlider::valueChanged,
>           [d](int value){ d->scrollTo(value); });


That's one (good) example.

Another one would be to avoid passing data through QAction::data
and using QObject::sender() in cases like


    void someSlot()
    {
        const QAction *action = qobject_cast<const QAction *>(sender());
        /* FIXME: check action */
        Data data = action->data().value<Data>();
        foo()->useData(data);
    }

    /* ... possibly lots of lines inbetween ... */

    {
        ...
        Data data = /*...*/;
        QAction *action = new QAction(...)
        action->setData(data);
        connect(action, &QAction::triggered, this, &ThisClass::someSlot); 
        ...
   }

vs:

    {
        ...
        Data data = /*...*/;
        QAction *act = new QAction(...)
        connect(act, &QAction::triggered, [this, data] { foo()->useData(data); }
        ...
    }

This is less than half the code, is type-safe, an keeps related code in
one place. One line to express one idea. No awkward boilerplate.

Andre'

PS: I probably should praise Olivier more often for the new connect syntax.
Personally I think that's the #1 feature in Qt 5. It makes a real (positive)
difference in Qt application development.

PPS:

> > What about boost::function?
> 
> Ugh, make Qt depend on boost? No, thanks...

*grin*

Indeed.




More information about the Development mailing list