I'm writing a simple Qt program to capture video feed from camera (using OpenCV). I'm using a QThread
object that loops, capturing images and feeding them to the MainWindow
object. This is working as it should.
The problem is that when I close, the application (i.e. pressing the "X") the camera capturing thread stops and the gui disappears. But the program is still running in the background. I also get a warning in the application output saying :
QThread: Destroyed while thread is still running.
How can I stop the application completely when quitting it?
main.cpp
#include <QApplication>
#include "application.h"
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Application app;
app.init();
return a.exec();
}
application.h
#include "mainwindow.h"
#include "camerathread.h"
#include "mathandler.h"
#include "tools.h"
#include "opencv2/core/core.hpp"
#ifndef APPLICATION
#define APPLICATION
class Application : public MatHandler{
MainWindow w;
CameraThread ct;
public:
Application() {
w.setFixedSize(800,600);
}
void init() {
ct.setMatHandler(this);
ct.start();
w.show();
}
void handleMat(cv::Mat mat) {
QImage qImage = toQImage(mat);
w.setImage(qImage);
}
};
#endif // APPLICATION
camerathread
#include <QThread>
#include "mathandler.h"
#include "opencv2/highgui/highgui.hpp"
#ifndef CAMERATHREAD
#define CAMERATHREAD
class CameraThread : public QThread {
MatHandler *matHandler;
public:
~CameraThread() {
}
void setMatHandler(MatHandler *h) {
matHandler = h;
}
private: void run() {
cv::VideoCapture vc(0);
if (vc.isOpened()) {
for(;;) {
cv::Mat img;
vc >> img;
matHandler->handleMat(img);
}
}
}
};
#endif // CAMERATHREAD
The program consists of more code than this, but I only included the code I think is relevant for the question. I'll post the rest if necessary.