PDA

View Full Version : Question about QTimer



A.H.M. Mahfuzur Rahman
7th August 2009, 14:01
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

A.H.M. Mahfuzur Rahman
7th August 2009, 14:03
If I am not clear, please, reply me about that.

caduel
7th August 2009, 14:18
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

A.H.M. Mahfuzur Rahman
7th August 2009, 15:01
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.

caduel
7th August 2009, 16:19
class X : public QObject
{
Q_OBJECT

X()
{
connect(this, SIGNAL(crossCaptureComplete()), SLOT(continue_it()));
}

Q_SIGNALS:
void crossCaptureComplete();
QTimer::singleShot(1500, this, SLOT(crossCapture()));
public Q_SLOTS:
// split the method that is interrupted by crossCapture in 2 parts:
void start_it()
{
// do something
// trigger cross
QTimer::singleShot(1500, this, SLOT(crossCapture()));
}

void continue_it()
{
// ...
}

// might run in a different object or thread...
void crossCapture()
{
// do something
emit crossCaptureComplete();
}
};

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.

A.H.M. Mahfuzur Rahman
8th August 2009, 05:25
I got the idea and it works.
Thank You very much.