Results 1 to 5 of 5

Thread: QTimer

  1. #1
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTimer

    Hi all,
    I have a problem with QTimer, all I want is to execute a piece of code within a loop each 2 seconds, but nothing happens, here is what I tried:

    Qt Code:
    1. void Monitor::mark()
    2. {
    3. QTextCursor cursor(textEdit->document());
    4. colorFormat.setForeground(Qt::red);
    5. cursor.setPosition(j);
    6. if (cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 3))
    7. cursor.mergeCharFormat(colorFormat);
    8. }
    9.  
    10. void Monitor::quickSearch(const char *x, int m, const char *y, int n, QTextEdit *textEdit) {
    11. this->textEdit = textEdit;
    12. j = 0;
    13. int k = 0;
    14.  
    15. while (j <= n - m) {
    16. if (memcmp(x, y + j, m) == 0)
    17. {
    18. //mark();
    19. QTimer::singleShot(2000, this, SLOT(mark()));
    20. }
    21. j += qsBc[y[j + m]];
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    The method mark() works just fine, when I call him without Qtimer, but I want to have a 2secs pause before doing that.
    Tried to search forums, but no results.
    Using QT4.5, PCLinuxOS
    Please help me, I am out of time

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer

    lets suppose your while loop runs for 100 iterations at one particular call.

    i guess that those 100 iterations would be completed in less than 2 secs. So you will be out of your loop before the timer can call the mark() for first time...

    print the current time at end of Monitor::quickSearch() and print the time at the beginning of mark().

  3. #3
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTimer

    Did you declare mark() as a slot?
    C++ & AMD forever

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer

    while (j <= n - m) {
    if (memcmp(x, y + j, m) == 0)
    {
    //mark();
    QTimer::singleShot(2000, this, SLOT(mark()));
    }
    j += qsBc[y[j + m]];
    }
    }

    after u comes out of the loop
    check this condition
    QTimer::bool isActive () const

    before j += qsBc[y[j + m]]; ie ... if timer->isActive() returns false only u try to increment .. till timer->isActive() returns false u check the condition in a while loop ...

    also dont forget to use qApp->processEvents(); in between the calls ..
    Last edited by wagmare; 2nd June 2009 at 08:00. Reason: adding processEvent() call ..
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer

    This guy posted the same thing on a different forum... he got the answer there... but please dont just leave when u get a answer... post the solution so that others having a similar problem can benefit... i was like that before .. not now.

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. Using QTimer in QThreads
    By tzhu in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2009, 11:12
  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.