Qt Singleshot Timer with Lambda function as slot


Example

If a singleshot timer is required, it is quiet handy to have the slot as lambda function right in the place where the timer is declared:

QTimer::singleShot(1000, []() { /*Code here*/ } );

Due to this Bug (QTBUG-26406), this is way is only possible since Qt5.4.

In earlier Qt5 versions it has to be done with more boiler plate code:

  QTimer *timer = new QTimer(this);
  timer->setSingleShot(true);

  connect(timer, &QTimer::timeout, [=]() {
    /*Code here*/
    timer->deleteLater();
  } );