PDA

View Full Version : QTimer Lambda



bmn
7th March 2016, 15:46
Hello,

I am trying to use the new Lambda method for a static QTimer call.
C++11 is enabled in the .pro file and I am working with Qt 5.5.1.

I am getting a segfault on line 6 when I try to use the following code snipped.
From my understanding this should generally use references to local variables except for the item pointer.
Since I manipulate the item, the lambda function has to use the mutable keyword.



void myWidget::modelItemChanged(QStandardItem *item)
{
QBrush brush = item->background();
item->setBackground(Qt::blue);
QTimer::singleShot(1000, [&, item]() mutable {
item->setBackground(brush);
});
}


Would appreciate your help.

anda_skoa
7th March 2016, 16:22
Your lambda captures "brush" by reference.
"brush" is destroyed when the modelItemChanged() method ends.
The lambda tries to access a destroyed object.

Cheers,
_

bmn
8th March 2016, 08:23
ouch, thats stupid.

thank you very much!