Results 1 to 5 of 5

Thread: QTimer delayed timeout

  1. #1
    Join Date
    Dec 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer delayed timeout

    I have a custom QWidget with a QTimer set up in the resizeEvent function. The basic functionality of it is to execute some code only when the user has finished resizing the window (which in turn affects this QWidget). The code looks something like this:

    Qt Code:
    1. CustomWidget::CustomWidget() : QWidget {
    2.  
    3. resizeTimer = new QTimer;
    4. connect(resizeTimer, SIGNAL(timeout()), this, SLOT(resizeStopped()));
    5.  
    6. }
    7.  
    8. void CustomWidget::resizeEvent(QResizeEvent *event){
    9.  
    10. resizeTimer->start(5);
    11. functionA()
    12.  
    13. }
    14.  
    15. void CustomWidget::resizeStopped(){
    16. resizeTimer->stop();
    17. functionB();
    18. }
    19.  
    20. void CustomWidget::functionA(){
    21. //code
    22. }
    23. void CustomWidget::functionB(){
    24. //code
    25. }
    To copy to clipboard, switch view to plain text mode 

    What I expected to happen was, while the window/widget was being resized, the timer would keep being reset until the resizing was paused long enough for the timer to timeout (in this case, 5 ms). However, for some reason when I have low intervals such as the 5 ms here, functionB() will only be called after my mouse has been released (after resizing the window). Higher values such as 100 ms work as expected, functionB() is called while the mouse is still pressed.

    I'm not complaining, this is exactly what I want to happen, however I have no idea why it is, so I'm afraid it might raise some issues in the future which will confuse me. Anyone have any ideas?

    Some additional information, is that functionA and functionB take some time to finish, around 50 - 150 ms. I was thinking this might be 'blocking' the timeout somehow if it times out before the function A/B has finished executing. I really have no idea though.

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer delayed timeout

    The timeout signal will only be delivered when control reaches the applications event loop.
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

  3. #3
    Join Date
    Dec 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer delayed timeout

    But wouldn't it delivered regardless of how long the timer interval is (as long as you wait long enough)? I don't understand why the short and long intervals have an effect.

  4. #4
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer delayed timeout

    My first thought is to change the connection type. Have You tried setting the connection type to Qt::QueuedConnection. Another reason why the slot is not evoked might be the rapidness with witch the signals are being sent. I think the application loop might be removing duplicated calls (as the windows event queue does).
    Last edited by Grimlock; 10th December 2009 at 13:51.
    Me, Grimlock, not "nice dino". ME BASH BRAINS!

  5. #5
    Join Date
    Dec 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer delayed timeout

    Changing it to Qt::QueuedConnection had an effect, the long intervals behave like the short intervals. Now another question is, why is the timeout signal queued? Qt::QueuedConnection says that "When emitted, the signal is queued until the event loop is able to deliver it to the slot." but I don't see why the event loop can't deliver to the slot until the mouse is released.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  3. Replies: 2
    Last Post: 9th September 2009, 00:26
  4. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

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.