PDA

View Full Version : QTimer



link7
31st May 2009, 21:56
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:





void Monitor::mark()
{
QTextCursor cursor(textEdit->document());
colorFormat.setForeground(Qt::red);
cursor.setPosition(j);
if (cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 3))
cursor.mergeCharFormat(colorFormat);
}

void Monitor::quickSearch(const char *x, int m, const char *y, int n, QTextEdit *textEdit) {
this->textEdit = textEdit;
j = 0;
int k = 0;

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

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

nish
1st June 2009, 04:22
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().

THRESHE
1st June 2009, 13:29
Did you declare mark() as a slot?

wagmare
2nd June 2009, 07:54
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 ..

nish
2nd June 2009, 08:35
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.