Ryan Kennedy
Ryan Kennedy

Reputation: 3615

Make QObject wait for its listeners before executing function

I'm working with structured light, and I have QCamera and QProjector classes I wrote. When the projector projects a pattern, it must wait for all attached cameras to capture that pattern before it moves on to the next one.

My current model is this:

Previously I had a hold() slot in QProjector. A camera who wanted the projector to wait for it would call

connect(projector,SIGNAL(hold()),camera,SLOT(wait()))` 

where camera->wait() is a dummy function. Then, in projector->disconnectNotify(), the projector checks to see if receivers(SIGNAL(hold())) is zero. If it is, the projector moves on to the next pattern in its queue; if not it waits some more.

This approach is a hot mess, and it doesn't even work--there is no guarantee that the projector won't move on to the next pattern between emitting patternProjected() and receiving the connection from the camera.

Anyone have a cleaner approach, ideally one more in line with the QObject philosophy?

Upvotes: 1

Views: 231

Answers (2)

chrisaverage
chrisaverage

Reputation: 254

You could provide a mediation object like so:

//startWork increments some internal counter
connect(camera_1...n, SIGNAL(captureStarted()), mediator, SLOT(startWork()))

//stopWork decrements the counter, when counter reaches 0 emits trigger()
connect(camera_1...n, SIGNAL(captureFinished()), mediator, SLOT(stopWork()))

//startCapture emits captureStarted
connect(projector, SIGNAL(patternProjected()), camera_1...n, SLOT(startCapture())

//projectNext emits patternProjected
connect(mediator, SIGNAL(trigger()), projector, SLOT(projectNext()))

Upvotes: 0

Spidey
Spidey

Reputation: 2589

You could initialize a member variable in your QProjector with receivers(SIGNAL(hold())), and every time the slot is called, you decrement it. When the counter reaches 0, you reinitialize with receivers(SIGNAL(hold())) and projects another pattern.

Of course, in this scenario hold() is a terrible (non-semantic) name, you should rename it to patternRead() or something like that.

Upvotes: 1

Related Questions