Results 1 to 6 of 6

Thread: Question about QTimer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Question about QTimer

    Is it possible to block code before the timeout() signal is emitted?
    I tried like this:

    void capture(){
    int i = 0;
    bool allowed = false; // global

    while(!allowed){
    if(i == 0){
    QTimer::singleShot(1500, this, SLOT(crossCapture()));
    i = 1;
    }
    }

    QDebug() << "I have finished working with crossCapture()";
    }
    void crossCapture(){
    allowed = true;
    }


    In this way, I thought QTimer will be called only once, so for crossCapture() and the while loop will continue until allowed is set true in crossCapture().

    But, It didn't worked. The while loop goes inifinte,crossCapture() is not even called and QDebug() is not reached.

    Please Help.
    Thanks

  2. #2
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QTimer

    If I am not clear, please, reply me about that.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QTimer

    i) to just once call that singleshot timer, why not just place that call before the loop?
    ii) QTimer events are process only by a running event loop; your code is busy running your while loop, thus no event loop is run, thus no timer events are process and your slot is never called
    iii) if you want, you can place the "crossCapture" stuff into a separate thread (if it is a long running operation)
    iv) either way:
    use a signal to trigger crossCapture(); the exit the calling function (**)
    use another signal, to "continue" where (**) left off

    HTH

  4. #4
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QTimer

    use a signal to trigger crossCapture(); the exit the calling function (**)
    use another signal, to "continue" where (**) left off


    Will you please give some detail, may be with some code.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QTimer

    Qt Code:
    1. class X : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. X()
    6. {
    7. connect(this, SIGNAL(crossCaptureComplete()), SLOT(continue_it()));
    8. }
    9.  
    10. Q_SIGNALS:
    11. void crossCaptureComplete();
    12. QTimer::singleShot(1500, this, SLOT(crossCapture()));
    13. public Q_SLOTS:
    14. // split the method that is interrupted by crossCapture in 2 parts:
    15. void start_it()
    16. {
    17. // do something
    18. // trigger cross
    19. QTimer::singleShot(1500, this, SLOT(crossCapture()));
    20. }
    21.  
    22. void continue_it()
    23. {
    24. // ...
    25. }
    26.  
    27. // might run in a different object or thread...
    28. void crossCapture()
    29. {
    30. // do something
    31. emit crossCaptureComplete();
    32. }
    33. };
    To copy to clipboard, switch view to plain text mode 

    I hope you get the idea. Of course, this is just a sketch and you need to adapt that to whatever you are trying to do.

  6. #6
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QTimer

    I got the idea and it works.
    Thank You very much.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Extending QTimer()
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 01:59
  3. QTimer Question
    By ComaWhite in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 18:07
  4. QThread/QObject and QTimer
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 08:37
  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
  •  
Qt is a trademark of The Qt Company.