PDA

View Full Version : QTimer problem



vv
2nd July 2007, 21:18
Hello
I am trying to insert qtimer in my program but in run time when I enter a function
timer does not go into the slot. I want qtimer to work parallel with the other code in my program. Here is the code:
-----------------------------------------
testplug::testplug(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
a = 0;
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT( myclick()));
connect(&mytimer,SIGNAL(timeout()),this,SLOT(deneme()));
mytimer.start(1000);
}

void testplug::deneme()
{
printf("Var is = %d\n",a++);

}
void testplug::myclick()
{
::Sleep(3000);
printf("exiting..");

}
---------------------------------
In the above code when it is in the myclick function, timeout occurs but it does not goes into the deneme() function.

What can be the problem ?

Thanks in advance.

jacek
2nd July 2007, 21:27
The timer will work only if the event loop works, but you block it with sleep().

vv
2nd July 2007, 21:40
I am removing sleep() and making something in the function.
In this case it is not runs the deneme() when the timeout occurs.

vv
2nd July 2007, 21:48
In Qt when an ordinary function is called, does event loop run?
I think it runs.
I change the myclick()

void myclick()
{
for(int i = 0;i<5000;++i);

}
and again QTimer does not work when the myclick() runs.

wysota
2nd July 2007, 21:57
Because you're still blocking the loop. Connect a timeout() signal of the timer to a custom slot, start the timer and return from the myclick() slot. Your other slot will be called when the timer timeouts.

jacek
2nd July 2007, 21:59
In Qt when an ordinary function is called, does event loop run?
No, because you have a single threaded application. Only one thing can happen at the same time.



void myclick()
{
for(int i = 0;i<5000;++i);

}
and again QTimer does not work when the myclick() runs.
It doesn't differ much from the one with sleep(). If you want to write such long loops, you have to invoke QCoreApplication::processEvents() from time to time to process events from the event queue.

vv
3rd July 2007, 06:35
Thank you very much Jacek for your replies..

vv
3rd July 2007, 07:01
Hi again,

I have a problem again.
I set my timer interval 1000 msec. In the myclick() slot sleep(5000).
After sleep, I call the processEvents() but it only once runs the timer slot deneme().
But it must run the deneme at least four times I think. Am I right?

wysota
3rd July 2007, 07:43
No, you're wrong. It should fire only once. Thus you should avoid using sleep in your application :) I really suggest you get rid of the busy loop and use a separate slot.

vv
3rd July 2007, 08:13
ok wysota.. I understan at last:o thanks