When you press the button,
while(capture) { ... }
the loop will run forever since capture
will never be set to NULL. This means that the code stream never leaves your loop, and thus the main thread cannot handle anything else, such as repainting, for example.
QTimer will emit its timeout signals (), but they will be placed as Events in the Qt Event Queue. While your on_buttonCaptureVideo_clicked()
method is running, these events will not be processed.
Here are my suggestions on how to make it work:
This code:
// Set to 25 frames per second const int imagePeriod = 1000/25; // ms imageTimer = new QTimer(this); imageTimer->setInterval(imagePeriod); connect(imageTimer, SIGNAL(timeout()), this, SLOT(doNextFrame())); // Use the default camera capture = cvCreateCameraCapture(-1);
belongs to the MainWindow constructor, because you want to install it only once. There is no need to do this again when the user presses a button on the second, third, etc.
The code that is inside your while
should go to the doNextFrame()
slot (without the while construct).
Then your button will do
imageTimer->start();
and then for example do
imageTimer->stop();
when pressed again.
Code example:
void MainWindow::on_buttonCaptureVideo_clicked() { if( imageTimer->isActive() ) { imageTimer->stop(); } else { imageTimer->start(); } }
What happens if you do this?
When you press the button, your slot with on_buttonCaptureVideo_clicked()
will be called from the GUI thread, the timer will start, and the method will return almost instantly.
Now the GUI thread is free and able to process reviews, etc.
From now on, the timer will send timeout signals () every 40 ms. Whenever the GUI thread is free, it will process this signal and call your doNextFrame
slot.
This slot will capture the next frame and return when it is done. When this is done, the GUI thread will be able to process other events again (for example, redraw).
As soon as you press the button again, the timer will stop and no new timeout () events will be dispatched. If you still see a couple of frames after clicking the button, this may mean that timer events were sent faster than they could be processed.