Results 1 to 3 of 3

Thread: QTimer Lambda

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Question QTimer Lambda

    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.

    Qt Code:
    1. void myWidget::modelItemChanged(QStandardItem *item)
    2. {
    3. QBrush brush = item->background();
    4. item->setBackground(Qt::blue);
    5. QTimer::singleShot(1000, [&, item]() mutable {
    6. item->setBackground(brush);
    7. });
    8. }
    To copy to clipboard, switch view to plain text mode 


    Would appreciate your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTimer Lambda

    Your lambda captures "brush" by reference.
    "brush" is destroyed when the modelItemChanged() method ends.
    The lambda tries to access a destroyed object.

    Cheers,
    _

  3. #3
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QTimer Lambda

    ouch, thats stupid.

    thank you very much!

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 20:47
  2. Replies: 15
    Last Post: 4th August 2012, 20:11
  3. Lambda callback functions
    By wayfaerer in forum Newbie
    Replies: 3
    Last Post: 12th March 2012, 11:33
  4. QTimer or something else
    By .:saeed:. in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2011, 08:47
  5. QTimer
    By nirup in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2007, 15:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.